Vivekkrishu commited on
Commit
4fc7857
·
1 Parent(s): 64c2d0c
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -2,18 +2,19 @@ import gradio as gr
2
  import joblib
3
  from src.preprocess import clean_text
4
  import datetime
5
- import time
6
 
7
  # Load model & responses
8
  model = joblib.load("models/lms_chatbot.joblib")
9
  responses = joblib.load("models/responses.joblib")
10
 
 
11
  history = []
12
 
13
  def chatbot_response(user_input):
14
  if not user_input.strip():
15
  return ""
16
 
 
17
  timestamp = datetime.datetime.now().strftime("%H:%M")
18
  history.append({
19
  "sender": "You",
@@ -27,14 +28,7 @@ def chatbot_response(user_input):
27
  tag = model.predict([clean_text(user_input)])[0]
28
  bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
29
 
30
- # Animate bot typing
31
- display = render_chat(typing=True)
32
- for i in range(len(bot_reply)+1):
33
- temp_msg = bot_reply[:i]
34
- display = render_chat(temp_msg)
35
- time.sleep(0.02)
36
-
37
- # Add final bot message
38
  timestamp = datetime.datetime.now().strftime("%H:%M")
39
  history.append({
40
  "sender": "Bot",
@@ -44,10 +38,13 @@ def chatbot_response(user_input):
44
  "align": "left"
45
  })
46
 
 
47
  return render_chat()
48
 
49
- def render_chat(temp_bot_msg=""):
50
- chat_html = '<div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc;">'
 
 
51
  for msg in history:
52
  chat_html += f"""
53
  <div style="text-align:{msg['align']}; margin:8px 0;">
@@ -57,23 +54,16 @@ def render_chat(temp_bot_msg=""):
57
  </div>
58
  </div>
59
  """
60
- if temp_bot_msg:
61
- chat_html += f"""
62
- <div style="text-align:left; margin:8px 0;">
63
- <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;">
64
- {temp_bot_msg}
65
- </div>
66
- </div>
67
- """
68
  chat_html += "</div>"
69
  return chat_html
70
 
 
71
  demo = gr.Interface(
72
  fn=chatbot_response,
73
- inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
74
- outputs=gr.HTML(),
75
  title="🟢 LMS Chatbot",
76
- description="Ask anything about your LMS. Automatic reply with chat bubbles!"
77
  )
78
 
79
  if __name__ == "__main__":
 
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",
 
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",
 
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;">
 
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__":