Spaces:
Build error
Build error
Update app.py
Browse filesUpdated the code to be based on the Creating a Chatbot Fast document. (https://www.gradio.app/guides/creating-a-chatbot-fast)
app.py
CHANGED
|
@@ -1,43 +1,86 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
This is a simple chatbot inspired by the Gradio guide, with predefined responses.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
# Define the chatbot response function
|
| 8 |
-
def respond(message, history):
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
#
|
| 16 |
responses = {
|
| 17 |
-
"hello":
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Default response if no match
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
-
fn=
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
-
# Launch the app
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
+
def chatbot_response(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
+
Enhanced chatbot response function with more natural responses and features.
|
| 8 |
+
Args:
|
| 9 |
+
message (str): User's current message
|
| 10 |
+
history (list): Chat history
|
| 11 |
+
Returns:
|
| 12 |
+
str: Bot's response, potentially streamed
|
| 13 |
"""
|
| 14 |
+
# Expanded response dictionary with more varied responses
|
| 15 |
responses = {
|
| 16 |
+
"hello": [
|
| 17 |
+
"Hi there! How can I help you today?",
|
| 18 |
+
"Hello! Nice to meet you!",
|
| 19 |
+
"Hey! What's on your mind?",
|
| 20 |
+
],
|
| 21 |
+
"how are you": [
|
| 22 |
+
"I'm doing great, thanks for asking! How about you?",
|
| 23 |
+
"I'm functioning perfectly! How are you today?",
|
| 24 |
+
"All systems operational! How's your day going?",
|
| 25 |
+
],
|
| 26 |
+
"bye": [
|
| 27 |
+
"Goodbye! Have a great day!",
|
| 28 |
+
"See you later! Take care!",
|
| 29 |
+
"Bye for now! Come back soon!",
|
| 30 |
+
],
|
| 31 |
+
"thank": [
|
| 32 |
+
"You're welcome!",
|
| 33 |
+
"Glad I could help!",
|
| 34 |
+
"No problem at all!",
|
| 35 |
+
],
|
| 36 |
}
|
| 37 |
+
|
| 38 |
+
# Simulate typing with streaming response
|
| 39 |
+
message = message.lower().strip()
|
| 40 |
+
|
| 41 |
+
# Find matching response
|
| 42 |
+
for key in responses:
|
| 43 |
+
if key in message:
|
| 44 |
+
response = random.choice(responses[key])
|
| 45 |
+
# Stream the response
|
| 46 |
+
for i in range(len(response)):
|
| 47 |
+
time.sleep(0.02) # Add slight delay for natural feeling
|
| 48 |
+
yield response[:i+1]
|
| 49 |
+
return
|
| 50 |
|
| 51 |
+
# Default response if no match found
|
| 52 |
+
default_responses = [
|
| 53 |
+
"I'm not sure I understand. Could you rephrase that?",
|
| 54 |
+
"Interesting! Tell me more about that.",
|
| 55 |
+
"I'm still learning. Could you elaborate?",
|
| 56 |
+
"That's a good question. Let me think about it...",
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
response = random.choice(default_responses)
|
| 60 |
+
for i in range(len(response)):
|
| 61 |
+
time.sleep(0.02)
|
| 62 |
+
yield response[:i+1]
|
| 63 |
|
| 64 |
+
# Create the Gradio interface with enhanced features
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
+
fn=chatbot_response,
|
| 67 |
+
title="Friendly Chat Assistant",
|
| 68 |
+
description="Chat with me! I can respond to greetings, questions, and more!",
|
| 69 |
+
examples=[
|
| 70 |
+
"Hello there!",
|
| 71 |
+
"How are you doing?",
|
| 72 |
+
"Thank you for helping",
|
| 73 |
+
"Goodbye!",
|
| 74 |
+
],
|
| 75 |
+
retry_btn="Retry ↺",
|
| 76 |
+
undo_btn="Undo ↩",
|
| 77 |
+
clear_btn="Clear 🗑️",
|
| 78 |
+
theme="soft",
|
| 79 |
+
cache_examples=True,
|
| 80 |
+
save_history=True, # Enable chat history
|
| 81 |
+
flagging_mode="manual", # Enable user feedback
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Launch the app with additional sharing capability
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
demo.launch(share=True)
|