habi01 commited on
Commit
f7c79fc
·
verified ·
1 Parent(s): ecc8436

Update app.py

Browse files

Updated the code to be based on the Creating a Chatbot Fast document. (https://www.gradio.app/guides/creating-a-chatbot-fast)

Files changed (1) hide show
  1. app.py +74 -31
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
- Respond to user input based on predefined logic.
11
- :param message: User's most recent message.
12
- :param history: List of previous messages in the format [{"role": ..., "content": ...}].
13
- :return: Updated history with the bot's response.
 
 
14
  """
15
- # Predefined responses
16
  responses = {
17
- "hello": "Hi there! How can I help you?",
18
- "how are you?": "I'm just a bot, but I'm doing great! How about you?",
19
- "what's your name?": "I'm your friendly chatbot! What's your name?",
20
- "bye": "Goodbye! Have a great day!",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Default response if no match is found
24
- response = responses.get(message.lower(), "I'm sorry, I didn't understand that. Can you try rephrasing?")
25
-
26
- # Update the chat history
27
- history.append({"role": "user", "content": message})
28
- history.append({"role": "assistant", "content": response})
29
-
30
- return history
 
 
 
 
31
 
32
- # Set up Gradio's ChatInterface
33
  demo = gr.ChatInterface(
34
- fn=respond, # The chatbot response function
35
- type="messages", # Ensure OpenAI-style 'role' and 'content' keys are used
36
- title="Simple Chatbot",
37
- description="Type a message to chat with this friendly bot!",
38
- examples=["hello", "how are you?", "what's your name?", "bye"], # Predefined examples for testing
 
 
 
 
 
 
 
 
 
 
 
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)