umarabbas890 commited on
Commit
3957ee2
·
verified ·
1 Parent(s): bd1ed7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
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 (slow on CPU, suitable for ~10s voiceovers)
8
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False, gpu=False)
9
 
10
- # Define emotional styles mapping (XTTS uses style_wav or speaker embedding)
11
- EMOTIONS = {
12
- "Neutral": None,
13
  "Sad": "sad",
14
  "Happy": "happy",
15
  "Angry": "angry",
16
  "Excited": "excited"
17
  }
18
 
19
- # Generate speech function
20
  def generate_voice(script, emotion):
21
- # Output path as WAV first
22
- wav_output_path = f"output_{uuid.uuid4().hex}.wav"
23
-
24
- # XTTS v2 supports emotion as "style" param
 
 
 
25
  tts.tts_to_file(
26
  text=script,
27
- file_path=wav_output_path,
28
- speaker_wav=None, # You can also add your own voice sample here
29
  language="en",
30
- emotion=EMOTIONS[emotion] # Optional style tag
 
 
31
  )
32
 
33
  # Convert to MP3 using pydub
34
- mp3_output_path = wav_output_path.replace(".wav", ".mp3")
35
- audio = AudioSegment.from_wav(wav_output_path)
36
- audio.export(mp3_output_path, format="mp3")
37
 
38
- return mp3_output_path, mp3_output_path
39
 
40
- # Gradio UI
41
  with gr.Blocks() as demo:
42
- gr.Markdown("## 🎙️ AI Voiceover Generator with Emotions")
43
 
44
- script_input = gr.Textbox(label="🎤 Script", placeholder="Enter your voiceover script...", lines=4, max_lines=4)
45
- emotion_input = gr.Dropdown(label="🎭 Emotion", choices=list(EMOTIONS.keys()), value="Neutral")
46
- generate_btn = gr.Button("Generate Voice")
47
 
48
- audio_output = gr.Audio(label="🎧 Listen", type="filepath")
49
- download_output = gr.File(label="⬇️ Download MP3")
 
50
 
51
- generate_btn.click(fn=generate_voice, inputs=[script_input, emotion_input], outputs=[audio_output, download_output])
52
 
53
- if __name__ == "__main__":
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()