Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_audio:
|
| 11 |
-
audio_path = tmp_audio.name
|
| 12 |
-
tts.save(audio_path)
|
| 13 |
-
|
| 14 |
-
# Read audio file in binary mode
|
| 15 |
-
with open(audio_path, "rb") as audio_file:
|
| 16 |
-
audio_data = audio_file.read()
|
| 17 |
-
|
| 18 |
-
# Groq API endpoint for audio transcription
|
| 19 |
-
groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
result = response.json()
|
| 42 |
-
return result.get("text", "No transcription available.")
|
| 43 |
-
else:
|
| 44 |
-
return f"Error: {response.status_code}, {response.text}"
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import edge_tts
|
| 3 |
+
import asyncio
|
| 4 |
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Get all available voices
|
| 8 |
+
async def get_voices():
|
| 9 |
+
voices = await edge_tts.list_voices()
|
| 10 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Text-to-speech function
|
| 13 |
+
async def text_to_speech(text, voice, rate, pitch):
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return None, gr.Warning("Please enter text to convert.")
|
| 16 |
+
if not voice:
|
| 17 |
+
return None, gr.Warning("Please select a voice.")
|
| 18 |
+
|
| 19 |
+
voice_short_name = voice.split(" - ")[0]
|
| 20 |
+
rate_str = f"{rate:+d}%"
|
| 21 |
+
pitch_str = f"{pitch:+d}Hz"
|
| 22 |
+
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
| 23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 24 |
+
tmp_path = tmp_file.name
|
| 25 |
+
await communicate.save(tmp_path)
|
| 26 |
+
return tmp_path, None
|
| 27 |
|
| 28 |
+
# Gradio interface function
|
| 29 |
+
def tts_interface(text, voice, rate, pitch):
|
| 30 |
+
audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
|
| 31 |
+
return audio, warning
|
| 32 |
|
| 33 |
+
# Create Gradio application
|
| 34 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
async def create_demo():
|
| 37 |
+
voices = await get_voices()
|
| 38 |
+
|
| 39 |
+
description = """
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=tts_interface,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Textbox(label="Input Text", lines=5),
|
| 46 |
+
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
|
| 47 |
+
gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
|
| 48 |
+
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
|
| 49 |
+
],
|
| 50 |
+
outputs=[
|
| 51 |
+
gr.Audio(label="Generated Audio", type="filepath"),
|
| 52 |
+
gr.Markdown(label="Warning", visible=False)
|
| 53 |
+
],
|
| 54 |
+
title="Edge TTS Text-to-Speech",
|
| 55 |
+
description=description,
|
| 56 |
+
article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
|
| 57 |
+
analytics_enabled=False,
|
| 58 |
+
allow_flagging="manual"
|
| 59 |
+
)
|
| 60 |
+
return demo
|
| 61 |
|
| 62 |
+
# Run the application
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo = asyncio.run(create_demo())
|
| 65 |
+
demo.launch()
|