File size: 3,255 Bytes
08313ff
 
 
8c65a18
c1e1c68
08313ff
 
 
 
 
8c65a18
08313ff
 
 
8c65a18
c1e1c68
 
 
8c65a18
c1e1c68
 
 
 
 
 
 
 
8c65a18
 
 
 
 
 
 
 
 
 
 
 
c1e1c68
8c65a18
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e1c68
 
 
 
 
 
 
 
8c65a18
 
 
c1e1c68
 
 
 
 
 
 
 
 
 
 
 
8c65a18
 
 
 
 
 
 
 
c1e1c68
 
08313ff
 
 
 
c1e1c68
 
 
8c65a18
08313ff
f3287af
 
08313ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import joblib
from src.preprocess import clean_text
import time
import datetime

# Load model and responses
model = joblib.load("models/lms_chatbot.joblib")
responses = joblib.load("models/responses.joblib")

# Keep conversation history
history = []

def chatbot_response(user_input):
    """Generates chat HTML with typing animation for bot."""
    if not user_input.strip():
        return ""
    
    # User message
    timestamp = datetime.datetime.now().strftime("%H:%M")
    history.append({
        "sender": "You",
        "message": user_input,
        "time": timestamp,
        "color": "#DCF8C6",
        "align": "right"
    })

    # Bot typing animation
    bot_reply = responses.get(model.predict([clean_text(user_input)])[0], ["Sorry, I don't understand."])[0]
    typing_html = """
    <div style="text-align:left; margin:8px 0;">
        <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;">
            Bot is typing...
        </div>
    </div>
    """
    # Render chat with typing bubble
    chat_html = render_chat(typing=True)
    
    # Simulate typing
    for i in range(len(bot_reply)+1):
        history.append({
            "sender": "Bot",
            "message": bot_reply[:i],
            "time": timestamp,
            "color": "#FFFFFF",
            "align": "left"
        })
        chat_html = render_chat()
        time.sleep(0.03)
        history.pop()  # remove last partial message to update in next loop

    # Add final bot message
    history.append({
        "sender": "Bot",
        "message": bot_reply,
        "time": timestamp,
        "color": "#FFFFFF",
        "align": "left"
    })
    
    return render_chat()

def render_chat(typing=False):
    chat_html = """
    <div style="font-family:Helvetica, Arial; background:#F0F0F0; padding:15px; height:400px; overflow-y:auto; border-radius:10px; border:1px solid #ccc">
    """
    for msg in history:
        chat_html += f"""
        <div style="text-align:{msg['align']}; margin:8px 0;">
            <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);">
                {msg['message']}<br>
                <span style="font-size:10px; color:gray; float:right;">{msg['time']}</span>
            </div>
        </div>
        """
    if typing:
        chat_html += """
        <div style="text-align:left; margin:8px 0;">
            <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;">
                Bot is typing...
            </div>
        </div>
        """
    chat_html += "</div>"
    return chat_html

# Gradio interface
demo = gr.Interface(
    fn=chatbot_response,
    inputs=gr.Textbox(lines=2, placeholder="Type your message here...", label="Your Message"),
    outputs=gr.HTML(label="Chat"),
    title="🟢 LMS Chatbot",
    description="Ask anything about your LMS. Automatic reply with typing animation!"
)

if __name__ == "__main__":
    demo.launch()