Tharindu1527 commited on
Commit
d7b8268
ยท
verified ยท
1 Parent(s): 9580530

Update Chatbot.py

Browse files
Files changed (1) hide show
  1. Chatbot.py +50 -50
Chatbot.py CHANGED
@@ -138,28 +138,35 @@ class ChatInterface:
138
  def get_chat_history(self, user_id):
139
  return MongoDBChatMessageHistory(
140
  session_id=user_id,
141
- connection_string= os.getenv("Mongo_URI"),
142
  database_name="Chatbot",
143
  collection_name="chat_histories",
144
  )
145
- def chat(self, message, history, user_id):
146
- if not user_id:
147
- return "Please enter a user ID First.", history,
148
-
149
- config = {"configurable": {"thread_id": user_id}}
150
- chat_history = self.get_chat_history(user_id)
151
 
152
- response = self.model.invoke(message, config, chat_history)
153
- return response, history + [(message, response)]
154
-
 
 
 
 
 
 
 
 
 
 
155
  def reset_chat(self, user_id):
156
- if user_id:
 
 
157
  chat_history = self.get_chat_history(user_id)
158
  chat_history.clear()
159
- return f"Chat history cleared for user {user_id}"
160
- return "Please enter a user ID first"
 
161
 
162
- # Create the Gradio Interface
163
  def create_gradio_interface():
164
  chat_interface = ChatInterface()
165
 
@@ -190,6 +197,7 @@ def create_gradio_interface():
190
  Welcome to your personal AI assistant! Enter your ID and start chatting.
191
  """
192
  )
 
193
  with gr.Row():
194
  with gr.Column(scale=3):
195
  user_id = gr.Textbox(
@@ -208,50 +216,42 @@ def create_gradio_interface():
208
  chatbot = gr.Chatbot(
209
  label="๐Ÿ’ฌ Chat History",
210
  height=500,
211
- bubble_full_width= False,
212
- show_label= True,
213
- elem_id = "chatbot"
214
  )
215
 
216
  with gr.Row():
217
- with gr.Column(scale=4):
218
- msg = gr.Textbox(
219
- label="โœ๏ธ Message",
220
- placeholder="Type your message Here...",
221
- show_label = True,
222
- elem_id = "message"
223
- )
224
- with gr.Column(scale = 1):
225
- submit = gr.Button("๐Ÿš€ Send", variant="primary")
226
 
227
- with gr.Row():
228
- clear = gr.Button("๐Ÿ—‘๏ธ Clear Chat", variant="secondary")
229
-
230
- # Status message for feedback
231
  status_msg = gr.Markdown("")
232
 
233
- # Event Handler
234
- msg_handler = submit.click(
235
- chat_interface.chat,
236
- inputs=[msg, chatbot, user_id, language],
237
- outputs=[msg, chatbot, status_msg],
238
- api_name = "chat"
239
- )
240
 
241
- msg.submit(
242
- chat_interface.chat,
243
- inputs=[msg, chatbot, user_id, language],
244
- outputs=[msg, chatbot, status_msg],
245
- api_name="chat_submit"
246
- )
247
 
248
- clear.click(
249
- chat_interface.reset_chat,
250
- inputs = [user_id],
251
- outputs=[chatbot],
252
- api_name= "clear"
253
- )
254
-
255
  return demo
256
 
257
  if __name__ == "__main__":
 
138
  def get_chat_history(self, user_id):
139
  return MongoDBChatMessageHistory(
140
  session_id=user_id,
141
+ connection_string=os.getenv("Mongo_URI"),
142
  database_name="Chatbot",
143
  collection_name="chat_histories",
144
  )
 
 
 
 
 
 
145
 
146
+ def chat(self, message, history, user_id, language="English"):
147
+ try:
148
+ if not user_id:
149
+ return "", history, "Please enter a user ID first."
150
+
151
+ config = {"configurable": {"thread_id": user_id}}
152
+ chat_history = self.get_chat_history(user_id)
153
+
154
+ response = self.model.invoke(message, config, chat_history)
155
+ return "", history + [(message, response)], ""
156
+ except Exception as e:
157
+ return "", history, f"Error: {str(e)}"
158
+
159
  def reset_chat(self, user_id):
160
+ try:
161
+ if not user_id:
162
+ return None, "Please enter a user ID first"
163
  chat_history = self.get_chat_history(user_id)
164
  chat_history.clear()
165
+ return None, f"Chat history cleared for user {user_id}"
166
+ except Exception as e:
167
+ return None, f"Error clearing chat: {str(e)}"
168
 
169
+ # Create the Gradio Interface
170
  def create_gradio_interface():
171
  chat_interface = ChatInterface()
172
 
 
197
  Welcome to your personal AI assistant! Enter your ID and start chatting.
198
  """
199
  )
200
+
201
  with gr.Row():
202
  with gr.Column(scale=3):
203
  user_id = gr.Textbox(
 
216
  chatbot = gr.Chatbot(
217
  label="๐Ÿ’ฌ Chat History",
218
  height=500,
219
+ bubble_full_width=False,
220
+ show_label=True,
221
+ elem_id="chatbot"
222
  )
223
 
224
  with gr.Row():
225
+ msg = gr.Textbox(
226
+ label="โœ๏ธ Message",
227
+ placeholder="Type your message here...",
228
+ show_label=True,
229
+ elem_id="message"
230
+ )
231
+ submit = gr.Button("๐Ÿš€ Send", variant="primary")
 
 
232
 
233
+ clear = gr.Button("๐Ÿ—‘๏ธ Clear Chat", variant="secondary")
 
 
 
234
  status_msg = gr.Markdown("")
235
 
236
+ # Event handlers
237
+ submit.click(
238
+ fn=chat_interface.chat,
239
+ inputs=[msg, chatbot, user_id, language],
240
+ outputs=[msg, chatbot, status_msg]
241
+ )
 
242
 
243
+ msg.submit(
244
+ fn=chat_interface.chat,
245
+ inputs=[msg, chatbot, user_id, language],
246
+ outputs=[msg, chatbot, status_msg]
247
+ )
 
248
 
249
+ clear.click(
250
+ fn=chat_interface.reset_chat,
251
+ inputs=[user_id],
252
+ outputs=[chatbot, status_msg]
253
+ )
254
+
 
255
  return demo
256
 
257
  if __name__ == "__main__":