Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""podcastcreatorapp.ipynb | |
Automatically generated by Colab. | |
Original file is located at | |
https://colab.research.google.com/drive/1VN0_bQSwY0tC9PzGMbeU-j7iN4TLbz8C | |
""" | |
# Commented out IPython magic to ensure Python compatibility. | |
!git clone https://github.com/myshell-ai/MeloTTS.git | |
# %cd MeloTTS | |
!pip install flask groq requests | |
!pip install cn2an pypinyin num2words pykakasi mecab-python3 unidic-lite fugashi g2p_en anyascii jamo gruut cached_path | |
!pip install gradio | |
# from flask_ngrok import run_with_ngrok | |
from flask import Flask, request, render_template | |
import re | |
import os | |
import requests | |
from groq import Groq | |
from melo.api import TTS | |
app = Flask(__name__) | |
# Initialize MeloTTS model | |
import torch | |
from melo.api import TTS | |
from melo.app import speaker_ids | |
device = 'auto' | |
model = TTS(language='EN', device=device) | |
speaker_ids = model.hps.data.spk2id | |
# Directory to save audio files | |
AUDIO_DIR = 'static' | |
os.makedirs(AUDIO_DIR, exist_ok=True) | |
TEMPLATE_DIR = 'templates' | |
os.makedirs(TEMPLATE_DIR, exist_ok=True) | |
# Retrieve the API key from the environment variable | |
GROQ_API_KEY = os.getenv('GROQ_API_KEY') | |
# Initialize the Groq client with the API key | |
groq_client = Groq(api_key=GROQ_API_KEY) | |
def clean_text_for_tts(text): | |
# Replace or remove unwanted characters | |
clean_text = re.sub(r'\*\*', '', text) # Remove double asterisks | |
clean_text = re.sub(r'\*', '', clean_text) # Remove single asterisks | |
clean_text = re.sub(r'•', '', clean_text) # Remove bullet points | |
clean_text = re.sub(r'(\d+\.)', '', clean_text) # Remove numbers followed by a period (like "1.") | |
clean_text = re.sub(r'\n', ' ', clean_text) # Replace newlines with spaces | |
# Additional replacements can be done here as needed | |
return clean_text | |
def generate_podcast_content(prompt): | |
try: | |
response = groq_client.chat.completions.create( | |
messages=[ | |
{"role": "user", "content": prompt} | |
], | |
model="llama3-8b-8192" | |
) | |
content = response.choices[0].message.content | |
return content | |
except Exception as e: | |
print(f"Error in generate_podcast_content: {e}") | |
return "Error generating content." | |
def text_to_speech(text): | |
try: | |
# Clean the text | |
cleaned_text = clean_text_for_tts(text) | |
# Initialize MeloTTS model | |
model = TTS(language='EN', device=device) | |
# Define the path to save the audio file in the static directory | |
static_audio_path = os.path.join('static', 'podcast.wav') | |
# Convert text to speech | |
model.tts_to_file(cleaned_text, static_audio_path, speed=1.0) | |
# Return the audio file path | |
return static_audio_path | |
except Exception as e: | |
print(f"Error in text_to_speech: {e}") | |
return None | |
def index(): | |
if request.method == 'POST': | |
prompt_input = request.form['prompt_input'] | |
content = generate_podcast_content(prompt_input) | |
audio_file_path = text_to_speech(content) | |
return render_template('result.html', content=content, audio_file_path=audio_file_path) | |
return render_template('index.html') | |
if __name__ == '__main__': | |
app.run(debug=True) |