Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
# Define
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Set up Gradio's ChatInterface
|
| 18 |
demo = gr.ChatInterface(
|
| 19 |
-
fn=
|
| 20 |
-
type="messages", # Use
|
| 21 |
-
title="Simple
|
| 22 |
-
description="This chatbot
|
| 23 |
-
examples=["
|
| 24 |
-
save_history=True, # Enable persistent chat history
|
| 25 |
-
theme="default", # Optional: Change to other themes like "compact" or "ocean"
|
| 26 |
chatbot=gr.Chatbot(height=400, placeholder="Start chatting with the bot!"),
|
| 27 |
textbox=gr.Textbox(placeholder="Type your message here...", container=False),
|
| 28 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Define a response function for the chatbot
|
| 4 |
+
def respond(message, history=[]):
|
| 5 |
+
# Predefined responses (case-insensitive matching)
|
| 6 |
+
responses = {
|
| 7 |
+
"hello": "Hi there! How can I help you?",
|
| 8 |
+
"how are you?": "I'm just a bot, but I'm doing great! How about you?",
|
| 9 |
+
"what's your name?": "I'm your friendly chatbot! What's your name?",
|
| 10 |
+
"bye": "Goodbye! Have a great day!",
|
| 11 |
+
"default": "I'm sorry, I didn't understand that. Can you try rephrasing?",
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Check if the user's message matches a predefined response
|
| 15 |
+
message_lower = message.lower()
|
| 16 |
+
response = responses.get(message_lower, responses["default"])
|
| 17 |
+
|
| 18 |
+
# Add the user message and bot response to the chat history
|
| 19 |
+
history.append((message, response))
|
| 20 |
+
|
| 21 |
+
return history
|
| 22 |
|
| 23 |
# Set up Gradio's ChatInterface
|
| 24 |
demo = gr.ChatInterface(
|
| 25 |
+
fn=respond, # Use the predefined respond function
|
| 26 |
+
type="messages", # Use openai-style 'role' and 'content' keys
|
| 27 |
+
title="Simple Predefined Chatbot",
|
| 28 |
+
description="This chatbot responds to specific predefined messages. Try 'hello' or 'how are you?'.",
|
| 29 |
+
examples=["hello", "how are you?", "what's your name?", "bye"],
|
|
|
|
|
|
|
| 30 |
chatbot=gr.Chatbot(height=400, placeholder="Start chatting with the bot!"),
|
| 31 |
textbox=gr.Textbox(placeholder="Type your message here...", container=False),
|
| 32 |
)
|