umarabbas890 commited on
Commit
c8774e7
·
verified ·
1 Parent(s): 07a7d2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -10
app.py CHANGED
@@ -1,24 +1,54 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
  import uuid
 
 
4
 
5
- tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
 
6
 
7
- def generate_voice(script):
8
- output_path = f"output_{uuid.uuid4().hex}.wav"
9
- tts.tts_to_file(text=script, file_path=output_path)
10
- return output_path, output_path
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with gr.Blocks() as demo:
13
- gr.Markdown("## 🎙️ AI Voiceover Generator")
14
 
15
- script_input = gr.Textbox(label="Enter Script", placeholder="Type here...", lines=4, max_lines=4, max_length=200)
 
16
  generate_btn = gr.Button("Generate Voice")
17
 
18
- audio_output = gr.Audio(label="Preview", type="filepath")
19
- download_output = gr.File(label="Download")
20
 
21
- generate_btn.click(fn=generate_voice, inputs=script_input, outputs=[audio_output, download_output])
22
 
23
  if __name__ == "__main__":
24
  demo.launch()
 
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()