File size: 1,174 Bytes
c4ef9fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)