import gradio as gr # For building the chatbot UI import random # For generating random responses # Chatbot response function def chatbot_response(user_input, history): user_input = user_input.lower() # Handle empty input and greet the user if user_input.strip() == "": return "Welcome! How can I assist you with your studies today?", history # Predefined responses based on user input if "time management" in user_input: return "Time management is key! Try creating a prioritized to-do list and setting specific study blocks.", history 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), history elif "hello" in user_input or "hi" in user_input: return "Hello! How can I help you with your studies today?", history else: return "I'm here to assist with general academic questions. Feel free to ask about study tips, time management, or anything else!", history # Gradio interface setup with gr.Blocks() as demo: gr.Markdown("# Study Assistance Chatbot") gr.Markdown("Welcome! Ask me anything related to your academic studies.") chatbot = gr.Chatbot() # Chat history UI user_input = gr.Textbox(label="Enter your question here:", placeholder="Type your question...") # Textbox for user input submit_button = gr.Button("Submit") # Submit button for user input # Initialize the chat history with a welcome message initial_history = [("Chatbot", "Welcome! How can I assist you with your studies today?")] # Submit action - to update chat with response and add it to chat history submit_button.click(chatbot_response, inputs=[user_input, chatbot], outputs=[user_input, chatbot]) # Launch the Gradio app demo.launch()