Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,8 @@ client_groq = Groq(api_key=GROQ_API_KEY)
|
|
| 24 |
async def nilla_engine(audio_path, chat_history):
|
| 25 |
if audio_path is None:
|
| 26 |
return None, "", "", chat_history
|
| 27 |
-
|
|
|
|
| 28 |
if chat_history is None:
|
| 29 |
chat_history = []
|
| 30 |
|
|
@@ -37,7 +38,6 @@ async def nilla_engine(audio_path, chat_history):
|
|
| 37 |
)
|
| 38 |
|
| 39 |
user_text = transcription.text
|
| 40 |
-
|
| 41 |
output_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 42 |
output_path = output_temp.name
|
| 43 |
|
|
@@ -53,10 +53,7 @@ async def nilla_engine(audio_path, chat_history):
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
async with client_gemini.aio.live.connect(model=MODEL_ID, config=config) as session:
|
| 56 |
-
await session.send_client_content(
|
| 57 |
-
turns=current_turns,
|
| 58 |
-
turn_complete=True
|
| 59 |
-
)
|
| 60 |
|
| 61 |
with wave.open(output_path, "wb") as wav:
|
| 62 |
wav.setnchannels(1)
|
|
@@ -66,15 +63,13 @@ async def nilla_engine(audio_path, chat_history):
|
|
| 66 |
async for response in session.receive():
|
| 67 |
if response.data is not None:
|
| 68 |
wav.writeframes(response.data)
|
| 69 |
-
|
| 70 |
if response.server_content and response.server_content.output_transcription:
|
| 71 |
model_response_text += response.server_content.output_transcription.text
|
| 72 |
|
| 73 |
new_history = current_turns + [{"role": "model", "parts": [{"text": model_response_text}]}]
|
| 74 |
-
|
| 75 |
return output_path, user_text, model_response_text, new_history
|
| 76 |
except Exception:
|
| 77 |
-
return None, user_text, "Error
|
| 78 |
|
| 79 |
def run_interface(audio_file, chat_history):
|
| 80 |
loop = asyncio.new_event_loop()
|
|
@@ -83,25 +78,26 @@ def run_interface(audio_file, chat_history):
|
|
| 83 |
|
| 84 |
with gr.Blocks(title=BOT_NAME) as demo:
|
| 85 |
gr.Markdown(f"# {BOT_NAME}")
|
| 86 |
-
gr.Markdown(f"{MOTOR_NAME} | {PROVIDER}")
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
-
input_audio = gr.Audio(label="Speak
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
-
output_text_user = gr.Textbox(label="
|
| 95 |
-
output_text_nilla = gr.Textbox(label="Nilla
|
| 96 |
-
|
| 97 |
-
with gr.Row():
|
| 98 |
-
output_audio = gr.Audio(label="Nilla's Voice Response")
|
| 99 |
|
| 100 |
submit_btn = gr.Button("Process")
|
|
|
|
|
|
|
| 101 |
submit_btn.click(
|
| 102 |
fn=run_interface,
|
| 103 |
-
inputs=[input_audio,
|
| 104 |
-
outputs=[output_audio, output_text_user, output_text_nilla,
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
|
|
|
| 24 |
async def nilla_engine(audio_path, chat_history):
|
| 25 |
if audio_path is None:
|
| 26 |
return None, "", "", chat_history
|
| 27 |
+
|
| 28 |
+
# اطمینان از اینکه هیستوری خالی نباشد
|
| 29 |
if chat_history is None:
|
| 30 |
chat_history = []
|
| 31 |
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
user_text = transcription.text
|
|
|
|
| 41 |
output_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 42 |
output_path = output_temp.name
|
| 43 |
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
async with client_gemini.aio.live.connect(model=MODEL_ID, config=config) as session:
|
| 56 |
+
await session.send_client_content(turns=current_turns, turn_complete=True)
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with wave.open(output_path, "wb") as wav:
|
| 59 |
wav.setnchannels(1)
|
|
|
|
| 63 |
async for response in session.receive():
|
| 64 |
if response.data is not None:
|
| 65 |
wav.writeframes(response.data)
|
|
|
|
| 66 |
if response.server_content and response.server_content.output_transcription:
|
| 67 |
model_response_text += response.server_content.output_transcription.text
|
| 68 |
|
| 69 |
new_history = current_turns + [{"role": "model", "parts": [{"text": model_response_text}]}]
|
|
|
|
| 70 |
return output_path, user_text, model_response_text, new_history
|
| 71 |
except Exception:
|
| 72 |
+
return None, user_text, "Error", chat_history
|
| 73 |
|
| 74 |
def run_interface(audio_file, chat_history):
|
| 75 |
loop = asyncio.new_event_loop()
|
|
|
|
| 78 |
|
| 79 |
with gr.Blocks(title=BOT_NAME) as demo:
|
| 80 |
gr.Markdown(f"# {BOT_NAME}")
|
|
|
|
| 81 |
|
| 82 |
+
# استفاده از JSON به جای State برای قابلیت دسترسی در API
|
| 83 |
+
history_component = gr.JSON(value=[], visible=False)
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
+
input_audio = gr.Audio(label="Speak", type="filepath")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
+
output_text_user = gr.Textbox(label="User")
|
| 90 |
+
output_text_nilla = gr.Textbox(label="Nilla")
|
| 91 |
+
output_audio = gr.Audio(label="Response")
|
|
|
|
|
|
|
| 92 |
|
| 93 |
submit_btn = gr.Button("Process")
|
| 94 |
+
|
| 95 |
+
# تعریف صریح در دکمه
|
| 96 |
submit_btn.click(
|
| 97 |
fn=run_interface,
|
| 98 |
+
inputs=[input_audio, history_component],
|
| 99 |
+
outputs=[output_audio, output_text_user, output_text_nilla, history_component],
|
| 100 |
+
api_name="run_interface" # نام ثابت برای API
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|