AashitaK commited on
Commit
a987774
·
verified ·
1 Parent(s): f5571bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -21,21 +21,24 @@ response_manager = ResponseManager(vector_store_id)
21
  # Define the chatbot function to handle user queries and generate responses
22
  def chat_interaction(query: str) -> str:
23
  """
24
- Function to handle the chatbot interaction.
25
  :param query: The user query to respond to.
26
- :return: The response text from the chatbot.
 
27
  """
28
  try:
29
  if query.strip():
30
  response = response_manager.create_response(query, model, temperature, max_output_tokens, max_num_results)
31
  if not response:
32
- return "Sorry, I couldn't generate a response at this time. Please try again later."
33
- # Return the response from the AI model
34
- return response
35
  else:
36
- return "Please enter a valid query."
 
37
  except Exception as e:
38
- return str(e)
 
39
 
40
  # Set parameters for the response generation
41
  model = "gpt-4o-mini" # Set the model to be used for response generation
@@ -68,43 +71,35 @@ if not all([chatbot_title, chatbot_description,
68
 
69
  # Define the reset function
70
  def reset_output():
71
- return chatbot_output_placeholder
72
 
73
  # Create a Gradio Blocks interface
74
  with gr.Blocks() as demo:
75
  gr.Markdown(f"## {chatbot_title}\n{chatbot_description}")
76
 
77
- with gr.Row():
78
- user_input = gr.Textbox(
79
- lines=7,
80
- label=chatbot_input_label,
81
- placeholder=chatbot_input_placeholder
82
- )
83
-
84
- with gr.Row():
85
- # Output box always visible with initial placeholder
86
- output = gr.Textbox(
87
- lines=7,
88
- label=chatbot_output_label,
89
- placeholder=chatbot_output_placeholder,
90
- interactive=False
91
- )
92
 
93
- # Place Reset on the Left and Submit on the Right
94
  with gr.Row():
95
- # Left Column for Reset
96
  with gr.Column(scale=1):
97
  reset = gr.Button(chatbot_reset_button, variant="secondary")
98
- # Right Column for Submit
99
  with gr.Column(scale=1):
100
  submit = gr.Button(chatbot_submit_button, variant="primary")
101
-
102
- # Define button click actions
103
- submit.click(fn=chat_interaction, inputs=user_input, outputs=output)
104
- reset.click(fn=reset_output, inputs=None, outputs=output)
105
 
106
- # Enable "Enter" key to submit
107
- user_input.submit(fn=chat_interaction, inputs=user_input, outputs=output)
 
 
108
 
109
  if __name__ == "__main__":
110
  demo.launch()
 
21
  # Define the chatbot function to handle user queries and generate responses
22
  def chat_interaction(query: str) -> str:
23
  """
24
+ Function to handle the chatbot interaction and maintain conversation history.
25
  :param query: The user query to respond to.
26
+ :param history: The conversation history (list of [input, output] pairs).
27
+ :return: Updated conversation history.
28
  """
29
  try:
30
  if query.strip():
31
  response = response_manager.create_response(query, model, temperature, max_output_tokens, max_num_results)
32
  if not response:
33
+ response = "Sorry, I couldn't generate a response at this time. Please try again later."
34
+ # Append the conversation
35
+ history.append((query, response))
36
  else:
37
+ history.append((query, "Please enter a valid query."))
38
+ return history
39
  except Exception as e:
40
+ history.append((query, str(e)))
41
+ return history
42
 
43
  # Set parameters for the response generation
44
  model = "gpt-4o-mini" # Set the model to be used for response generation
 
71
 
72
  # Define the reset function
73
  def reset_output():
74
+ return []
75
 
76
  # Create a Gradio Blocks interface
77
  with gr.Blocks() as demo:
78
  gr.Markdown(f"## {chatbot_title}\n{chatbot_description}")
79
 
80
+ # Chatbot history component
81
+ chatbot_output = gr.Chatbot(label="Chat History")
82
+
83
+ # User input
84
+ user_input = gr.Textbox(
85
+ lines=2,
86
+ label=chatbot_input_label,
87
+ placeholder=chatbot_input_placeholder
88
+ )
 
 
 
 
 
 
89
 
90
+ # Buttons
91
  with gr.Row():
92
+ # Reset button on the left (Gray)
93
  with gr.Column(scale=1):
94
  reset = gr.Button(chatbot_reset_button, variant="secondary")
95
+ # Submit button on the right (Blue)
96
  with gr.Column(scale=1):
97
  submit = gr.Button(chatbot_submit_button, variant="primary")
 
 
 
 
98
 
99
+ # Submit and Reset button actions
100
+ submit.click(fn=chatbot, inputs=[user_input, chatbot_output], outputs=chatbot_output)
101
+ user_input.submit(fn=chatbot, inputs=[user_input, chatbot_output], outputs=chatbot_output)
102
+ reset.click(fn=reset_output, inputs=None, outputs=chatbot_output)
103
 
104
  if __name__ == "__main__":
105
  demo.launch()