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 = """
""" for msg in history: chat_html += f"""
{msg['message']}
{msg['time']}
""" chat_html += "
" 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()