import gradio as gr import os import requests import base64 ORCHESTRATOR_URL = os.getenv("HF_ORCHESTRATOR_URL", "") WEBHOOK_SECRET = os.getenv("HF_ORCHESTRATOR_WEBHOOK_SECRET", "") BASIC_USER = os.getenv("N8N_BASIC_AUTH_USER", "") BASIC_PASS = os.getenv("N8N_BASIC_AUTH_PASSWORD", "") def _headers(): headers = {"x-webhook-secret": WEBHOOK_SECRET, "Content-Type": "application/json"} if BASIC_USER and BASIC_PASS: import base64 as b64 token = b64.b64encode(f"{BASIC_USER}:{BASIC_PASS}".encode()).decode() headers["Authorization"] = f"Basic {token}" return headers def chat_send(message, history, conversation_id): payload = {"secret": WEBHOOK_SECRET, "conversation_id": conversation_id, "text": message} r = requests.post(f"{ORCHESTRATOR_URL}/voice-agent", json=payload, headers=_headers()) data = r.json() if r.status_code != 200 or data.get("error"): return history + [[message, data.get("error", "Error")]], conversation_id, "" assistant = data.get("assistant_response_text") or "" convo = data.get("conversation_id") or conversation_id audio_html = "" if data.get("tts_audio_url"): audio_html = f"" return history + [[message, assistant]], convo, audio_html def voice_send(audio_path, history, conversation_id): audio_b64 = None if audio_path: with open(audio_path, "rb") as f: audio_b64 = base64.b64encode(f.read()).decode("utf-8") payload = {"secret": WEBHOOK_SECRET, "conversation_id": conversation_id, "audio": audio_b64} r = requests.post(f"{ORCHESTRATOR_URL}/voice-agent", json=payload, headers=_headers()) data = r.json() if r.status_code != 200 or data.get("error"): return history + [["", data.get("error", "Error")]], conversation_id, "" user_text = data.get("transcript_text", "") assistant = data.get("assistant_response_text") or "" convo = data.get("conversation_id") or conversation_id audio_html = "" if data.get("tts_audio_url"): audio_html = f"" return history + [[user_text, assistant]], convo, audio_html with gr.Blocks() as demo: gr.Markdown("# Voice Agent") chatbox = gr.Chatbot() msg = gr.Textbox(label="Message") send_btn = gr.Button("Send") mic = gr.Audio(sources=["microphone"], type="filepath", label="Talk") voice_btn = gr.Button("Send Voice") audio_out = gr.HTML() conversation_state = gr.State(value=None) send_btn.click(chat_send, [msg, chatbox, conversation_state], [chatbox, conversation_state, audio_out]) voice_btn.click(voice_send, [mic, chatbox, conversation_state], [chatbox, conversation_state, audio_out]) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)