import gradio as gr # Define a function to handle the chatbot's response def respond(message, chat_history): # Simple Q&A logic if "hello" in message.lower(): response = "Hello! How can I assist you today?" elif "how are you" in message.lower(): response = "I'm just a chatbot, but I'm here to help you!" elif "what is your name" in message.lower(): response = "I'm a chatbot. You can call me Assistant." else: response = "I'm not sure how to answer that. Can you please rephrase your question?" # Append the user's message and the bot's response to the chat history chat_history.append({"role": "user", "content": message}) chat_history.append({"role": "assistant", "content": response}) return "", chat_history # Create a Gradio interface for the chatbot with gr.Blocks() as demo: chatbot = gr.Chatbot(type="messages") msg = gr.Textbox(label="Enter your message") clear = gr.Button("Clear") # Define the event listeners msg.submit(respond, [msg, chatbot], [msg, chatbot]) clear.click(lambda: [], None, chatbot, queue=False) # Launch the interface demo.launch(show_error=True)