Spaces:
Sleeping
Sleeping
File size: 3,644 Bytes
49f9f52 ff8ae15 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 49f9f52 1707dde 6d3ae5e 49f9f52 1707dde 49f9f52 |
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 |
import gradio as gr
from gtts import gTTS
from agent import CustomerServiceAgent
# --- Gradio UI Functions ---
def generate_audio_response(text):
"""
Converts the agent's text response into an audio file using gTTS.
"""
if not text or not text.strip():
return None
output_path = "assistant_response.mp3"
tts = gTTS(text=text, lang='en')
tts.save(output_path)
return output_path
def respond(text_query, history_state):
"""
The main interaction function. It calls the agent, updates the history state,
and formats the history for Markdown display.
"""
if not text_query or not text_query.strip():
# If input is empty, just return the current state without changes
formatted_history = "\n---\n".join([f"**You:** {turn['user']}\n\n**Agent:** {turn['assistant']}" for turn in history_state])
return "", history_state, formatted_history
# Get the response from the from-scratch agent, passing the query and history
assistant_response = agent.get_rag_response(text_query, history_state)
# Append the new turn to the history state (list of dictionaries)
new_history = history_state + [{'user': text_query, 'assistant': assistant_response}]
# Format the entire history for display in the Markdown component
formatted_history = "\n---\n".join([f"**You:** {turn['user']}\n\n**Agent:** {turn['assistant']}" for turn in new_history])
return assistant_response, new_history, formatted_history
# --- Launch the Gradio Web Interface ---
print("Launching Gradio Interface for IMPROVED From-Scratch Agent...")
# Instantiate the agent from agent_scratch_improved.py
agent = CustomerServiceAgent()
# Define the UI layout using Gradio Blocks for more control
with gr.Blocks(theme=gr.themes.Soft(), title="Advanced Customer Service Agent") as app:
gr.Markdown("# 🤖 Advanced Customer Service Agent (From Scratch - Improved)")
gr.Markdown("Type your query in the text box and press the 'Submit' button.")
# State to hold the conversation history as a list of dictionaries
history_state = gr.State([])
with gr.Row():
with gr.Column(scale=2):
text_input = gr.Textbox(
label="Your Question",
lines=4,
placeholder="e.g., 'My package hasn't arrived yet.'"
)
text_submit_btn = gr.Button("Submit", variant="primary")
with gr.Accordion("Agent's Latest Response", open=True):
agent_response_text = gr.Textbox(
label="Response Text",
interactive=False,
lines=4
)
with gr.Row():
read_aloud_btn = gr.Button("Read Response Aloud")
audio_output = gr.Audio(label="Agent's Voice", autoplay=False)
with gr.Column(scale=3):
history_display = gr.Markdown(
"Conversation history will appear here.",
label="Full Conversation"
)
# Connect the UI components to the functions
text_submit_btn.click(
fn=respond,
inputs=[text_input, history_state],
outputs=[agent_response_text, history_state, history_display]
).then(lambda: "", outputs=[text_input]) # Clears the input box after submit
read_aloud_btn.click(
fn=generate_audio_response,
inputs=[agent_response_text],
outputs=[audio_output]
)
gr.Markdown("--- \n *Project by Daniel Kiani*")
# Launch the app with a public share link
app.launch(debug=True, share=True) |