Update app.py
Browse files
app.py
CHANGED
|
@@ -52,11 +52,11 @@ def get_ai_response(text, detected_language="ar"):
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error getting AI response: {str(e)}"
|
| 54 |
|
| 55 |
-
def text_to_speech(text, language="ar"):
|
| 56 |
"""Convert text to speech using Groq TTS"""
|
| 57 |
try:
|
| 58 |
-
#
|
| 59 |
-
voice =
|
| 60 |
model = "playai-tts-arabic"
|
| 61 |
|
| 62 |
response = client.audio.speech.create(
|
|
@@ -76,7 +76,7 @@ def text_to_speech(text, language="ar"):
|
|
| 76 |
return f"Error generating audio: {str(e)}"
|
| 77 |
|
| 78 |
# -------------------- Gradio Interface Function --------------------
|
| 79 |
-
def process_voice_chat(audio):
|
| 80 |
"""Main function to process voice input and generate response"""
|
| 81 |
|
| 82 |
if audio is None:
|
|
@@ -97,8 +97,8 @@ def process_voice_chat(audio):
|
|
| 97 |
if ai_response.startswith("Error"):
|
| 98 |
return transcription, ai_response, None
|
| 99 |
|
| 100 |
-
# Step 3: Convert response to speech
|
| 101 |
-
audio_output = text_to_speech(ai_response, "ar")
|
| 102 |
|
| 103 |
# Check if audio generation failed
|
| 104 |
if isinstance(audio_output, str) and audio_output.startswith("Error"):
|
|
@@ -117,6 +117,12 @@ with gr.Blocks(title="مساعد المحادثة الصوتية", theme=gr.them
|
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
audio_input = gr.Audio(
|
| 121 |
sources=["microphone", "upload"],
|
| 122 |
type="filepath",
|
|
@@ -144,15 +150,16 @@ with gr.Blocks(title="مساعد المحادثة الصوتية", theme=gr.them
|
|
| 144 |
# Button action
|
| 145 |
submit_btn.click(
|
| 146 |
fn=process_voice_chat,
|
| 147 |
-
inputs=[audio_input],
|
| 148 |
outputs=[transcription_output, ai_response_output, audio_output]
|
| 149 |
)
|
| 150 |
|
| 151 |
gr.Markdown(
|
| 152 |
"""
|
| 153 |
### 📝 التعليمات:
|
| 154 |
-
1.
|
| 155 |
-
2.
|
|
|
|
| 156 |
|
| 157 |
### 🔑 للنشر على Hugging Face Spaces:
|
| 158 |
أضف هذه الأسرار في إعدادات المساحة:
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error getting AI response: {str(e)}"
|
| 54 |
|
| 55 |
+
def text_to_speech(text, voice_choice="Amira-PlayAI", language="ar"):
|
| 56 |
"""Convert text to speech using Groq TTS"""
|
| 57 |
try:
|
| 58 |
+
# Use selected Arabic voice
|
| 59 |
+
voice = voice_choice
|
| 60 |
model = "playai-tts-arabic"
|
| 61 |
|
| 62 |
response = client.audio.speech.create(
|
|
|
|
| 76 |
return f"Error generating audio: {str(e)}"
|
| 77 |
|
| 78 |
# -------------------- Gradio Interface Function --------------------
|
| 79 |
+
def process_voice_chat(audio, voice_choice):
|
| 80 |
"""Main function to process voice input and generate response"""
|
| 81 |
|
| 82 |
if audio is None:
|
|
|
|
| 97 |
if ai_response.startswith("Error"):
|
| 98 |
return transcription, ai_response, None
|
| 99 |
|
| 100 |
+
# Step 3: Convert response to speech with selected voice
|
| 101 |
+
audio_output = text_to_speech(ai_response, voice_choice, "ar")
|
| 102 |
|
| 103 |
# Check if audio generation failed
|
| 104 |
if isinstance(audio_output, str) and audio_output.startswith("Error"):
|
|
|
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|
| 120 |
+
voice_selector = gr.Radio(
|
| 121 |
+
choices=["Amira-PlayAI", "Ahmed-PlayAI"],
|
| 122 |
+
value="Amira-PlayAI",
|
| 123 |
+
label="اختر الصوت | Select Voice",
|
| 124 |
+
info="أميرة (أنثى) | أحمد (ذكر)"
|
| 125 |
+
)
|
| 126 |
audio_input = gr.Audio(
|
| 127 |
sources=["microphone", "upload"],
|
| 128 |
type="filepath",
|
|
|
|
| 150 |
# Button action
|
| 151 |
submit_btn.click(
|
| 152 |
fn=process_voice_chat,
|
| 153 |
+
inputs=[audio_input, voice_selector],
|
| 154 |
outputs=[transcription_output, ai_response_output, audio_output]
|
| 155 |
)
|
| 156 |
|
| 157 |
gr.Markdown(
|
| 158 |
"""
|
| 159 |
### 📝 التعليمات:
|
| 160 |
+
1. اختر الصوت المفضل (أميرة أو أحمد)
|
| 161 |
+
2. سجل صوتك باستخدام الميكروفون أو ارفع ملف صوتي
|
| 162 |
+
3. اضغط على "معالجة" للحصول على رد الذكاء الاصطناعي مع صوت
|
| 163 |
|
| 164 |
### 🔑 للنشر على Hugging Face Spaces:
|
| 165 |
أضف هذه الأسرار في إعدادات المساحة:
|