umarabbas890 commited on
Commit
a1b949b
·
verified ·
1 Parent(s): 7220192

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()