Update app.py
Browse files
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 |
-
|
| 10 |
-
model_name = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 11 |
-
tts = TTS(model_name)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"Happy": "happy",
|
| 18 |
-
"Angry": "angry",
|
| 19 |
-
"Excited": "excited"
|
| 20 |
-
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Generate a temporary filename
|
| 29 |
-
output_path = f"outputs/{uuid.uuid4().hex}.wav"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
text=text,
|
| 34 |
-
file_path=output_path,
|
| 35 |
-
speaker_wav=None,
|
| 36 |
-
language="en",
|
| 37 |
-
emotion=emotion_label
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 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 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|