Spaces:
Sleeping
Sleeping
Commit
·
fc1847d
1
Parent(s):
c3f8261
updated
Browse files
app.py
CHANGED
|
@@ -1,50 +1,70 @@
|
|
| 1 |
-
import
|
| 2 |
import joblib
|
| 3 |
from src.preprocess import clean_text
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
model = joblib.load("models/lms_chatbot.joblib")
|
| 8 |
responses = joblib.load("models/responses.joblib")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
st.session_state.messages = []
|
| 13 |
|
| 14 |
-
st.set_page_config(page_title="🟢 LMS Chatbot", page_icon="🤖")
|
| 15 |
-
st.title("🟢 LMS Chatbot")
|
| 16 |
-
st.markdown("Ask anything about your LMS and get automated responses!")
|
| 17 |
-
|
| 18 |
-
# Function to generate bot response
|
| 19 |
def chatbot_response(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
tag = model.predict([clean_text(user_input)])[0]
|
| 21 |
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
from src.preprocess import clean_text
|
| 4 |
+
import datetime
|
| 5 |
|
| 6 |
+
# Load model & responses
|
| 7 |
model = joblib.load("models/lms_chatbot.joblib")
|
| 8 |
responses = joblib.load("models/responses.joblib")
|
| 9 |
|
| 10 |
+
# Keep conversation history
|
| 11 |
+
history = []
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def chatbot_response(user_input):
|
| 14 |
+
if not user_input.strip():
|
| 15 |
+
return ""
|
| 16 |
+
|
| 17 |
+
# Add user message
|
| 18 |
+
timestamp = datetime.datetime.now().strftime("%H:%M")
|
| 19 |
+
history.append({
|
| 20 |
+
"sender": "You",
|
| 21 |
+
"message": user_input,
|
| 22 |
+
"time": timestamp,
|
| 23 |
+
"color": "#DCF8C6",
|
| 24 |
+
"align": "right"
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
+
# Bot prediction
|
| 28 |
tag = model.predict([clean_text(user_input)])[0]
|
| 29 |
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 30 |
+
|
| 31 |
+
# Add bot message
|
| 32 |
+
timestamp = datetime.datetime.now().strftime("%H:%M")
|
| 33 |
+
history.append({
|
| 34 |
+
"sender": "Bot",
|
| 35 |
+
"message": bot_reply,
|
| 36 |
+
"time": timestamp,
|
| 37 |
+
"color": "#FFFFFF",
|
| 38 |
+
"align": "left"
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
# Render chat
|
| 42 |
+
return render_chat()
|
| 43 |
+
|
| 44 |
+
def render_chat():
|
| 45 |
+
chat_html = """
|
| 46 |
+
<div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc;">
|
| 47 |
+
"""
|
| 48 |
+
for msg in history:
|
| 49 |
+
chat_html += f"""
|
| 50 |
+
<div style="text-align:{msg['align']}; margin:8px 0;">
|
| 51 |
+
<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);">
|
| 52 |
+
{msg['message']}<br>
|
| 53 |
+
<span style="font-size:10px; color:gray; float:right;">{msg['time']}</span>
|
| 54 |
+
</div>
|
| 55 |
+
</div>
|
| 56 |
+
"""
|
| 57 |
+
chat_html += "</div>"
|
| 58 |
+
return chat_html
|
| 59 |
+
|
| 60 |
+
# Gradio interface
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=chatbot_response,
|
| 63 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message here...", label="Your Message"),
|
| 64 |
+
outputs=gr.HTML(label="Chat"),
|
| 65 |
+
title="🟢 LMS Chatbot",
|
| 66 |
+
description="Ask anything about your LMS. Automatic reply with chat bubbles and timestamps!"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|