import gradio as gr import random # Simple chatbot responses responses = { "hello": ["Hi there!", "Hello!", "Hey! How can I help you?"], "how are you": ["I'm doing well, thank you!", "Great! Thanks for asking.", "I'm a chatbot, so I'm always ready to help!"], "bye": ["Goodbye!", "See you later!", "Bye! Have a great day!"], "default": ["I'm not sure I understand.", "Can you tell me more?", "That's interesting!", "Let's talk about something else."] } def chatbot(message, history): """ Simple chatbot function that responds to user messages. Args: message (str): The user's input message history (list): List of previous conversations Returns: str: The chatbot's response """ message = message.lower() # Check for specific keywords and respond accordingly if "hello" in message or "hi" in message: response = random.choice(responses["hello"]) elif "how are you" in message: response = random.choice(responses["how are you"]) elif "bye" in message: response = random.choice(responses["bye"]) else: response = random.choice(responses["default"]) return response # Create the chatbot interface with gr.Blocks(title="Simple Chatbot") as demo: gr.Markdown("# 🤖 Simple Chatbot") gr.Markdown("Talk to this basic chatbot. Type 'hello', 'how are you', or 'bye' for specific responses!") # Chatbot component chatbot_interface = gr.Chatbot( label="Chatbot Conversation", bubble_full_width=False ) # User input user_input = gr.Textbox( label="Your Message", placeholder="Type your message here...", lines=2 ) # Submit button submit_btn = gr.Button("Send Message") # Clear button clear_btn = gr.Button("Clear Conversation") # Connect components user_input.submit( fn=chatbot, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface], queue=False ).then( fn=lambda: "", inputs=None, outputs=user_input, queue=False ) submit_btn.click( fn=chatbot, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface], queue=False ).then( fn=lambda: "", inputs=None, outputs=user_input, queue=False ) clear_btn.click( fn=lambda: None, inputs=None, outputs=chatbot_interface, queue=False ) # Launch the app if __name__ == "__main__": demo.launch()