Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
| 3 |
-
import uuid
|
| 4 |
import os
|
|
|
|
| 5 |
from pydub import AudioSegment
|
| 6 |
|
| 7 |
-
# Load XTTS v2 model (
|
| 8 |
-
tts = TTS(model_name="tts_models/
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
"Neutral":
|
| 13 |
"Sad": "sad",
|
| 14 |
"Happy": "happy",
|
| 15 |
"Angry": "angry",
|
| 16 |
"Excited": "excited"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
# Generate
|
| 20 |
def generate_voice(script, emotion):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
tts.tts_to_file(
|
| 26 |
text=script,
|
| 27 |
-
|
| 28 |
-
speaker_wav=None, # You can also add your own voice sample here
|
| 29 |
language="en",
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
# Convert to MP3 using pydub
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
return
|
| 39 |
|
| 40 |
-
# Gradio
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("## 🎙️ AI Voiceover Generator with
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
|
|
|
| 3 |
import os
|
| 4 |
+
import uuid
|
| 5 |
from pydub import AudioSegment
|
| 6 |
|
| 7 |
+
# Load XTTS v2 model (correct model path)
|
| 8 |
+
tts = TTS(model_name="tts_models/en/xtts_v2", progress_bar=False, gpu=False)
|
| 9 |
|
| 10 |
+
# Emotions mapped to style embeddings
|
| 11 |
+
emotion_styles = {
|
| 12 |
+
"Neutral": "neutral",
|
| 13 |
"Sad": "sad",
|
| 14 |
"Happy": "happy",
|
| 15 |
"Angry": "angry",
|
| 16 |
"Excited": "excited"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# Generate voice and save as MP3
|
| 20 |
def generate_voice(script, emotion):
|
| 21 |
+
if not script.strip():
|
| 22 |
+
return "Please enter a script.", None
|
| 23 |
+
|
| 24 |
+
style = emotion_styles.get(emotion, "neutral")
|
| 25 |
+
|
| 26 |
+
# Generate speech (XTTS auto-selects English and supports style)
|
| 27 |
+
output_path = f"output_{uuid.uuid4().hex}.wav"
|
| 28 |
tts.tts_to_file(
|
| 29 |
text=script,
|
| 30 |
+
speaker_wav=None,
|
|
|
|
| 31 |
language="en",
|
| 32 |
+
file_path=output_path,
|
| 33 |
+
style_wav=None,
|
| 34 |
+
style=style
|
| 35 |
)
|
| 36 |
|
| 37 |
# Convert to MP3 using pydub
|
| 38 |
+
mp3_path = output_path.replace(".wav", ".mp3")
|
| 39 |
+
sound = AudioSegment.from_wav(output_path)
|
| 40 |
+
sound.export(mp3_path, format="mp3")
|
| 41 |
|
| 42 |
+
return mp3_path, mp3_path
|
| 43 |
|
| 44 |
+
# Gradio Interface
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("## 🎙️ AI Voiceover Generator with Emotion Control\nConvert your script into a voiceover with the tone you choose!")
|
| 47 |
|
| 48 |
+
with gr.Row():
|
| 49 |
+
script_input = gr.Textbox(label="Enter Your Script", lines=5, placeholder="Type your video script here...")
|
| 50 |
+
emotion_choice = gr.Dropdown(["Neutral", "Sad", "Happy", "Angry", "Excited"], label="Select Emotion", value="Neutral")
|
| 51 |
|
| 52 |
+
generate_button = gr.Button("🎤 Generate Voiceover")
|
| 53 |
+
audio_output = gr.Audio(label="Listen", type="filepath")
|
| 54 |
+
download_link = gr.File(label="Download MP3")
|
| 55 |
|
| 56 |
+
generate_button.click(fn=generate_voice, inputs=[script_input, emotion_choice], outputs=[audio_output, download_link])
|
| 57 |
|
| 58 |
+
demo.launch()
|
|
|