Spaces:
Sleeping
Sleeping
Commit
·
8c65a18
1
Parent(s):
c1e1c68
updated
Browse files- src/gui.py +42 -12
src/gui.py
CHANGED
|
@@ -1,28 +1,23 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
| 4 |
from src.preprocess import clean_text
|
|
|
|
| 5 |
import datetime
|
| 6 |
|
| 7 |
# Load model and responses
|
| 8 |
model = joblib.load("models/lms_chatbot.joblib")
|
| 9 |
responses = joblib.load("models/responses.joblib")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
history = []
|
| 13 |
|
| 14 |
def chatbot_response(user_input):
|
| 15 |
-
"""
|
| 16 |
if not user_input.strip():
|
| 17 |
return ""
|
| 18 |
|
| 19 |
-
|
| 20 |
-
tag = model.predict([clean_input])[0]
|
| 21 |
-
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 22 |
-
|
| 23 |
timestamp = datetime.datetime.now().strftime("%H:%M")
|
| 24 |
-
|
| 25 |
-
# Add user message
|
| 26 |
history.append({
|
| 27 |
"sender": "You",
|
| 28 |
"message": user_input,
|
|
@@ -30,8 +25,33 @@ def chatbot_response(user_input):
|
|
| 30 |
"color": "#DCF8C6",
|
| 31 |
"align": "right"
|
| 32 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
history.append({
|
| 36 |
"sender": "Bot",
|
| 37 |
"message": bot_reply,
|
|
@@ -40,7 +60,9 @@ def chatbot_response(user_input):
|
|
| 40 |
"align": "left"
|
| 41 |
})
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
chat_html = """
|
| 45 |
<div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc">
|
| 46 |
"""
|
|
@@ -53,6 +75,14 @@ def chatbot_response(user_input):
|
|
| 53 |
</div>
|
| 54 |
</div>
|
| 55 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
chat_html += "</div>"
|
| 57 |
return chat_html
|
| 58 |
|
|
@@ -62,7 +92,7 @@ demo = gr.Interface(
|
|
| 62 |
inputs=gr.Textbox(lines=2, placeholder="Type your message here...", label="Your Message"),
|
| 63 |
outputs=gr.HTML(label="Chat"),
|
| 64 |
title="🟢 LMS Chatbot",
|
| 65 |
-
description="Ask anything about your
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
from src.preprocess import clean_text
|
| 4 |
+
import time
|
| 5 |
import datetime
|
| 6 |
|
| 7 |
# Load model and responses
|
| 8 |
model = joblib.load("models/lms_chatbot.joblib")
|
| 9 |
responses = joblib.load("models/responses.joblib")
|
| 10 |
|
| 11 |
+
# Keep conversation history
|
| 12 |
history = []
|
| 13 |
|
| 14 |
def chatbot_response(user_input):
|
| 15 |
+
"""Generates chat HTML with typing animation for bot."""
|
| 16 |
if not user_input.strip():
|
| 17 |
return ""
|
| 18 |
|
| 19 |
+
# User message
|
|
|
|
|
|
|
|
|
|
| 20 |
timestamp = datetime.datetime.now().strftime("%H:%M")
|
|
|
|
|
|
|
| 21 |
history.append({
|
| 22 |
"sender": "You",
|
| 23 |
"message": user_input,
|
|
|
|
| 25 |
"color": "#DCF8C6",
|
| 26 |
"align": "right"
|
| 27 |
})
|
| 28 |
+
|
| 29 |
+
# Bot typing animation
|
| 30 |
+
bot_reply = responses.get(model.predict([clean_text(user_input)])[0], ["Sorry, I don't understand."])[0]
|
| 31 |
+
typing_html = """
|
| 32 |
+
<div style="text-align:left; margin:8px 0;">
|
| 33 |
+
<div style="display:inline-block; background:#FFFFFF; padding:10px 15px; border-radius:20px; max-width:70%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-style:italic; color:gray;">
|
| 34 |
+
Bot is typing...
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
"""
|
| 38 |
+
# Render chat with typing bubble
|
| 39 |
+
chat_html = render_chat(typing=True)
|
| 40 |
|
| 41 |
+
# Simulate typing
|
| 42 |
+
for i in range(len(bot_reply)+1):
|
| 43 |
+
history.append({
|
| 44 |
+
"sender": "Bot",
|
| 45 |
+
"message": bot_reply[:i],
|
| 46 |
+
"time": timestamp,
|
| 47 |
+
"color": "#FFFFFF",
|
| 48 |
+
"align": "left"
|
| 49 |
+
})
|
| 50 |
+
chat_html = render_chat()
|
| 51 |
+
time.sleep(0.03)
|
| 52 |
+
history.pop() # remove last partial message to update in next loop
|
| 53 |
+
|
| 54 |
+
# Add final bot message
|
| 55 |
history.append({
|
| 56 |
"sender": "Bot",
|
| 57 |
"message": bot_reply,
|
|
|
|
| 60 |
"align": "left"
|
| 61 |
})
|
| 62 |
|
| 63 |
+
return render_chat()
|
| 64 |
+
|
| 65 |
+
def render_chat(typing=False):
|
| 66 |
chat_html = """
|
| 67 |
<div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc">
|
| 68 |
"""
|
|
|
|
| 75 |
</div>
|
| 76 |
</div>
|
| 77 |
"""
|
| 78 |
+
if typing:
|
| 79 |
+
chat_html += """
|
| 80 |
+
<div style="text-align:left; margin:8px 0;">
|
| 81 |
+
<div style="display:inline-block; background:#FFFFFF; padding:10px 15px; border-radius:20px; max-width:70%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-style:italic; color:gray;">
|
| 82 |
+
Bot is typing...
|
| 83 |
+
</div>
|
| 84 |
+
</div>
|
| 85 |
+
"""
|
| 86 |
chat_html += "</div>"
|
| 87 |
return chat_html
|
| 88 |
|
|
|
|
| 92 |
inputs=gr.Textbox(lines=2, placeholder="Type your message here...", label="Your Message"),
|
| 93 |
outputs=gr.HTML(label="Chat"),
|
| 94 |
title="🟢 LMS Chatbot",
|
| 95 |
+
description="Ask anything about your LMS. Automatic reply with typing animation!"
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|