Spaces:
Sleeping
Sleeping
Update Chatbot.py
Browse files- 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=
|
| 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 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
def reset_chat(self, user_id):
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
chat_history = self.get_chat_history(user_id)
|
| 158 |
chat_history.clear()
|
| 159 |
-
return f"Chat history cleared for user {user_id}"
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
-
|
| 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=
|
| 212 |
-
show_label=
|
| 213 |
-
elem_id
|
| 214 |
)
|
| 215 |
|
| 216 |
with gr.Row():
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
with gr.Column(scale = 1):
|
| 225 |
-
submit = gr.Button("๐ Send", variant="primary")
|
| 226 |
|
| 227 |
-
|
| 228 |
-
clear = gr.Button("๐๏ธ Clear Chat", variant="secondary")
|
| 229 |
-
|
| 230 |
-
# Status message for feedback
|
| 231 |
status_msg = gr.Markdown("")
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
)
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
)
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 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__":
|