Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import edge_tts
|
| 3 |
-
import tempfile
|
| 4 |
import asyncio
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
"سيدة (مصرية)": "ar-EG-SalmaNeural",
|
| 10 |
-
"رجل (سعودي)": "ar-SA-HamedNeural",
|
| 11 |
-
"سيدة (سعودية)": "ar-SA-ZariyahNeural",
|
| 12 |
-
"English (US) F": "en-US-AriaNeural",
|
| 13 |
-
"English (US) M": "en-US-EricNeural"
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
async def generate_audio(text, voice_label, emotion_ignored, advanced_ignored, rate_ignored, pitch_ignored):
|
| 17 |
-
"""
|
| 18 |
-
Generates audio.
|
| 19 |
-
Note: We ignore rate/pitch arguments because Natiq Pro handles
|
| 20 |
-
speed and pitch shifting on the Frontend (Web Audio API) for zero-latency adjustments.
|
| 21 |
-
"""
|
| 22 |
-
if not text or not text.strip():
|
| 23 |
return None
|
| 24 |
-
|
| 25 |
-
# Default to Saudi Male if voice not found
|
| 26 |
-
voice_id = VOICES.get(voice_label, "ar-SA-HamedNeural")
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
# Inputs match the order sent by your frontend
|
| 40 |
with gr.Row():
|
| 41 |
text_input = gr.Textbox(label="Text")
|
| 42 |
-
voice_input = gr.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
demo.queue(max_size=40, default_concurrency_limit=20).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import edge_tts
|
|
|
|
| 3 |
import asyncio
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# الدالة الرئيسية التي تستقبل المعاملات الجديدة
|
| 8 |
+
async def text_to_speech_edge(text, voice, emotion, is_symbol, rate, pitch):
|
| 9 |
+
if not text.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return None
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# التعامل مع القيم الافتراضية إذا كانت فارغة
|
| 13 |
+
if not rate: rate = "+0%"
|
| 14 |
+
if not pitch: pitch = "+0Hz"
|
| 15 |
|
| 16 |
+
# تحديد اسم الصوت بناءً على المدخل
|
| 17 |
+
voice_map = {
|
| 18 |
+
"رجل (مصري)": "ar-EG-ShakirNeural",
|
| 19 |
+
"سيدة (مصرية)": "ar-EG-SalmaNeural",
|
| 20 |
+
"رجل (سعودي)": "ar-SA-HamedNeural",
|
| 21 |
+
"سيدة (سعودية)": "ar-SA-ZariyahNeural",
|
| 22 |
+
"English (US) M": "en-US-EricNeural",
|
| 23 |
+
"English (US) F": "en-US-AriaNeural"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# استخدام المفتاح المباشر إذا لم يكن في القاموس
|
| 27 |
+
selected_voice = voice_map.get(voice, voice)
|
| 28 |
+
|
| 29 |
+
output_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 30 |
+
output_path = output_file.name
|
| 31 |
+
output_file.close()
|
| 32 |
+
|
| 33 |
+
# توليد الصوت مع المعاملات الإضافية
|
| 34 |
+
communicate = edge_tts.Communicate(text, selected_voice, rate=rate, pitch=pitch)
|
| 35 |
+
await communicate.save(output_path)
|
| 36 |
+
|
| 37 |
+
return output_path
|
| 38 |
|
| 39 |
+
# بناء الواجهة
|
| 40 |
with gr.Blocks() as demo:
|
|
|
|
| 41 |
with gr.Row():
|
| 42 |
text_input = gr.Textbox(label="Text")
|
| 43 |
+
voice_input = gr.Dropdown(
|
| 44 |
+
["رجل (مصري)", "سيدة (مصرية)", "رجل (سعودي)", "سيدة (سعودية)", "English (US) M", "English (US) F"],
|
| 45 |
+
label="Voice"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# مدخلات إضافية ليتوافق مع طلب التطبيق
|
| 49 |
+
emotion_input = gr.Textbox(label="Emotion", value="محايد", visible=False)
|
| 50 |
+
symbol_input = gr.Checkbox(label="Is Symbol", value=True, visible=False)
|
| 51 |
|
| 52 |
+
# المعاملات الجديدة المهمة
|
| 53 |
+
rate_input = gr.Textbox(label="Rate", value="+0%")
|
| 54 |
+
pitch_input = gr.Textbox(label="Pitch", value="+0Hz")
|
| 55 |
|
| 56 |
+
output_audio = gr.Audio(label="Output", type="filepath")
|
| 57 |
+
|
| 58 |
+
submit_btn = gr.Button("Generate")
|
| 59 |
+
|
| 60 |
+
# ربط الدالة بالواجهة - تأكد من الترتيب الصحيح للمدخلات
|
| 61 |
+
submit_btn.click(
|
| 62 |
+
text_to_speech_edge,
|
| 63 |
+
inputs=[text_input, voice_input, emotion_input, symbol_input, rate_input, pitch_input],
|
| 64 |
+
outputs=output_audio
|
| 65 |
)
|
| 66 |
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.queue().launch()
|
|
|