Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from elevenlabs.client import ElevenLabs | |
| from elevenlabs.conversational_ai.conversation import ( | |
| Conversation, | |
| ConversationInitiationData | |
| ) | |
| from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface | |
| # ---------------- CONFIG ---------------- | |
| AGENT_ID = "agent_0101kgh0sk0cfajtgsbnk6j2pgxk" | |
| API_KEY = "sk_f5eb12d622af1ca1e65615cc112bbac35470a3cb1d96a34e" | |
| # --------------------------------------- | |
| class ConversationManager: | |
| def __init__(self): | |
| self.conversation = None | |
| self.chat_history = [] | |
| def append_message(self, role, content): | |
| """Thread-safe message appender - Gradio 6.x format""" | |
| self.chat_history.append({ | |
| "role": role, | |
| "content": content | |
| }) | |
| def start(self, customer_name): | |
| if self.conversation: | |
| return self.chat_history, "β οΈ Call already active" | |
| self.chat_history = [] | |
| elevenlabs = ElevenLabs(api_key=API_KEY) | |
| init_data = ConversationInitiationData( | |
| dynamic_variables={"customer_name": customer_name} | |
| ) | |
| self.conversation = Conversation( | |
| elevenlabs, | |
| AGENT_ID, | |
| config=init_data, | |
| requires_auth=True, | |
| audio_interface=DefaultAudioInterface(), | |
| callback_user_transcript=lambda t: self.append_message("user", t), | |
| callback_agent_response=lambda r: self.append_message("assistant", r), | |
| callback_agent_response_correction=lambda o, c: self.append_message( | |
| "assistant", f"~~{o}~~ β {c}" | |
| ), | |
| ) | |
| self.conversation.start_session() | |
| return self.chat_history, "π’ Call started" | |
| def stop(self): | |
| if not self.conversation: | |
| return self.chat_history, "β οΈ No active call" | |
| self.conversation.end_session() | |
| self.conversation = None | |
| return self.chat_history, "π΄ Call ended" | |
| def clear(self): | |
| """Clear chat history""" | |
| self.chat_history = [] | |
| return self.chat_history, "ποΈ Conversation cleared" | |
| def get_updates(self): | |
| return self.chat_history | |
| # Global manager | |
| manager = ConversationManager() | |
| # ---------------- GRADIO UI ---------------- | |
| with gr.Blocks(title="Voice Call Agent", fill_height=True) as demo: | |
| gr.Markdown("# π Voice Call Agent") | |
| gr.Markdown("Powered by ElevenLabs Conversational AI") | |
| with gr.Row(): | |
| customer_name = gr.Textbox( | |
| label="Customer Name", | |
| value="Govind", | |
| scale=3 | |
| ) | |
| start_btn = gr.Button("π Start Call", scale=1, variant="primary") | |
| stop_btn = gr.Button("π΄ End Call", scale=1, variant="stop") | |
| clear_btn = gr.Button("ποΈ Clear", scale=1) | |
| status = gr.Textbox(label="Status", interactive=False) | |
| # Chatbot takes full remaining height | |
| chatbot = gr.Chatbot( | |
| label="Conversation Transcript", | |
| height=600, | |
| scale=1 | |
| ) | |
| # Event handlers | |
| start_btn.click( | |
| fn=manager.start, | |
| inputs=[customer_name], | |
| outputs=[chatbot, status] | |
| ) | |
| stop_btn.click( | |
| fn=manager.stop, | |
| outputs=[chatbot, status] | |
| ) | |
| clear_btn.click( | |
| fn=manager.clear, | |
| outputs=[chatbot, status] | |
| ) | |
| # β AUTO-REFRESH with Gradio 6.x Timer | |
| timer = gr.Timer(0.5) | |
| timer.tick( | |
| fn=manager.get_updates, | |
| outputs=[chatbot] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |