Spaces:
Running
Running
File size: 2,618 Bytes
3641859 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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() |