EmadAgha commited on
Commit
4ebd109
·
verified ·
1 Parent(s): fbcb457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -47
app.py CHANGED
@@ -1,62 +1,68 @@
1
  import gradio as gr
2
  import edge_tts
3
- import tempfile
4
  import asyncio
 
 
5
 
6
- # Map frontend voice labels to Edge TTS internal IDs
7
- VOICES = {
8
- "رجل (مصري)": "ar-EG-ShakirNeural",
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
- communicate = edge_tts.Communicate(text, voice_id)
 
 
29
 
30
- # Create a temporary file
31
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
32
- tmp_path = tmp_file.name
33
-
34
- await communicate.save(tmp_path)
35
- return tmp_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Build the Interface
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.Textbox(label="Voice")
43
- emotion_input = gr.Textbox(label="Emotion")
44
- advanced_input = gr.Checkbox(label="Is Advanced")
45
- rate_input = gr.Number(label="Rate")
46
- pitch_input = gr.Textbox(label="Pitch")
 
 
 
47
 
48
- audio_output = gr.Audio(label="Generated Audio", type="filepath")
 
 
49
 
50
- generate_btn = gr.Button("Generate")
51
-
52
- # The API name must match what is in audioService.ts
53
- generate_btn.click(
54
- fn=generate_audio,
55
- inputs=[text_input, voice_input, emotion_input, advanced_input, rate_input, pitch_input],
56
- outputs=audio_output,
57
- api_name="text_to_speech_edge"
 
58
  )
59
 
60
- # CRITICAL: This enables parallel processing.
61
- # The frontend sends 10 requests at once; this allows the backend to accept 20 at once.
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()