import gradio as gr import pandas as pd import os import atexit from fastrtc import WebRTC, ReplyOnPause, get_stt_model, get_tts_model from settings import Settings from pydantic_ai.messages import ( ModelMessage, ModelRequest, ModelResponse, UserPromptPart, TextPart ) from agents import form_agent, response_agent # Config and Globals settings = Settings() stt_model = get_stt_model() tts_model = get_tts_model() messages: list[ModelMessage] = [] DATA_PATH = "data.csv" df = pd.read_csv(DATA_PATH) if os.path.exists(DATA_PATH) else pd.DataFrame(columns=["customer_name", "request_type", "issue", "emotion"]) def save_data_on_exit(): df.to_csv(DATA_PATH, index=False) atexit.register(save_data_on_exit) def df_update(): global df try: form_response = form_agent.run_sync(user_prompt="Do your thing", message_history=messages) new_row = { "customer_name": form_response.data.customername, "request_type": form_response.data.requesttype, "issue": form_response.data.issue, "emotion": form_response.data.emotion } df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True) df.to_csv(DATA_PATH, index=False) return "โ DataFrame updated successfully!" except Exception as e: return f"โ Update failed: {str(e)}" def update_table(): global df if os.path.exists(DATA_PATH): df = pd.read_csv(DATA_PATH) else: df = pd.DataFrame(columns=["customer_name", "request_type", "issue", "emotion"]) return df def reset_memory(): global messages messages = [] return "๐ง Memory reset successfully." async def handle_audio(audio): prompt = stt_model.stt(audio) response_text = await response_agent.run(user_prompt=prompt, message_history=messages) messages.append(ModelRequest(parts=[UserPromptPart(content=prompt)])) messages.append(ModelResponse(parts=[TextPart(content=response_text.data)])) for chunk in tts_model.stream_tts(response_text.data): yield chunk async def handle_text_chat(user_text, history): response = await response_agent.run(user_prompt=user_text, message_history=messages) messages.append(ModelRequest(parts=[UserPromptPart(content=user_text)])) messages.append(ModelResponse(parts=[TextPart(content=response.data)])) history = history + [[user_text, response.data]] return "", history # Gradio UI with gr.Blocks(css=""" .toolbox { display: flex; gap: 0.5rem; margin-top: 0.5rem; } .footer-note { text-align: center; font-size: 0.85rem; color: #666; margin-top: 1rem; } """) as demo: gr.Markdown("