Vivekkrishu commited on
Commit
fc1847d
·
1 Parent(s): c3f8261
Files changed (1) hide show
  1. app.py +60 -40
app.py CHANGED
@@ -1,50 +1,70 @@
1
- import streamlit as st
2
  import joblib
3
  from src.preprocess import clean_text
4
- import time
5
 
6
- # Load trained model & responses
7
  model = joblib.load("models/lms_chatbot.joblib")
8
  responses = joblib.load("models/responses.joblib")
9
 
10
- # Initialize session state
11
- if "messages" not in st.session_state:
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
- return bot_reply
23
-
24
- # User input
25
- user_input = st.text_input("Type your message here:", key="input")
26
-
27
- if user_input:
28
- # Append user message
29
- st.session_state.messages.append({"sender": "user", "content": user_input})
30
-
31
- # Typing animation for bot
32
- bot_reply = chatbot_response(user_input)
33
- display_text = ""
34
- st.session_state.messages.append({"sender": "bot", "content": ""}) # placeholder
35
-
36
- bot_index = len(st.session_state.messages) - 1
37
- for char in bot_reply:
38
- display_text += char
39
- st.session_state.messages[bot_index]["content"] = display_text
40
- time.sleep(0.03) # adjust typing speed
41
- st.experimental_rerun() # refresh chat to show animation
42
-
43
- # Display chat messages
44
- for msg in st.session_state.messages:
45
- if msg["sender"] == "user":
46
- with st.chat_message("user"):
47
- st.write(msg["content"])
48
- else:
49
- with st.chat_message("assistant"):
50
- st.write(msg["content"])
 
 
 
 
 
 
 
 
 
 
 
 
 
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()