DanielKiani commited on
Commit
1707dde
·
verified ·
1 Parent(s): 6435ecc
Files changed (1) hide show
  1. scripts/app.py +42 -21
scripts/app.py CHANGED
@@ -8,7 +8,7 @@ def generate_audio_response(text):
8
  """
9
  Converts the agent's text response into an audio file using gTTS.
10
  """
11
- if not text:
12
  return None
13
  output_path = "assistant_response.mp3"
14
  tts = gTTS(text=text, lang='en')
@@ -17,57 +17,78 @@ def generate_audio_response(text):
17
 
18
  def respond(text_query, history_state):
19
  """
20
- The main interaction function called by the Gradio interface.
 
21
  """
22
  if not text_query or not text_query.strip():
23
- formatted_history = "\n".join([f"**You:** {turn['user']}\n**Agent:** {turn['assistant']}" for turn in history_state])
 
24
  return "", history_state, formatted_history
25
 
26
- query = text_query.strip()
27
- assistant_response_text = agent.get_rag_response(query, history_state)
28
 
29
- new_history = history_state + [{'user': query, 'assistant': assistant_response_text}]
30
- formatted_history = "\n".join([f"**You:** {turn['user']}\n**Agent:** {turn['assistant']}" for turn in new_history])
31
 
32
- return assistant_response_text, new_history, formatted_history
 
 
 
33
 
34
  # --- Launch the Gradio Web Interface ---
35
- print("Launching Gradio Interface...")
36
 
37
- # Instantiate the agent from agent.py
38
  agent = CustomerServiceAgent()
39
 
40
- # Define the UI layout
41
- with gr.Blocks(theme=gr.themes.Soft(), title="Customer Service Agent") as app:
42
- gr.Markdown("# Advanced Customer Service Agent")
43
- gr.Markdown("Type your query below and press Submit.")
44
 
 
45
  history_state = gr.State([])
46
 
47
  with gr.Row():
48
  with gr.Column(scale=2):
49
- text_input = gr.Textbox(label="Your Question", lines=4, placeholder="Type your question here...")
50
- text_submit_btn = gr.Button("Submit")
 
 
 
 
51
 
52
- with gr.Accordion("Agent's Response", open=True):
53
- agent_response_text = gr.Textbox(label="Response Text", interactive=False, lines=4)
 
 
 
 
54
  with gr.Row():
55
  read_aloud_btn = gr.Button("Read Response Aloud")
56
  audio_output = gr.Audio(label="Agent's Voice", autoplay=False)
57
-
58
  with gr.Column(scale=3):
59
- history_display = gr.Markdown("Conversation history will appear here.", label="Conversation")
 
 
 
60
 
 
61
  text_submit_btn.click(
62
  fn=respond,
63
  inputs=[text_input, history_state],
64
  outputs=[agent_response_text, history_state, history_display]
65
- ).then(lambda: "", outputs=[text_input])
66
 
67
  read_aloud_btn.click(
68
  fn=generate_audio_response,
69
  inputs=[agent_response_text],
70
  outputs=[audio_output]
71
  )
 
 
72
 
 
73
  app.launch(debug=True, share=True)
 
8
  """
9
  Converts the agent's text response into an audio file using gTTS.
10
  """
11
+ if not text or not text.strip():
12
  return None
13
  output_path = "assistant_response.mp3"
14
  tts = gTTS(text=text, lang='en')
 
17
 
18
  def respond(text_query, history_state):
19
  """
20
+ The main interaction function. It calls the agent, updates the history state,
21
+ and formats the history for Markdown display.
22
  """
23
  if not text_query or not text_query.strip():
24
+ # If input is empty, just return the current state without changes
25
+ formatted_history = "\n---\n".join([f"**You:** {turn['user']}\n\n**Agent:** {turn['assistant']}" for turn in history_state])
26
  return "", history_state, formatted_history
27
 
28
+ # Get the response from the from-scratch agent, passing the query and history
29
+ assistant_response = agent.get_rag_response(text_query, history_state)
30
 
31
+ # Append the new turn to the history state (list of dictionaries)
32
+ new_history = history_state + [{'user': text_query, 'assistant': assistant_response}]
33
 
34
+ # Format the entire history for display in the Markdown component
35
+ formatted_history = "\n---\n".join([f"**You:** {turn['user']}\n\n**Agent:** {turn['assistant']}" for turn in new_history])
36
+
37
+ return assistant_response, new_history, formatted_history
38
 
39
  # --- Launch the Gradio Web Interface ---
40
+ print("Launching Gradio Interface for IMPROVED From-Scratch Agent...")
41
 
42
+ # Instantiate the agent from agent_scratch_improved.py
43
  agent = CustomerServiceAgent()
44
 
45
+ # Define the UI layout using Gradio Blocks for more control
46
+ with gr.Blocks(theme=gr.themes.Soft(), title="Advanced Customer Service Agent") as app:
47
+ gr.Markdown("# 🤖 Advanced Customer Service Agent (From Scratch - Improved)")
48
+ gr.Markdown("Type your query in the text box and press the 'Submit' button.")
49
 
50
+ # State to hold the conversation history as a list of dictionaries
51
  history_state = gr.State([])
52
 
53
  with gr.Row():
54
  with gr.Column(scale=2):
55
+ text_input = gr.Textbox(
56
+ label="Your Question",
57
+ lines=4,
58
+ placeholder="e.g., 'My package hasn't arrived yet.'"
59
+ )
60
+ text_submit_btn = gr.Button("Submit", variant="primary")
61
 
62
+ with gr.Accordion("Agent's Latest Response", open=True):
63
+ agent_response_text = gr.Textbox(
64
+ label="Response Text",
65
+ interactive=False,
66
+ lines=4
67
+ )
68
  with gr.Row():
69
  read_aloud_btn = gr.Button("Read Response Aloud")
70
  audio_output = gr.Audio(label="Agent's Voice", autoplay=False)
71
+
72
  with gr.Column(scale=3):
73
+ history_display = gr.Markdown(
74
+ "Conversation history will appear here.",
75
+ label="Full Conversation"
76
+ )
77
 
78
+ # Connect the UI components to the functions
79
  text_submit_btn.click(
80
  fn=respond,
81
  inputs=[text_input, history_state],
82
  outputs=[agent_response_text, history_state, history_display]
83
+ ).then(lambda: "", outputs=[text_input]) # Clears the input box after submit
84
 
85
  read_aloud_btn.click(
86
  fn=generate_audio_response,
87
  inputs=[agent_response_text],
88
  outputs=[audio_output]
89
  )
90
+
91
+ gr.Markdown("--- \n *Project based on the roadmap by Gemini.*")
92
 
93
+ # Launch the app with a public share link
94
  app.launch(debug=True, share=True)