ayush2917 commited on
Commit
6043438
·
verified ·
1 Parent(s): 9e9b1dd

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +46 -18
chatbot.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
2
  import requests
 
3
  from dotenv import load_dotenv
4
- from messages import krishna_blessings
5
  from ayush_messages import ayush_surprises
6
 
7
  # Load environment variables (Hugging Face Space secrets)
@@ -10,7 +11,8 @@ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
10
 
11
  # Simple context tracking (e.g., last topic discussed)
12
  conversation_context = {
13
- "last_topic": None # Store the last keyword matched (e.g., "birthday", "riddle")
 
14
  }
15
 
16
  def get_krishna_response(user_input):
@@ -18,16 +20,53 @@ def get_krishna_response(user_input):
18
  Generate a response from Little Krishna based on user input.
19
  - Match user input to predefined messages in krishna_blessings or ayush_surprises using keywords.
20
  - Use context to provide more coherent responses.
 
21
  - Fall back to Hugging Face's Inference API for unmatched inputs.
22
  """
23
  user_input_lower = user_input.lower().strip()
24
 
 
 
 
25
  # Reset context if user starts a new conversation
26
  if "start over" in user_input_lower or "reset" in user_input_lower:
27
  conversation_context["last_topic"] = None
 
28
  return "Hare Manavi! Let’s start a new adventure in Vrindavan—what would you like to talk about?"
29
 
30
- # Greetings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  if "hello" in user_input_lower or "hi" in user_input_lower or "hii" in user_input_lower:
32
  conversation_context["last_topic"] = "greeting"
33
  return krishna_blessings["greeting"]
@@ -53,12 +92,10 @@ def get_krishna_response(user_input):
53
  conversation_context["last_topic"] = "greeting"
54
  return krishna_blessings["welcome"]
55
 
56
- # Identity Questions (e.g., "Who are you")
57
  if "who are you" in user_input_lower or "what are you" in user_input_lower or "tell me about yourself" in user_input_lower:
58
  conversation_context["last_topic"] = "identity"
59
  return "Hare Manavi! I’m Little Krishna, the playful cowherd of Vrindavan! I love playing my flute, stealing butter, and dancing with the gopis. What would you like to do with me today?"
60
 
61
- # Playful Responses
62
  if "play" in user_input_lower or "fun" in user_input_lower:
63
  conversation_context["last_topic"] = "playful"
64
  return krishna_blessings["playful"]
@@ -84,7 +121,6 @@ def get_krishna_response(user_input):
84
  conversation_context["last_topic"] = "swing"
85
  return krishna_blessings["swing"]
86
 
87
- # Shy or Quiet Moments
88
  if "shy" in user_input_lower:
89
  conversation_context["last_topic"] = "shy"
90
  return krishna_blessings["shy"]
@@ -101,10 +137,9 @@ def get_krishna_response(user_input):
101
  conversation_context["last_topic"] = "thoughtful"
102
  return krishna_blessings["thoughtful"]
103
 
104
- # Jokes
105
- if "joke" in user_input_lower or "funny" in user_input_lower:
106
  conversation_context["last_topic"] = "joke"
107
- return krishna_blessings["joke"]
108
  if "laugh" in user_input_lower or "giggle" in user_input_lower:
109
  conversation_context["last_topic"] = "joke"
110
  return krishna_blessings["giggle_joke"]
@@ -127,7 +162,6 @@ def get_krishna_response(user_input):
127
  conversation_context["last_topic"] = "joke"
128
  return krishna_blessings["mischief_joke"]
129
 
130
- # Riddles
131
  if "riddle" in user_input_lower or "puzzle" in user_input_lower:
132
  conversation_context["last_topic"] = "riddle"
133
  return krishna_blessings["riddle"]
@@ -138,7 +172,6 @@ def get_krishna_response(user_input):
138
  conversation_context["last_topic"] = "riddle"
139
  return krishna_blessings["question"]
140
 
141
- # Birthday Wishes
142
  if "birthday" in user_input_lower:
143
  conversation_context["last_topic"] = "birthday"
144
  return ayush_surprises["birthday"]
@@ -170,7 +203,6 @@ def get_krishna_response(user_input):
170
  conversation_context["last_topic"] = "birthday"
171
  return krishna_blessings["birthday_magic"]
172
 
173
- # Wisdom and Advice
174
  if "wisdom" in user_input_lower or "advice" in user_input_lower:
175
  conversation_context["last_topic"] = "wisdom"
176
  return krishna_blessings["wisdom"]
@@ -196,7 +228,6 @@ def get_krishna_response(user_input):
196
  conversation_context["last_topic"] = "wisdom"
197
  return krishna_blessings["love"]
198
 
199
- # Nature and Vrindavan
200
  if "nature" in user_input_lower or "vrindavan" in user_input_lower:
201
  conversation_context["last_topic"] = "nature"
202
  return krishna_blessings["nature"]
@@ -225,7 +256,6 @@ def get_krishna_response(user_input):
225
  conversation_context["last_topic"] = "nature"
226
  return krishna_blessings["sunset"]
227
 
228
- # Encouragement
229
  if "encourage" in user_input_lower or "cheer" in user_input_lower:
230
  conversation_context["last_topic"] = "encourage"
231
  return krishna_blessings["encourage"]
@@ -248,7 +278,6 @@ def get_krishna_response(user_input):
248
  conversation_context["last_topic"] = "encourage"
249
  return krishna_blessings["shine"]
250
 
251
- # Miscellaneous
252
  if "friend" in user_input_lower:
253
  conversation_context["last_topic"] = "friend"
254
  return krishna_blessings["friend"]
@@ -280,7 +309,6 @@ def get_krishna_response(user_input):
280
  conversation_context["last_topic"] = "blessing"
281
  return krishna_blessings["blessing"]
282
 
283
- # Context-Based Responses (if no direct match, use the last topic)
284
  if conversation_context["last_topic"]:
285
  last_topic = conversation_context["last_topic"]
286
  if last_topic in krishna_blessings:
@@ -294,8 +322,8 @@ def get_krishna_response(user_input):
294
  payload = {
295
  "inputs": f"You are Little Krishna, playful and wise. Respond to this in a fun, Krishna-like way: '{user_input}'",
296
  "parameters": {
297
- "max_length": 50, # Reduced for shorter, more playful responses
298
- "temperature": 0.7 # Slightly lower for more coherent responses
299
  }
300
  }
301
  try:
 
1
  import os
2
  import requests
3
+ import random
4
  from dotenv import load_dotenv
5
+ from messages import krishna_blessings, ayush_teasing
6
  from ayush_messages import ayush_surprises
7
 
8
  # Load environment variables (Hugging Face Space secrets)
 
11
 
12
  # Simple context tracking (e.g., last topic discussed)
13
  conversation_context = {
14
+ "last_topic": None, # Store the last keyword matched (e.g., "birthday", "riddle")
15
+ "message_count": 0 # Track the number of messages to trigger Ayush-teasing every 5th message
16
  }
17
 
18
  def get_krishna_response(user_input):
 
20
  Generate a response from Little Krishna based on user input.
21
  - Match user input to predefined messages in krishna_blessings or ayush_surprises using keywords.
22
  - Use context to provide more coherent responses.
23
+ - Occasionally tease Manavi about Ayush (keyword-based or every 5th message).
24
  - Fall back to Hugging Face's Inference API for unmatched inputs.
25
  """
26
  user_input_lower = user_input.lower().strip()
27
 
28
+ # Increment message count
29
+ conversation_context["message_count"] += 1
30
+
31
  # Reset context if user starts a new conversation
32
  if "start over" in user_input_lower or "reset" in user_input_lower:
33
  conversation_context["last_topic"] = None
34
+ conversation_context["message_count"] = 0
35
  return "Hare Manavi! Let’s start a new adventure in Vrindavan—what would you like to talk about?"
36
 
37
+ # Check for Ayush-teasing triggers (keyword-based)
38
+ if "joke" in user_input_lower:
39
+ conversation_context["last_topic"] = "joke"
40
+ # Randomly decide between a Krishna joke and an Ayush-teasing joke
41
+ if random.choice([True, False]):
42
+ return random.choice(ayush_teasing["joke"])
43
+ return krishna_blessings["joke"]
44
+ if "i miss" in user_input_lower or "missing" in user_input_lower:
45
+ conversation_context["last_topic"] = "missing"
46
+ return random.choice(ayush_teasing["missing"])
47
+ if "bored" in user_input_lower:
48
+ conversation_context["last_topic"] = "bored"
49
+ return random.choice(ayush_teasing["bored"])
50
+ if "tired" in user_input_lower:
51
+ conversation_context["last_topic"] = "tired"
52
+ return random.choice(ayush_teasing["tired"])
53
+ if "lonely" in user_input_lower:
54
+ conversation_context["last_topic"] = "lonely"
55
+ return random.choice(ayush_teasing["lonely"])
56
+ if "manavi" in user_input_lower:
57
+ conversation_context["last_topic"] = "manavi"
58
+ return random.choice(ayush_teasing["manavi"])
59
+ if "ayush" in user_input_lower or "krishna talk about ayush" in user_input_lower:
60
+ conversation_context["last_topic"] = "ayush"
61
+ return random.choice(ayush_teasing["ayush"])
62
+
63
+ # Every 5th message, randomly trigger an Ayush-teasing message (if no keyword match)
64
+ if conversation_context["message_count"] % 5 == 0:
65
+ # Randomly select a category from ayush_teasing
66
+ category = random.choice(list(ayush_teasing.keys()))
67
+ return random.choice(ayush_teasing[category])
68
+
69
+ # Existing keyword mappings for krishna_blessings and ayush_surprises
70
  if "hello" in user_input_lower or "hi" in user_input_lower or "hii" in user_input_lower:
71
  conversation_context["last_topic"] = "greeting"
72
  return krishna_blessings["greeting"]
 
92
  conversation_context["last_topic"] = "greeting"
93
  return krishna_blessings["welcome"]
94
 
 
95
  if "who are you" in user_input_lower or "what are you" in user_input_lower or "tell me about yourself" in user_input_lower:
96
  conversation_context["last_topic"] = "identity"
97
  return "Hare Manavi! I’m Little Krishna, the playful cowherd of Vrindavan! I love playing my flute, stealing butter, and dancing with the gopis. What would you like to do with me today?"
98
 
 
99
  if "play" in user_input_lower or "fun" in user_input_lower:
100
  conversation_context["last_topic"] = "playful"
101
  return krishna_blessings["playful"]
 
121
  conversation_context["last_topic"] = "swing"
122
  return krishna_blessings["swing"]
123
 
 
124
  if "shy" in user_input_lower:
125
  conversation_context["last_topic"] = "shy"
126
  return krishna_blessings["shy"]
 
137
  conversation_context["last_topic"] = "thoughtful"
138
  return krishna_blessings["thoughtful"]
139
 
140
+ if "funny" in user_input_lower:
 
141
  conversation_context["last_topic"] = "joke"
142
+ return krishna_blessings["funny"]
143
  if "laugh" in user_input_lower or "giggle" in user_input_lower:
144
  conversation_context["last_topic"] = "joke"
145
  return krishna_blessings["giggle_joke"]
 
162
  conversation_context["last_topic"] = "joke"
163
  return krishna_blessings["mischief_joke"]
164
 
 
165
  if "riddle" in user_input_lower or "puzzle" in user_input_lower:
166
  conversation_context["last_topic"] = "riddle"
167
  return krishna_blessings["riddle"]
 
172
  conversation_context["last_topic"] = "riddle"
173
  return krishna_blessings["question"]
174
 
 
175
  if "birthday" in user_input_lower:
176
  conversation_context["last_topic"] = "birthday"
177
  return ayush_surprises["birthday"]
 
203
  conversation_context["last_topic"] = "birthday"
204
  return krishna_blessings["birthday_magic"]
205
 
 
206
  if "wisdom" in user_input_lower or "advice" in user_input_lower:
207
  conversation_context["last_topic"] = "wisdom"
208
  return krishna_blessings["wisdom"]
 
228
  conversation_context["last_topic"] = "wisdom"
229
  return krishna_blessings["love"]
230
 
 
231
  if "nature" in user_input_lower or "vrindavan" in user_input_lower:
232
  conversation_context["last_topic"] = "nature"
233
  return krishna_blessings["nature"]
 
256
  conversation_context["last_topic"] = "nature"
257
  return krishna_blessings["sunset"]
258
 
 
259
  if "encourage" in user_input_lower or "cheer" in user_input_lower:
260
  conversation_context["last_topic"] = "encourage"
261
  return krishna_blessings["encourage"]
 
278
  conversation_context["last_topic"] = "encourage"
279
  return krishna_blessings["shine"]
280
 
 
281
  if "friend" in user_input_lower:
282
  conversation_context["last_topic"] = "friend"
283
  return krishna_blessings["friend"]
 
309
  conversation_context["last_topic"] = "blessing"
310
  return krishna_blessings["blessing"]
311
 
 
312
  if conversation_context["last_topic"]:
313
  last_topic = conversation_context["last_topic"]
314
  if last_topic in krishna_blessings:
 
322
  payload = {
323
  "inputs": f"You are Little Krishna, playful and wise. Respond to this in a fun, Krishna-like way: '{user_input}'",
324
  "parameters": {
325
+ "max_length": 50,
326
+ "temperature": 0.7
327
  }
328
  }
329
  try: