Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
-
import time
|
| 4 |
|
| 5 |
-
def
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
Args:
|
| 9 |
-
message (str): User's
|
| 10 |
history (list): Chat history
|
| 11 |
Returns:
|
| 12 |
-
str: Bot's response
|
| 13 |
"""
|
| 14 |
-
#
|
| 15 |
responses = {
|
| 16 |
-
"hello":
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
#
|
| 39 |
message = message.lower().strip()
|
| 40 |
-
|
| 41 |
-
# Find matching response
|
| 42 |
for key in responses:
|
| 43 |
if key in message:
|
| 44 |
-
|
| 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
|
| 60 |
-
|
| 61 |
-
time.sleep(0.02)
|
| 62 |
-
yield response[:i+1]
|
| 63 |
|
| 64 |
-
# Create the
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
-
fn=
|
| 67 |
-
title="
|
| 68 |
-
description="Chat with me!
|
| 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(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def respond(message, history):
|
| 4 |
"""
|
| 5 |
+
Simple chatbot response function.
|
| 6 |
Args:
|
| 7 |
+
message (str): User's message
|
| 8 |
history (list): Chat history
|
| 9 |
Returns:
|
| 10 |
+
str: Bot's response
|
| 11 |
"""
|
| 12 |
+
# Simple response dictionary
|
| 13 |
responses = {
|
| 14 |
+
"hello": "Hi there! How can I help you?",
|
| 15 |
+
"hi": "Hello! Nice to meet you!",
|
| 16 |
+
"how are you": "I'm doing great, thanks for asking!",
|
| 17 |
+
"bye": "Goodbye! Have a great day!",
|
| 18 |
+
"thank you": "You're welcome!",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Check for matching response
|
| 22 |
message = message.lower().strip()
|
|
|
|
|
|
|
| 23 |
for key in responses:
|
| 24 |
if key in message:
|
| 25 |
+
return responses[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Default response if no match found
|
| 28 |
+
return "I'm not sure I understand. Could you rephrase that?"
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Create the chat interface
|
| 31 |
demo = gr.ChatInterface(
|
| 32 |
+
fn=respond,
|
| 33 |
+
title="Simple Chat Assistant",
|
| 34 |
+
description="Chat with me! Try saying hello!",
|
| 35 |
+
examples=["hello", "how are you", "thank you", "bye"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|