Spaces:
Sleeping
Sleeping
| 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() | |