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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -67
app.py CHANGED
@@ -1,86 +1,39 @@
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)
 
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()