Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
def text_to_speech(text, language, pitch):
|
| 8 |
+
tts = gTTS(text=text, lang=language, slow=False)
|
| 9 |
+
|
| 10 |
+
# Save to a temporary file
|
| 11 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 12 |
+
tts.save(fp.name)
|
| 13 |
+
|
| 14 |
+
# Apply pitch shift using ffmpeg
|
| 15 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 16 |
+
pitch_shift = pitch - 1 # Adjust pitch shift value
|
| 17 |
+
os.system(f"ffmpeg -i {fp.name} -af asetrate=44100*{2**pitch_shift},aresample=44100 {output_file}")
|
| 18 |
+
|
| 19 |
+
# Clean up the original file
|
| 20 |
+
os.unlink(fp.name)
|
| 21 |
+
|
| 22 |
+
return output_file
|
| 23 |
+
|
| 24 |
+
def gradio_tts_interface(text, language, pitch):
|
| 25 |
+
audio_file = text_to_speech(text, language, pitch)
|
| 26 |
+
return audio_file
|
| 27 |
+
|
| 28 |
+
iface = gr.Blocks(theme="Hev832/Applio")
|
| 29 |
+
|
| 30 |
+
with iface:
|
| 31 |
+
gr.Markdown("# Text-to-Speech Demo")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
text_input = gr.Textbox(label="Enter text to convert to speech", lines=3)
|
| 36 |
+
language_input = gr.Dropdown(["en", "fr", "es", "de", "it", "id", "ja"], label="Select Language")
|
| 37 |
+
pitch_input = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Pitch (0.5 for lower/male, 1.0 for normal, 2.0 for higher/female)")
|
| 38 |
+
submit_button = gr.Button("Convert to Speech")
|
| 39 |
+
|
| 40 |
+
with gr.Column():
|
| 41 |
+
audio_output = gr.Audio(label="Generated Speech")
|
| 42 |
+
|
| 43 |
+
submit_button.click(
|
| 44 |
+
fn=gradio_tts_interface,
|
| 45 |
+
inputs=[text_input, language_input, pitch_input],
|
| 46 |
+
outputs=audio_output
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
iface.launch()
|