Spaces:
Sleeping
Sleeping
Update chatbot_ui.py
Browse files1 unmodified line23456567891011121389101411121314152021222324161718192620212223242528262728293031323334333435363738394041424344353637383940411 unmodified lineimport random # For generating random responses
# Chatbot response functiondef chatbot_response(user_input): user_input = user_input.lower()def chatbot_response(user_input, chat_history): response = ""
if user_input.strip() == "": return "Welcome! How can I assist you with your studies today?"
if "time management" in user_input: return "Time management is key! Try creating a prioritized to-do list and setting specific study blocks."
if "time management" in user_input.lower(): response = "Time management is key! Try creating a prioritized to-do list and setting specific study blocks." elif "study tips" in user_input.lower(): elif "study tips" in user_input: tips = [ "Take regular breaks while studying to stay focused.", "Use active recall and spaced repetition for better retention.", "Set a specific goal for each study session." ] return random.choice(tips)
elif "hello" in user_input or "hi" in user_input: return "Hello! How can I help you with your studies today?"
response = random.choice(tips) elif "hello" in user_input.lower() or "hi" in user_input.lower(): response = "Hello! How can I help you with your studies today?" else: return "I'm here to assist with general academic questions. Feel free to ask about study tips, time management, or anything else!" response = "I'm here to assist with general academic questions. Feel free to ask about study tips, time management, or anything else!" chat_history.append(("You", user_input)) chat_history.append(("Chatbot", response)) return "", chat_history
# Gradio interface# Gradio interface with chat historywith gr.Blocks() as demo: gr.Markdown("# Study Assistance Chatbot") gr.Markdown("Welcome! Ask me anything related to your academic studies.")
chatbot = gr.Chatbot(label="Chatbot") user_input = gr.Textbox(label="Enter your question here:", placeholder="Type your question...") submit_button = gr.Button("Submit") with gr.Row(): with gr.Column(): user_input = gr.Textbox(label="Enter your question here:", placeholder="Type your question...") submit_button = gr.Button("Submit") with gr.Column(): chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False) # Automatically simulate the first message demo.load(lambda: "Welcome! How can I assist you with your studies today?", outputs=chatbot_output) submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output) # Set an initial welcome message chat_history = [("Chatbot", "Welcome! How can I assist you with your studies today?")]
submit_button.click(chatbot_response, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
# Launch the Gradio appdemo.launch()chatbot_ui.py+22-28