Vivekkrishu commited on
Commit
a0aec8b
·
1 Parent(s): 8c65a18
Files changed (1) hide show
  1. app.py +78 -17
app.py CHANGED
@@ -1,9 +1,10 @@
1
- # app.py
2
  import gradio as gr
3
  import joblib
4
  from src.preprocess import clean_text
 
 
5
 
6
- # Load trained model and responses
7
  model = joblib.load("models/lms_chatbot.joblib")
8
  responses = joblib.load("models/responses.joblib")
9
 
@@ -11,27 +12,87 @@ responses = joblib.load("models/responses.joblib")
11
  history = []
12
 
13
  def chatbot_response(user_input):
14
- clean_input = clean_text(user_input)
15
- tag = model.predict([clean_input])[0]
16
- bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
17
 
18
- # Append to history
19
- history.append((user_input, bot_reply))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Format chat history nicely
22
- formatted_history = ""
23
- for user_msg, bot_msg in history:
24
- formatted_history += f"**You:** {user_msg}\n**Bot:** {bot_msg}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- return formatted_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Gradio Interface
29
  demo = gr.Interface(
30
  fn=chatbot_response,
31
- inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
32
- outputs=gr.Markdown(),
33
- title="LMS Chatbot",
34
- description="Ask anything about LMS and get automated responses."
35
  )
36
 
37
  if __name__ == "__main__":
 
 
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
  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",
23
+ "message": user_input,
24
+ "time": timestamp,
25
+ "color": "#DCF8C6",
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,
58
+ "time": timestamp,
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
 
89
+ # Gradio interface
90
  demo = gr.Interface(
91
  fn=chatbot_response,
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__":