Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import edge_tts
|
| 3 |
import tempfile
|
| 4 |
-
import os
|
| 5 |
import asyncio
|
| 6 |
|
| 7 |
-
#
|
| 8 |
VOICE_MAP = {
|
| 9 |
"رجل (مصري)": "ar-EG-ShakirNeural",
|
| 10 |
"سيدة (مصرية)": "ar-EG-SalmaNeural",
|
|
@@ -15,67 +14,46 @@ VOICE_MAP = {
|
|
| 15 |
}
|
| 16 |
|
| 17 |
async def generate_speech(text, voice, emotion, is_symbol, rate, pitch):
|
| 18 |
-
|
| 19 |
-
if not text or not text.strip():
|
| 20 |
-
return None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
# إذا وصلت القيم فارغة من الواجهة الأمامية، نستخدم القيم الافتراضية
|
| 24 |
final_rate = rate if rate and isinstance(rate, str) and len(rate.strip()) > 0 else "+0%"
|
| 25 |
final_pitch = pitch if pitch and isinstance(pitch, str) and len(pitch.strip()) > 0 else "+0Hz"
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
selected_voice = VOICE_MAP[voice]
|
| 32 |
-
elif voice in VOICE_MAP.values():
|
| 33 |
-
selected_voice = voice
|
| 34 |
|
| 35 |
-
print(f"
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
# 4. إنشاء ملف مؤقت
|
| 39 |
output_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 40 |
output_path = output_file.name
|
| 41 |
output_file.close()
|
| 42 |
|
| 43 |
-
# 5. التوليد باستخدام Edge TTS
|
| 44 |
communicate = edge_tts.Communicate(text, selected_voice, rate=final_rate, pitch=final_pitch)
|
| 45 |
await communicate.save(output_path)
|
| 46 |
-
|
| 47 |
return output_path
|
| 48 |
-
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"
|
| 51 |
-
# في حال حدوث خطأ، نرجح None ليتم التعامل معه في الواجهة
|
| 52 |
raise gr.Error(f"TTS Error: {str(e)}")
|
| 53 |
|
| 54 |
-
#
|
| 55 |
with gr.Blocks(title="Natiq Pro API") as demo:
|
| 56 |
-
gr.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
input_symbol = gr.Checkbox(label="Is Symbol", value=True)
|
| 64 |
-
input_rate = gr.Textbox(label="Rate", value="+0%")
|
| 65 |
-
input_pitch = gr.Textbox(label="Pitch", value="+0Hz")
|
| 66 |
-
|
| 67 |
-
output_audio = gr.Audio(label="Generated Audio", type="filepath")
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
btn.click(
|
| 74 |
-
fn=generate_speech,
|
| 75 |
-
inputs=[input_text, input_voice, input_emotion, input_symbol, input_rate, input_pitch],
|
| 76 |
-
outputs=[output_audio],
|
| 77 |
-
api_name="text_to_speech_edge"
|
| 78 |
-
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import edge_tts
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import asyncio
|
| 5 |
|
| 6 |
+
# Voice Mapping
|
| 7 |
VOICE_MAP = {
|
| 8 |
"رجل (مصري)": "ar-EG-ShakirNeural",
|
| 9 |
"سيدة (مصرية)": "ar-EG-SalmaNeural",
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
async def generate_speech(text, voice, emotion, is_symbol, rate, pitch):
|
| 17 |
+
if not text or not text.strip(): return None
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Defaults
|
|
|
|
| 20 |
final_rate = rate if rate and isinstance(rate, str) and len(rate.strip()) > 0 else "+0%"
|
| 21 |
final_pitch = pitch if pitch and isinstance(pitch, str) and len(pitch.strip()) > 0 else "+0Hz"
|
| 22 |
|
| 23 |
+
# Voice Selection
|
| 24 |
+
selected_voice = "ar-SA-HamedNeural"
|
| 25 |
+
if voice in VOICE_MAP: selected_voice = VOICE_MAP[voice]
|
| 26 |
+
elif voice in VOICE_MAP.values(): selected_voice = voice
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
print(f"Generating: {len(text)} chars | {selected_voice} | {final_rate} | {final_pitch}")
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
output_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 32 |
output_path = output_file.name
|
| 33 |
output_file.close()
|
| 34 |
|
|
|
|
| 35 |
communicate = edge_tts.Communicate(text, selected_voice, rate=final_rate, pitch=final_pitch)
|
| 36 |
await communicate.save(output_path)
|
|
|
|
| 37 |
return output_path
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"ERROR: {str(e)}")
|
|
|
|
| 40 |
raise gr.Error(f"TTS Error: {str(e)}")
|
| 41 |
|
| 42 |
+
# UI Definition using Blocks (Fixes TypeError)
|
| 43 |
with gr.Blocks(title="Natiq Pro API") as demo:
|
| 44 |
+
with gr.Row(visible=False):
|
| 45 |
+
t = gr.Textbox(label="Text")
|
| 46 |
+
v = gr.Textbox(label="Voice")
|
| 47 |
+
e = gr.Textbox(label="Emotion", value="neutral")
|
| 48 |
+
s = gr.Checkbox(label="Is Symbol", value=True)
|
| 49 |
+
r = gr.Textbox(label="Rate", value="+0%")
|
| 50 |
+
p = gr.Textbox(label="Pitch", value="+0Hz")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
o = gr.Audio(label="Output", type="filepath")
|
| 53 |
+
b = gr.Button("Generate", visible=False)
|
| 54 |
|
| 55 |
+
# Explicit API Name
|
| 56 |
+
b.click(generate_speech, inputs=[t,v,e,s,r,p], outputs=[o], api_name="text_to_speech_edge")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.queue().launch()
|