Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -188,43 +188,43 @@ def get_chat_history_string(chat_memory):
|
|
188 |
return history
|
189 |
|
190 |
# تابع تبدیل متن به گفتار با استفاده از Azure Speech SDK
|
191 |
-
def synthesize_speech(text):
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
def get_model_response(messages):
|
229 |
response = client.chat.completions.create(
|
230 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
@@ -346,41 +346,41 @@ import requests
|
|
346 |
# else:
|
347 |
# return {"error": response.json()}
|
348 |
|
349 |
-
def process_audio(audio):
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
|
369 |
-
|
370 |
|
371 |
-
|
372 |
-
|
373 |
|
374 |
-
|
375 |
-
|
376 |
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
|
385 |
# def process_audio(audio):
|
386 |
# # باز کردن فایل صوتی ضبطشده
|
@@ -433,14 +433,14 @@ def clear_memory():
|
|
433 |
with gr.Blocks() as demo:
|
434 |
chatbot = gr.Chatbot(height=500)
|
435 |
msg = gr.Textbox(show_label=False, placeholder="Send Message")
|
436 |
-
audio_input = gr.Audio(sources="microphone", type="filepath",label="Audio voice to voice")
|
437 |
-
audio_output = gr.Audio(label="Assistant voice response") # افزودن کامپوننت صوتی برای خروجی
|
438 |
-
inputs=gr.Audio(sources="microphone", type="filepath",label="Audio voice to text")
|
439 |
with gr.Row():
|
440 |
submit_btn = gr.Button("send")
|
441 |
-
voice_btn = gr.Button("voice to voice")
|
442 |
clear_btn = gr.Button("clear_chat 🧹")
|
443 |
-
voice_btn1 = gr.Button("voice to text")
|
444 |
def fix_bidi_text(text):
|
445 |
RLE = '\u202B' # Right-To-Left Embedding
|
446 |
PDF = '\u202C' # Pop Directional Formatting
|
@@ -454,46 +454,46 @@ with gr.Blocks() as demo:
|
|
454 |
chat_history.append((fixed_message, fixed_bot_response))
|
455 |
|
456 |
# تبدیل پاسخ به صوت
|
457 |
-
audio_data = synthesize_speech(bot_response)
|
458 |
return chat_history, "" # افزودن audio_data به خروجی
|
459 |
|
460 |
-
def response_voice(audio, chat_history):
|
461 |
-
|
462 |
-
|
463 |
|
464 |
-
|
465 |
-
|
466 |
|
467 |
-
|
468 |
-
|
469 |
|
470 |
-
|
471 |
-
|
472 |
|
473 |
-
|
474 |
|
475 |
-
|
476 |
-
|
477 |
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
def response_voice1(audio, chat_history):
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
submit_btn.click(respond, [msg, chatbot], [chatbot, msg])
|
492 |
-
voice_btn.click(response_voice, [audio_input, chatbot], [chatbot, msg, audio_output])
|
493 |
-
voice_btn1.click(response_voice1, [inputs, chatbot], [chatbot, msg])
|
494 |
|
495 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
496 |
-
clear_btn.click(clear_memory, inputs=None, outputs=[chatbot, msg
|
497 |
|
498 |
# اجرای Gradio
|
499 |
if __name__ == "__main__":
|
|
|
188 |
return history
|
189 |
|
190 |
# تابع تبدیل متن به گفتار با استفاده از Azure Speech SDK
|
191 |
+
# def synthesize_speech(text):
|
192 |
+
# try:
|
193 |
+
# speech_config = speechsdk.SpeechConfig(subscription=AZURE_SPEECH_API_KEY, region=AZURE_SPEECH_REGION)
|
194 |
+
# speech_config.speech_synthesis_voice_name = AZURE_SPEECH_VOICE_NAME
|
195 |
+
|
196 |
+
# # تنظیم فرمت خروجی صوتی
|
197 |
+
# speech_config.set_speech_synthesis_output_format(
|
198 |
+
# speechsdk.SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3
|
199 |
+
# )
|
200 |
+
|
201 |
+
# # تولید نام فایل موقت برای ذخیره صوت
|
202 |
+
# voice_generate_path = f'{uuid.uuid4()}.mp3'
|
203 |
+
# temp_voice_generate_path = os.path.join(os.getcwd(), voice_generate_path)
|
204 |
+
|
205 |
+
# # تنظیم خروجی به فایل
|
206 |
+
# audio_config = speechsdk.audio.AudioOutputConfig(filename=temp_voice_generate_path)
|
207 |
+
|
208 |
+
# speech_synthesizer = speechsdk.SpeechSynthesizer(
|
209 |
+
# speech_config=speech_config,
|
210 |
+
# audio_config=audio_config
|
211 |
+
# )
|
212 |
+
|
213 |
+
# speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
|
214 |
+
|
215 |
+
# if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
|
216 |
+
# # خواندن دادههای صوتی از فایل
|
217 |
+
# with open(temp_voice_generate_path, "rb") as audio_file:
|
218 |
+
# audio_data = audio_file.read()
|
219 |
+
# # حذف فایل موقت
|
220 |
+
# os.remove(temp_voice_generate_path)
|
221 |
+
# return audio_data
|
222 |
+
# else:
|
223 |
+
# print("خطا در تبدیل متن به گفتار:", speech_synthesis_result.reason)
|
224 |
+
# return None
|
225 |
+
# except Exception as e:
|
226 |
+
# print("خطا در تبدیل متن به گفتار:", e)
|
227 |
+
# return None
|
228 |
def get_model_response(messages):
|
229 |
response = client.chat.completions.create(
|
230 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
|
|
346 |
# else:
|
347 |
# return {"error": response.json()}
|
348 |
|
349 |
+
# def process_audio(audio):
|
350 |
+
# audio_file = open(audio, "rb")
|
351 |
+
# try:
|
352 |
+
# audio = AudioSegment.from_file(audio_file)
|
353 |
+
# except CouldntDecodeError:
|
354 |
+
# os.remove(audio)
|
355 |
+
# return "Unsupported audio format"
|
356 |
+
|
357 |
+
# # بررسی مدت زمان فایل صوتی
|
358 |
+
# duration_seconds = len(audio_file) / 1000.0 # مدت زمان به ثانیه
|
359 |
+
# if duration_seconds > 900:
|
360 |
+
# os.remove(audio)
|
361 |
+
# return "Audio file is too long"
|
362 |
+
|
363 |
+
# project_root = os.path.dirname(os.path.dirname(__file__))
|
364 |
+
# voice_id = str(uuid.uuid4())
|
365 |
+
# # تبدیل به فرمت WAV سازگار با Whisper
|
366 |
+
# whisper_path = f'{voice_id}.wav'
|
367 |
+
# whisper_voice_path = os.path.join(project_root, whisper_path)
|
368 |
|
369 |
+
# audio.export(whisper_voice_path, format='wav')
|
370 |
|
371 |
+
# # ارسال درخواست به API Hugging Face
|
372 |
+
# output = query(whisper_voice_path)
|
373 |
|
374 |
+
# # حذف فایل موقت
|
375 |
+
# os.remove(whisper_voice_path)
|
376 |
|
377 |
+
# if "error" in output:
|
378 |
+
# print("Error:", output["error"])
|
379 |
+
# return "Transcription failed"
|
380 |
+
# else:
|
381 |
+
# text_question = output.get("text", "")
|
382 |
+
# print("text_question =", text_question)
|
383 |
+
# return text_question
|
384 |
|
385 |
# def process_audio(audio):
|
386 |
# # باز کردن فایل صوتی ضبطشده
|
|
|
433 |
with gr.Blocks() as demo:
|
434 |
chatbot = gr.Chatbot(height=500)
|
435 |
msg = gr.Textbox(show_label=False, placeholder="Send Message")
|
436 |
+
# audio_input = gr.Audio(sources="microphone", type="filepath",label="Audio voice to voice")
|
437 |
+
# audio_output = gr.Audio(label="Assistant voice response") # افزودن کامپوننت صوتی برای خروجی
|
438 |
+
# inputs=gr.Audio(sources="microphone", type="filepath",label="Audio voice to text")
|
439 |
with gr.Row():
|
440 |
submit_btn = gr.Button("send")
|
441 |
+
# voice_btn = gr.Button("voice to voice")
|
442 |
clear_btn = gr.Button("clear_chat 🧹")
|
443 |
+
# voice_btn1 = gr.Button("voice to text")
|
444 |
def fix_bidi_text(text):
|
445 |
RLE = '\u202B' # Right-To-Left Embedding
|
446 |
PDF = '\u202C' # Pop Directional Formatting
|
|
|
454 |
chat_history.append((fixed_message, fixed_bot_response))
|
455 |
|
456 |
# تبدیل پاسخ به صوت
|
457 |
+
# audio_data = synthesize_speech(bot_response)
|
458 |
return chat_history, "" # افزودن audio_data به خروجی
|
459 |
|
460 |
+
# def response_voice(audio, chat_history):
|
461 |
+
# if not audio:
|
462 |
+
# return chat_history, "فایل صوتی ارائه نشده است.", None
|
463 |
|
464 |
+
# # پردازش فایل صوتی و دریافت متن تبدیلشده
|
465 |
+
# transcribed_text = process_audio(audio)
|
466 |
|
467 |
+
# # دریافت پاسخ مدل با استفاده از متن تبدیلشده
|
468 |
+
# bot_response = agent_respond(transcribed_text)
|
469 |
|
470 |
+
# # اصلاح متنها برای نمایش راستچین
|
471 |
+
# fixed_user_message = fix_bidi_text(transcribed_text)
|
472 |
|
473 |
+
# fixed_bot_response = fix_bidi_text(bot_response)
|
474 |
|
475 |
+
# # افزودن پیامها به تاریخچه چت
|
476 |
+
# chat_history.append((fixed_user_message, fixed_bot_response))
|
477 |
|
478 |
+
# # تبدیل پاسخ به صوت
|
479 |
+
# audio_data = synthesize_speech(bot_response)
|
480 |
+
# return chat_history, "", audio_data
|
481 |
+
# # افزودن audio_data به خروجی
|
482 |
+
# def response_voice1(audio, chat_history):
|
483 |
+
# if not audio:
|
484 |
+
# return chat_history, "No audio file provided."
|
485 |
+
|
486 |
+
# # پردازش فایل صوتی
|
487 |
+
# bot_response = process_audio(audio)
|
488 |
+
# fixed_bot_response = fix_bidi_text(bot_response)
|
489 |
+
# chat_history.append(("صدا ارسال شد", fixed_bot_response))
|
490 |
+
# return chat_history, ""
|
491 |
submit_btn.click(respond, [msg, chatbot], [chatbot, msg])
|
492 |
+
# voice_btn.click(response_voice, [audio_input, chatbot], [chatbot, msg, audio_output])
|
493 |
+
# voice_btn1.click(response_voice1, [inputs, chatbot], [chatbot, msg])
|
494 |
|
495 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
496 |
+
clear_btn.click(clear_memory, inputs=None, outputs=[chatbot, msg])
|
497 |
|
498 |
# اجرای Gradio
|
499 |
if __name__ == "__main__":
|