File size: 2,095 Bytes
fc1847d
b84ccee
 
fc1847d
b84ccee
fc1847d
b84ccee
 
9ec6ed2
4fc7857
fc1847d
9ec6ed2
b84ccee
fc1847d
 
 
4fc7857
fc1847d
 
 
 
 
 
 
 
 
 
2c53cdf
 
fc1847d
4fc7857
fc1847d
 
 
 
 
 
 
 
 
4fc7857
fc1847d
 
4fc7857
 
 
 
fc1847d
 
 
 
 
 
 
 
 
 
 
 
4fc7857
fc1847d
 
4fc7857
 
fc1847d
4fc7857
fc1847d
 
 
 
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
import gradio as gr
import joblib
from src.preprocess import clean_text
import datetime

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

# Keep conversation history
history = []

def chatbot_response(user_input):
    if not user_input.strip():
        return ""

    # Add user message
    timestamp = datetime.datetime.now().strftime("%H:%M")
    history.append({
        "sender": "You",
        "message": user_input,
        "time": timestamp,
        "color": "#DCF8C6",
        "align": "right"
    })

    # Bot prediction
    tag = model.predict([clean_text(user_input)])[0]
    bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]

    # Add bot message
    timestamp = datetime.datetime.now().strftime("%H:%M")
    history.append({
        "sender": "Bot",
        "message": bot_reply,
        "time": timestamp,
        "color": "#FFFFFF",
        "align": "left"
    })

    # Render chat
    return render_chat()

def render_chat():
    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>
        """
    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 chat bubbles and timestamps!"
)

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