Spaces:
Sleeping
Sleeping
File size: 3,623 Bytes
19a534e c929609 19a534e c929609 19a534e c929609 19a534e c929609 19a534e c929609 19a534e c929609 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
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() |