Vivekkrishu commited on
Commit
2c53cdf
·
1 Parent(s): a0aec8b
Files changed (1) hide show
  1. app.py +14 -43
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import gradio as gr
2
  import joblib
3
  from src.preprocess import clean_text
4
- import time
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
 
@@ -12,11 +11,10 @@ responses = joblib.load("models/responses.joblib")
12
  history = []
13
 
14
  def chatbot_response(user_input):
15
- """Generates chat HTML with typing animation for bot."""
16
  if not user_input.strip():
17
  return ""
18
-
19
- # User message
20
  timestamp = datetime.datetime.now().strftime("%H:%M")
21
  history.append({
22
  "sender": "You",
@@ -26,32 +24,12 @@ def chatbot_response(user_input):
26
  "align": "right"
27
  })
28
 
29
- # Bot typing animation
30
- bot_reply = responses.get(model.predict([clean_text(user_input)])[0], ["Sorry, I don't understand."])[0]
31
- typing_html = """
32
- <div style="text-align:left; margin:8px 0;">
33
- <div style="display:inline-block; background:#FFFFFF; padding:10px 15px; border-radius:20px; max-width:70%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-style:italic; color:gray;">
34
- Bot is typing...
35
- </div>
36
- </div>
37
- """
38
- # Render chat with typing bubble
39
- chat_html = render_chat(typing=True)
40
-
41
- # Simulate typing
42
- for i in range(len(bot_reply)+1):
43
- history.append({
44
- "sender": "Bot",
45
- "message": bot_reply[:i],
46
- "time": timestamp,
47
- "color": "#FFFFFF",
48
- "align": "left"
49
- })
50
- chat_html = render_chat()
51
- time.sleep(0.03)
52
- history.pop() # remove last partial message to update in next loop
53
 
54
- # Add final bot message
 
55
  history.append({
56
  "sender": "Bot",
57
  "message": bot_reply,
@@ -59,30 +37,23 @@ def chatbot_response(user_input):
59
  "color": "#FFFFFF",
60
  "align": "left"
61
  })
62
-
 
63
  return render_chat()
64
 
65
- def render_chat(typing=False):
66
  chat_html = """
67
- <div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc">
68
  """
69
  for msg in history:
70
  chat_html += f"""
71
  <div style="text-align:{msg['align']}; margin:8px 0;">
72
- <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);">
73
  {msg['message']}<br>
74
  <span style="font-size:10px; color:gray; float:right;">{msg['time']}</span>
75
  </div>
76
  </div>
77
  """
78
- if typing:
79
- chat_html += """
80
- <div style="text-align:left; margin:8px 0;">
81
- <div style="display:inline-block; background:#FFFFFF; padding:10px 15px; border-radius:20px; max-width:70%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-style:italic; color:gray;">
82
- Bot is typing...
83
- </div>
84
- </div>
85
- """
86
  chat_html += "</div>"
87
  return chat_html
88
 
@@ -92,7 +63,7 @@ demo = gr.Interface(
92
  inputs=gr.Textbox(lines=2, placeholder="Type your message here...", label="Your Message"),
93
  outputs=gr.HTML(label="Chat"),
94
  title="🟢 LMS Chatbot",
95
- description="Ask anything about your LMS. Automatic reply with typing animation!"
96
  )
97
 
98
  if __name__ == "__main__":
 
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
 
 
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",
 
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,
 
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
 
 
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__":