Spaces:
Sleeping
Sleeping
Commit
·
bb89462
1
Parent(s):
8c22ca2
updated
Browse files
app.py
CHANGED
|
@@ -1,70 +1,120 @@
|
|
| 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 |
-
#
|
| 11 |
-
|
|
|
|
| 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()
|
|
|
|
| 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()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# app.py
|
| 74 |
+
import streamlit as st
|
| 75 |
import joblib
|
| 76 |
from src.preprocess import clean_text
|
| 77 |
import datetime
|
| 78 |
|
| 79 |
+
# Load trained model & responses
|
| 80 |
model = joblib.load("models/lms_chatbot.joblib")
|
| 81 |
responses = joblib.load("models/responses.joblib")
|
| 82 |
|
| 83 |
+
# Initialize session state for messages
|
| 84 |
+
if "messages" not in st.session_state:
|
| 85 |
+
st.session_state.messages = []
|
| 86 |
|
| 87 |
def chatbot_response(user_input):
|
| 88 |
+
"""Predict tag and generate response."""
|
| 89 |
if not user_input.strip():
|
| 90 |
return ""
|
| 91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
tag = model.predict([clean_text(user_input)])[0]
|
| 93 |
bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
|
| 94 |
+
return bot_reply
|
| 95 |
+
|
| 96 |
+
# Streamlit UI
|
| 97 |
+
st.set_page_config(page_title="🟢 LMS Chatbot", page_icon="🤖")
|
| 98 |
+
st.title("🟢 LMS Chatbot")
|
| 99 |
+
st.markdown("Ask anything about your LMS and get automated responses!")
|
| 100 |
+
|
| 101 |
+
# User input
|
| 102 |
+
user_input = st.text_input("Type your message here:", key="input")
|
| 103 |
+
|
| 104 |
+
if user_input:
|
| 105 |
+
# Append user message
|
| 106 |
+
st.session_state.messages.append({"sender": "user", "content": user_input})
|
| 107 |
+
|
| 108 |
+
# Get bot response
|
| 109 |
+
bot_reply = chatbot_response(user_input)
|
| 110 |
+
st.session_state.messages.append({"sender": "bot", "content": bot_reply})
|
| 111 |
+
|
| 112 |
+
# Display chat messages
|
| 113 |
+
for msg in st.session_state.messages:
|
| 114 |
+
if msg["sender"] == "user":
|
| 115 |
+
with st.chat_message("user"):
|
| 116 |
+
st.write(msg["content"])
|
| 117 |
+
else:
|
| 118 |
+
with st.chat_message("assistant"):
|
| 119 |
+
st.write(msg["content"])
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|