Spaces:
Running
Running
| # app.py | |
| import gradio as gr | |
| def chatbot_response(message, history): | |
| """Process user input and return chatbot response""" | |
| user_input = message.lower() | |
| if "hello" in user_input: | |
| return "Hello there! How can I help you today?" | |
| elif "bye" in user_input: | |
| return "Goodbye! π" | |
| else: | |
| return f"You said: {message}. I'm still learning!" | |
| # Create Gradio interface | |
| with gr.Blocks(title="AI Chatbot", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π€ AI Chatbot") | |
| gr.Markdown("Welcome to my simple AI chatbot! Try saying 'hello' or 'bye'.") | |
| # Chat interface | |
| chatbot = gr.Chatbot( | |
| label="Chat", | |
| height=400, | |
| show_label=True, | |
| container=True, | |
| bubble_full_width=False | |
| ) | |
| # Text input | |
| msg = gr.Textbox( | |
| label="Your message", | |
| placeholder="Type your message here...", | |
| lines=1, | |
| max_lines=3, | |
| show_label=True | |
| ) | |
| # Submit button | |
| submit_btn = gr.Button("Send", variant="primary") | |
| # Clear button | |
| clear_btn = gr.Button("Clear", variant="secondary") | |
| # Event handlers | |
| def user(user_message, history): | |
| return "", history + [[user_message, None]] | |
| def bot(history): | |
| user_message = history[-1][0] | |
| bot_message = chatbot_response(user_message, history) | |
| history[-1][1] = bot_message | |
| return history | |
| # Connect the interface | |
| msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( | |
| bot, chatbot, chatbot | |
| ) | |
| submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then( | |
| bot, chatbot, chatbot | |
| ) | |
| clear_btn.click(lambda: None, None, chatbot, queue=False) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |