Vivekkrishu commited on
Commit
c1e1c68
·
1 Parent(s): 08313ff
Files changed (1) hide show
  1. 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 time
6
 
7
  # Load model and responses
8
  model = joblib.load("models/lms_chatbot.joblib")
9
  responses = joblib.load("models/responses.joblib")
10
 
11
- # Keep chat history
12
  history = []
13
 
14
  def chatbot_response(user_input):
15
- """Simulate Tkinter-style chat UI with bubbles."""
 
 
 
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
- # Add user and bot messages to history
21
- history.append(("You", user_input))
22
- history.append(("Bot", bot_reply))
23
-
24
- # Format messages like chat bubbles
25
- formatted_history = ""
26
- for sender, msg in history:
27
- if sender == "You":
28
- formatted_history += f"<div style='background-color:#DCF8C6; padding:8px; border-radius:10px; margin:4px 0; width:fit-content; float:right; clear:both'>{msg}</div>"
29
- else:
30
- formatted_history += f"<div style='background-color:#FFFFFF; padding:8px; border-radius:10px; margin:4px 0; width:fit-content; float:left; clear:both'>{msg}</div>"
31
- return formatted_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 LMS and get automated responses."
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__":