Spaces:
Sleeping
Sleeping
Commit ·
c1e1c68
1
Parent(s): 08313ff
updated
Browse files- src/gui.py +45 -19
src/gui.py
CHANGED
|
@@ -2,41 +2,67 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
| 4 |
from src.preprocess import clean_text
|
| 5 |
-
import
|
| 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 |
clean_input = clean_text(user_input)
|
| 17 |
tag = model.predict([clean_input])[0]
|
| 18 |
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Gradio interface
|
| 34 |
demo = gr.Interface(
|
| 35 |
fn=chatbot_response,
|
| 36 |
-
inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
|
| 37 |
-
outputs=gr.HTML(),
|
| 38 |
-
title="LMS Chatbot",
|
| 39 |
-
description="Ask anything about
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
| 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 |
+
# Conversation history
|
| 12 |
history = []
|
| 13 |
|
| 14 |
def chatbot_response(user_input):
|
| 15 |
+
"""Professional chat UI with bubbles, timestamps, and scrolling."""
|
| 16 |
+
if not user_input.strip():
|
| 17 |
+
return ""
|
| 18 |
+
|
| 19 |
clean_input = clean_text(user_input)
|
| 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,
|
| 29 |
+
"time": timestamp,
|
| 30 |
+
"color": "#DCF8C6",
|
| 31 |
+
"align": "right"
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
# Add bot message
|
| 35 |
+
history.append({
|
| 36 |
+
"sender": "Bot",
|
| 37 |
+
"message": bot_reply,
|
| 38 |
+
"time": timestamp,
|
| 39 |
+
"color": "#FFFFFF",
|
| 40 |
+
"align": "left"
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
# Generate HTML chat
|
| 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 |
+
"""
|
| 47 |
+
for msg in history:
|
| 48 |
+
chat_html += f"""
|
| 49 |
+
<div style="text-align:{msg['align']}; margin:8px 0;">
|
| 50 |
+
<div style="display:inline-block; background:{msg['color']}; padding:10px 15px; border-radius:20px; max-width:70%; box-shadow: 0 2px 5px rgba(0,0,0,0.2);">
|
| 51 |
+
{msg['message']}<br>
|
| 52 |
+
<span style="font-size:10px; color:gray; float:right;">{msg['time']}</span>
|
| 53 |
+
</div>
|
| 54 |
+
</div>
|
| 55 |
+
"""
|
| 56 |
+
chat_html += "</div>"
|
| 57 |
+
return chat_html
|
| 58 |
|
| 59 |
# Gradio interface
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=chatbot_response,
|
| 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 Learning Management System and get instant automated responses. Chat bubbles with timestamps for a modern look."
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|