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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -56
app.py CHANGED
@@ -1,66 +1,24 @@
1
- # app.py
2
  import gradio as gr
3
- import torch
4
- import torchaudio
5
  from TTS.api import TTS
6
  import uuid
7
- import os
8
 
9
- # Load the XTTS model from Coqui
10
- model_name = "tts_models/multilingual/multi-dataset/xtts_v2"
11
- tts = TTS(model_name)
12
 
13
- # Emotions supported by XTTS
14
- EMOTION_MAP = {
15
- "Neutral": "neutral",
16
- "Sad": "sad",
17
- "Happy": "happy",
18
- "Angry": "angry",
19
- "Excited": "excited"
20
- }
21
 
22
- # Output directory
23
- os.makedirs("outputs", exist_ok=True)
24
 
25
- def generate_voiceover(text, emotion):
26
- emotion_label = EMOTION_MAP.get(emotion, "neutral")
27
-
28
- # Generate a temporary filename
29
- output_path = f"outputs/{uuid.uuid4().hex}.wav"
30
 
31
- # XTTS only supports cloning voice with reference. Use default speaker.
32
- tts.tts_to_file(
33
- text=text,
34
- file_path=output_path,
35
- speaker_wav=None,
36
- language="en",
37
- emotion=emotion_label
38
- )
39
 
40
- # Convert to MP3
41
- mp3_path = output_path.replace(".wav", ".mp3")
42
- waveform, sample_rate = torchaudio.load(output_path)
43
- torchaudio.save(mp3_path, waveform, sample_rate, format="mp3")
44
 
45
- return mp3_path
46
-
47
- # Gradio UI
48
- def app(text, emotion):
49
- mp3_path = generate_voiceover(text, emotion)
50
- return mp3_path, mp3_path
51
-
52
- iface = gr.Interface(
53
- fn=app,
54
- inputs=[
55
- gr.Textbox(label="Enter your script here", lines=5, placeholder="Type something..."),
56
- gr.Dropdown(label="Choose Emotion", choices=list(EMOTION_MAP.keys()), value="Neutral")
57
- ],
58
- outputs=[
59
- gr.Audio(label="Generated Voiceover"),
60
- gr.File(label="Download MP3")
61
- ],
62
- title="🎙️ AI Voiceover Generator with Emotion Control",
63
- description="Convert your script into a voiceover with emotion using XTTS (free & open-source)."
64
- )
65
-
66
- iface.launch()
 
 
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()