ayush2917 commited on
Commit
25c0746
·
verified ·
1 Parent(s): 8d77a93

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +180 -254
chatbot.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -110,7 +111,11 @@ SYSTEM_PROMPT = (
110
  "User: 'how are you krishna'\n"
111
  "Response: 'Hare Manavi! I’m as joyful as a peacock dancing in Vrindavan—how about you, my friend?'\n\n"
112
  "User: 'yes'\n"
113
- "Response: 'Hare Manavi! Let’s play a flute melody by the Yamuna—the peacocks will dance with us!'\n\n"
 
 
 
 
114
  "Now, respond to the user’s input in a fun, Krishna-like way:"
115
  )
116
 
@@ -119,18 +124,9 @@ conversation_context = {
119
  "last_topic": None, # Store the last keyword matched (e.g., "birthday", "riddle")
120
  "message_count": 0, # Track the number of messages to trigger Ayush-teasing every 5th message
121
  "last_response": None, # Track the last response to avoid repetition and enable follow-ups
122
- "last_activity": None # Track ongoing activities (e.g., "dancing", "exploring")
123
  }
124
 
125
- # List of generic positive responses to cycle through for affirmations when no activity is in progress
126
- GENERIC_POSITIVE_RESPONSES = [
127
- "Hare Manavi! Wonderful—let’s make today as magical as Vrindavan’s sunsets!",
128
- "Hare Manavi! Great—shall we chase the butterflies by the Yamuna next?",
129
- "Hare Manavi! Perfect—let’s share some butter under the kadamba tree!",
130
- "Hare Manavi! Lovely—how about a little dance with the gopis now?",
131
- "Hare Manavi! Splendid—let’s listen to the birds sing by the river!"
132
- ]
133
-
134
  def analyze_sentiment(user_input):
135
  """Analyze the sentiment of the user's input using a sentiment analysis model."""
136
  headers = {
@@ -140,27 +136,45 @@ def analyze_sentiment(user_input):
140
  payload = {
141
  "inputs": user_input
142
  }
143
- try:
144
- response = requests.post(
145
- "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english",
146
- headers=headers,
147
- json=payload
148
- )
149
- if response.status_code == 200:
150
- result = response.json()
151
- if isinstance(result, list) and len(result) > 0:
152
- sentiment = result[0]
153
- label = sentiment[0]["label"] # "POSITIVE" or "NEGATIVE"
154
- return label.lower()
155
- return "neutral"
156
- except Exception as e:
157
- print(f"Error analyzing sentiment: {str(e)}")
158
- return "neutral"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  def get_krishna_response(user_input):
161
  """
162
  Generate a response from Little Krishna based on user input.
163
- - Match user input to predefined messages in krishna_blessings or ayush_surprises using keywords.
164
  - Use sentiment analysis to tailor responses based on Manavi's mood, but only as a fallback.
165
  - Use context to provide follow-up responses (e.g., after "yes").
166
  - Occasionally tease Manavi about Ayush (keyword-based or every 5th message).
@@ -174,532 +188,443 @@ def get_krishna_response(user_input):
174
  # Increment message count
175
  conversation_context["message_count"] += 1
176
 
 
 
 
177
  # Reset context if user starts a new conversation
178
  if "start over" in user_input_lower or "reset" in user_input_lower:
179
  conversation_context["last_topic"] = None
180
  conversation_context["message_count"] = 0
181
  conversation_context["last_response"] = None
182
- conversation_context["last_activity"] = None
183
  return "Hare Manavi! Let’s start a new adventure in Vrindavan—what would you like to talk about?"
184
 
185
  # Check for follow-up responses based on context
186
  if conversation_context["last_response"] == "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?":
187
  if "yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower:
188
- conversation_context["last_response"] = None
189
- conversation_context["last_activity"] = "dancing"
190
  return "Hare Manavi! Let’s play a flute melody by the Yamuna—the peacocks will dance with us!"
191
 
192
- # Handle ongoing activities
193
- if conversation_context["last_activity"]:
194
- if conversation_context["last_activity"] == "dancing":
195
- if "yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower:
196
- conversation_context["last_response"] = None
197
- conversation_context["last_activity"] = "dancing"
198
- return "Hare Manavi! The gopis have joined us—let’s twirl under the moonlight by the Yamuna!"
199
- if "how" in user_input_lower or "but how" in user_input_lower:
200
- conversation_context["last_response"] = None
201
- return "Hare Manavi! We’ll sway to my flute’s tune, letting the rhythm guide our steps—what a joyful dance it’ll be!"
202
- elif conversation_context["last_activity"] == "exploring":
203
- if "yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower:
204
- conversation_context["last_response"] = None
205
- return "Hare Manavi! We found a pot of creamy butter—let’s share it under the stars by the Yamuna!"
206
- if "how" in user_input_lower or "but how" in user_input_lower:
207
- conversation_context["last_response"] = None
208
- return "Hare Manavi! We’ll tiptoe through the trees, listening for the gopis’ laughter to guide us to their hidden butter pots!"
209
-
210
- # Check for exploration triggers
211
- if "explore" in user_input_lower or "adventure" in user_input_lower:
212
- conversation_context["last_topic"] = "adventure"
213
- conversation_context["last_response"] = None
214
- conversation_context["last_activity"] = "exploring"
215
- return "Hare Manavi! Let’s sneak through Vrindavan’s forests to find hidden butter pots—what a mischievous delight!"
216
-
217
  # Check for Ayush-teasing triggers (keyword-based)
218
- if "joke" in user_input_lower:
219
  conversation_context["last_topic"] = "joke"
220
  conversation_context["last_response"] = None
221
- conversation_context["last_activity"] = None
222
  if random.choice([True, False]):
223
  return random.choice(ayush_teasing["joke"])
224
  return krishna_blessings["joke"]
225
- if "i miss" in user_input_lower or "missing" in user_input_lower:
226
  conversation_context["last_topic"] = "missing"
227
  conversation_context["last_response"] = None
228
- conversation_context["last_activity"] = None
229
  return random.choice(ayush_teasing["missing"])
230
- if "bored" in user_input_lower:
231
  conversation_context["last_topic"] = "bored"
232
  conversation_context["last_response"] = None
233
- conversation_context["last_activity"] = None
234
  return random.choice(ayush_teasing["bored"])
235
- if "tired" in user_input_lower:
236
  conversation_context["last_topic"] = "tired"
237
  conversation_context["last_response"] = None
238
- conversation_context["last_activity"] = None
239
  return random.choice(ayush_teasing["tired"])
240
- if "lonely" in user_input_lower:
241
  conversation_context["last_topic"] = "lonely"
242
  conversation_context["last_response"] = None
243
- conversation_context["last_activity"] = None
244
  return random.choice(ayush_teasing["lonely"])
245
- if "manavi" in user_input_lower:
246
  conversation_context["last_topic"] = "manavi"
247
  conversation_context["last_response"] = None
248
- conversation_context["last_activity"] = None
249
  return random.choice(ayush_teasing["manavi"])
250
- if "ayush" in user_input_lower or "krishna talk about ayush" in user_input_lower:
251
  conversation_context["last_topic"] = "ayush"
252
  conversation_context["last_response"] = None
253
- conversation_context["last_activity"] = None
254
  return random.choice(ayush_teasing["ayush"])
255
 
256
  # Trigger for "chat with you"
257
- if "chat with you" in user_input_lower or "want to chat" in user_input_lower:
258
  conversation_context["last_topic"] = "chat_with_you"
259
  conversation_context["last_response"] = None
260
- conversation_context["last_activity"] = None
261
  return krishna_blessings["chat_with_you"]
262
 
263
  # Every 5th message, randomly trigger an Ayush-teasing message (if no keyword match)
264
- if conversation_context["message_count"] % 5 == 0:
 
265
  category = random.choice(list(ayush_teasing.keys()))
266
  conversation_context["last_response"] = None
267
- conversation_context["last_activity"] = None
268
  return random.choice(ayush_teasing[category])
269
 
270
  # Existing keyword mappings for krishna_blessings and ayush_surprises
271
- if "hello" in user_input_lower or "hi" in user_input_lower or "hii" in user_input_lower:
272
  conversation_context["last_topic"] = "greeting"
273
  conversation_context["last_response"] = None
274
- conversation_context["last_activity"] = None
275
  return krishna_blessings["greeting"]
276
- if "good morning" in user_input_lower:
277
  conversation_context["last_topic"] = "greeting"
278
  conversation_context["last_response"] = None
279
- conversation_context["last_activity"] = None
280
  return krishna_blessings["good_morning"]
281
- if "good afternoon" in user_input_lower:
282
  conversation_context["last_topic"] = "greeting"
283
  conversation_context["last_response"] = None
284
- conversation_context["last_activity"] = None
285
  return krishna_blessings["good_afternoon"]
286
- if "good evening" in user_input_lower:
287
  conversation_context["last_topic"] = "greeting"
288
  conversation_context["last_response"] = None
289
- conversation_context["last_activity"] = None
290
  return krishna_blessings["good_evening"]
291
- if "hey" in user_input_lower:
292
  conversation_context["last_topic"] = "greeting"
293
  conversation_context["last_response"] = None
294
- conversation_context["last_activity"] = None
295
  return krishna_blessings["hey"]
296
- if "howdy" in user_input_lower:
297
  conversation_context["last_topic"] = "greeting"
298
  conversation_context["last_response"] = None
299
- conversation_context["last_activity"] = None
300
  return krishna_blessings["howdy"]
301
- if "namaste" in user_input_lower:
302
  conversation_context["last_topic"] = "greeting"
303
  conversation_context["last_response"] = None
304
- conversation_context["last_activity"] = None
305
  return krishna_blessings["namaste"]
306
- if "welcome" in user_input_lower:
307
  conversation_context["last_topic"] = "greeting"
308
  conversation_context["last_response"] = None
309
- conversation_context["last_activity"] = None
310
  return krishna_blessings["welcome"]
311
 
312
- 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 or "what are you doing" in user_input_lower:
313
  conversation_context["last_topic"] = "identity"
314
  conversation_context["last_response"] = None
315
- conversation_context["last_activity"] = None
316
  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?"
317
 
318
- if "how are you" in user_input_lower:
319
  conversation_context["last_topic"] = "how_are_you"
320
  conversation_context["last_response"] = None
321
- conversation_context["last_activity"] = None
322
  return "Hare Manavi! I’m as joyful as a peacock dancing in Vrindavan—how about you, my friend?"
323
 
324
- if "yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  conversation_context["last_topic"] = "yes"
326
  conversation_context["last_response"] = None
327
- # Cycle through generic positive responses if no activity is in progress
328
- if not conversation_context["last_activity"]:
329
- response = random.choice(GENERIC_POSITIVE_RESPONSES)
330
- # Rotate the list to avoid repetition
331
- GENERIC_POSITIVE_RESPONSES.append(GENERIC_POSITIVE_RESPONSES.pop(0))
332
- return response
 
333
 
334
- if "play" in user_input_lower or "fun" in user_input_lower:
335
  conversation_context["last_topic"] = "playful"
336
  conversation_context["last_response"] = None
337
- conversation_context["last_activity"] = None
338
  return krishna_blessings["playful"]
339
- if "dance" in user_input_lower:
340
  conversation_context["last_topic"] = "dance"
341
  conversation_context["last_response"] = None
342
- conversation_context["last_activity"] = "dancing"
343
  return krishna_blessings["dance"]
344
- if "flute" in user_input_lower:
345
  conversation_context["last_topic"] = "flute"
346
  conversation_context["last_response"] = None
347
- conversation_context["last_activity"] = None
348
  return krishna_blessings["flute"]
349
- if "butter" in user_input_lower:
350
  conversation_context["last_topic"] = "butter"
351
  conversation_context["last_response"] = None
352
- conversation_context["last_activity"] = None
353
  return krishna_blessings["butter"]
354
- if "mischief" in user_input_lower or "prank" in user_input_lower:
355
  conversation_context["last_topic"] = "mischief"
356
  conversation_context["last_response"] = None
357
- conversation_context["last_activity"] = None
358
  return krishna_blessings["mischief"]
359
- if "chase" in user_input_lower or "run" in user_input_lower:
360
  conversation_context["last_topic"] = "chase"
361
  conversation_context["last_response"] = None
362
- conversation_context["last_activity"] = None
363
  return krishna_blessings["chase"]
364
- if "giggle" in user_input_lower:
365
  conversation_context["last_topic"] = "giggle"
366
  conversation_context["last_response"] = None
367
- conversation_context["last_activity"] = None
368
  return krishna_blessings["giggle"]
369
- if "swing" in user_input_lower:
370
  conversation_context["last_topic"] = "swing"
371
  conversation_context["last_response"] = None
372
- conversation_context["last_activity"] = None
373
  return krishna_blessings["swing"]
374
 
375
- if "shy" in user_input_lower:
376
  conversation_context["last_topic"] = "shy"
377
  conversation_context["last_response"] = None
378
- conversation_context["last_activity"] = None
379
  return krishna_blessings["shy"]
380
- if "quiet" in user_input_lower or "calm" in user_input_lower:
381
  conversation_context["last_topic"] = "quiet"
382
  conversation_context["last_response"] = None
383
- conversation_context["last_activity"] = None
384
  return krishna_blessings["quiet"]
385
- if "peace" in user_input_lower or "serene" in user_input_lower:
386
  conversation_context["last_topic"] = "peace"
387
  conversation_context["last_response"] = None
388
- conversation_context["last_activity"] = None
389
  return krishna_blessings["peace"]
390
- if "still" in user_input_lower or "gentle" in user_input_lower:
391
  conversation_context["last_topic"] = "still"
392
  conversation_context["last_response"] = None
393
- conversation_context["last_activity"] = None
394
  return krishna_blessings["still"]
395
- if "thoughtful" in user_input_lower or "reflect" in user_input_lower:
396
  conversation_context["last_topic"] = "thoughtful"
397
  conversation_context["last_response"] = None
398
- conversation_context["last_activity"] = None
399
  return krishna_blessings["thoughtful"]
400
 
401
- if "funny" in user_input_lower:
402
  conversation_context["last_topic"] = "joke"
403
  conversation_context["last_response"] = None
404
- conversation_context["last_activity"] = None
405
  return krishna_blessings["funny"]
406
- if "laugh" in user_input_lower or "giggle" in user_input_lower:
407
  conversation_context["last_topic"] = "joke"
408
  conversation_context["last_response"] = None
409
- conversation_context["last_activity"] = None
410
  return krishna_blessings["giggle_joke"]
411
- if "silly" in user_input_lower:
412
  conversation_context["last_topic"] = "joke"
413
  conversation_context["last_response"] = None
414
- conversation_context["last_activity"] = None
415
  return krishna_blessings["silly"]
416
- if "butter joke" in user_input_lower:
417
  conversation_context["last_topic"] = "joke"
418
  conversation_context["last_response"] = None
419
- conversation_context["last_activity"] = None
420
  return krishna_blessings["butter_joke"]
421
- if "cow joke" in user_input_lower:
422
  conversation_context["last_topic"] = "joke"
423
  conversation_context["last_response"] = None
424
- conversation_context["last_activity"] = None
425
  return krishna_blessings["cow_joke"]
426
- if "flute joke" in user_input_lower:
427
  conversation_context["last_topic"] = "joke"
428
  conversation_context["last_response"] = None
429
- conversation_context["last_activity"] = None
430
  return krishna_blessings["flute_joke"]
431
- if "dance joke" in user_input_lower:
432
  conversation_context["last_topic"] = "joke"
433
  conversation_context["last_response"] = None
434
- conversation_context["last_activity"] = None
435
  return krishna_blessings["dance_joke"]
436
- if "mischief joke" in user_input_lower:
437
  conversation_context["last_topic"] = "joke"
438
  conversation_context["last_response"] = None
439
- conversation_context["last_activity"] = None
440
  return krishna_blessings["mischief_joke"]
441
 
442
- if "riddle" in user_input_lower or "puzzle" in user_input_lower:
443
  conversation_context["last_topic"] = "riddle"
444
  conversation_context["last_response"] = None
445
- conversation_context["last_activity"] = None
446
  return krishna_blessings["riddle"]
447
- if "mystery" in user_input_lower or "enigma" in user_input_lower:
448
  conversation_context["last_topic"] = "riddle"
449
  conversation_context["last_response"] = None
450
- conversation_context["last_activity"] = None
451
  return krishna_blessings["mystery"]
452
- if "question" in user_input_lower:
453
  conversation_context["last_topic"] = "riddle"
454
  conversation_context["last_response"] = None
455
- conversation_context["last_activity"] = None
456
  return krishna_blessings["question"]
457
 
458
- if "birthday" in user_input_lower:
459
  conversation_context["last_topic"] = "birthday"
460
  conversation_context["last_response"] = None
461
- conversation_context["last_activity"] = None
462
  return ayush_surprises["birthday"]
463
- if "happy birthday" in user_input_lower:
464
  conversation_context["last_topic"] = "birthday"
465
  conversation_context["last_response"] = None
466
- conversation_context["last_activity"] = None
467
  return krishna_blessings["happy_birthday"]
468
- if "birthday wish" in user_input_lower:
469
  conversation_context["last_topic"] = "birthday"
470
  conversation_context["last_response"] = None
471
- conversation_context["last_activity"] = None
472
  return krishna_blessings["birthday_wish"]
473
- if "birthday blessing" in user_input_lower:
474
  conversation_context["last_topic"] = "birthday"
475
  conversation_context["last_response"] = None
476
- conversation_context["last_activity"] = None
477
  return krishna_blessings["birthday_blessing"]
478
- if "birthday dance" in user_input_lower:
479
  conversation_context["last_topic"] = "birthday"
480
  conversation_context["last_response"] = None
481
- conversation_context["last_activity"] = "dancing"
482
  return krishna_blessings["birthday_dance"]
483
- if "birthday song" in user_input_lower:
484
  conversation_context["last_topic"] = "birthday"
485
  conversation_context["last_response"] = None
486
- conversation_context["last_activity"] = None
487
  return krishna_blessings["birthday_song"]
488
- if "birthday gift" in user_input_lower:
489
  conversation_context["last_topic"] = "birthday"
490
  conversation_context["last_response"] = None
491
- conversation_context["last_activity"] = None
492
  return krishna_blessings["birthday_gift"]
493
- if "birthday smile" in user_input_lower:
494
  conversation_context["last_topic"] = "birthday"
495
  conversation_context["last_response"] = None
496
- conversation_context["last_activity"] = None
497
  return krishna_blessings["birthday_smile"]
498
- if "birthday love" in user_input_lower:
499
  conversation_context["last_topic"] = "birthday"
500
  conversation_context["last_response"] = None
501
- conversation_context["last_activity"] = None
502
  return krishna_blessings["birthday_love"]
503
- if "birthday magic" in user_input_lower:
504
  conversation_context["last_topic"] = "birthday"
505
  conversation_context["last_response"] = None
506
- conversation_context["last_activity"] = None
507
  return krishna_blessings["birthday_magic"]
508
 
509
- if "wisdom" in user_input_lower or "advice" in user_input_lower:
510
  conversation_context["last_topic"] = "wisdom"
511
  conversation_context["last_response"] = None
512
- conversation_context["last_activity"] = None
513
  return krishna_blessings["wisdom"]
514
- if "lesson" in user_input_lower or "truth" in user_input_lower:
515
  conversation_context["last_topic"] = "wisdom"
516
  conversation_context["last_response"] = None
517
- conversation_context["last_activity"] = None
518
  return krishna_blessings["lesson"]
519
- if "kindness" in user_input_lower:
520
  conversation_context["last_topic"] = "wisdom"
521
  conversation_context["last_response"] = None
522
- conversation_context["last_activity"] = None
523
  return krishna_blessings["kindness"]
524
- if "patience" in user_input_lower:
525
  conversation_context["last_topic"] = "wisdom"
526
  conversation_context["last_response"] = None
527
- conversation_context["last_activity"] = None
528
  return krishna_blessings["patience"]
529
- if "courage" in user_input_lower:
530
  conversation_context["last_topic"] = "wisdom"
531
  conversation_context["last_response"] = None
532
- conversation_context["last_activity"] = None
533
  return krishna_blessings["courage"]
534
- if "joy" in user_input_lower:
535
  conversation_context["last_topic"] = "wisdom"
536
  conversation_context["last_response"] = None
537
- conversation_context["last_activity"] = None
538
  return krishna_blessings["joy"]
539
- if "friendship" in user_input_lower:
540
  conversation_context["last_topic"] = "wisdom"
541
  conversation_context["last_response"] = None
542
- conversation_context["last_activity"] = None
543
  return krishna_blessings["friendship"]
544
- if "love" in user_input_lower:
545
  conversation_context["last_topic"] = "wisdom"
546
  conversation_context["last_response"] = None
547
- conversation_context["last_activity"] = None
548
  return krishna_blessings["love"]
549
 
550
- if "nature" in user_input_lower or "vrindavan" in user_input_lower:
551
  conversation_context["last_topic"] = "nature"
552
  conversation_context["last_response"] = None
553
- conversation_context["last_activity"] = None
554
  return krishna_blessings["nature"]
555
- if "yamuna" in user_input_lower or "river" in user_input_lower:
556
  conversation_context["last_topic"] = "nature"
557
  conversation_context["last_response"] = None
558
- conversation_context["last_activity"] = None
559
  return krishna_blessings["yamuna"]
560
- if "peacock" in user_input_lower:
561
  conversation_context["last_topic"] = "nature"
562
  conversation_context["last_response"] = None
563
- conversation_context["last_activity"] = None
564
  return krishna_blessings["peacock"]
565
- if "cow" in user_input_lower:
566
  conversation_context["last_topic"] = "nature"
567
  conversation_context["last_response"] = None
568
- conversation_context["last_activity"] = None
569
  return krishna_blessings["cow"]
570
- if "flower" in user_input_lower:
571
  conversation_context["last_topic"] = "nature"
572
  conversation_context["last_response"] = None
573
- conversation_context["last_activity"] = None
574
  return krishna_blessings["flower"]
575
- if "tree" in user_input_lower:
576
  conversation_context["last_topic"] = "nature"
577
  conversation_context["last_response"] = None
578
- conversation_context["last_activity"] = None
579
  return krishna_blessings["tree"]
580
- if "forest" in user_input_lower:
581
  conversation_context["last_topic"] = "nature"
582
  conversation_context["last_response"] = None
583
- conversation_context["last_activity"] = "exploring"
584
  return krishna_blessings["forest"]
585
- if "bird" in user_input_lower:
586
  conversation_context["last_topic"] = "nature"
587
  conversation_context["last_response"] = None
588
- conversation_context["last_activity"] = None
589
  return krishna_blessings["bird"]
590
- if "sunset" in user_input_lower:
591
  conversation_context["last_topic"] = "nature"
592
  conversation_context["last_response"] = None
593
- conversation_context["last_activity"] = None
594
  return krishna_blessings["sunset"]
595
 
596
- if "encourage" in user_input_lower or "cheer" in user_input_lower:
597
  conversation_context["last_topic"] = "encourage"
598
  conversation_context["last_response"] = None
599
- conversation_context["last_activity"] = None
600
  return krishna_blessings["encourage"]
601
- if "support" in user_input_lower or "uplift" in user_input_lower:
602
  conversation_context["last_topic"] = "encourage"
603
  conversation_context["last_response"] = None
604
- conversation_context["last_activity"] = None
605
  return krishna_blessings["support"]
606
- if "inspire" in user_input_lower or "motivate" in user_input_lower:
607
  conversation_context["last_topic"] = "encourage"
608
  conversation_context["last_response"] = None
609
- conversation_context["last_activity"] = None
610
  return krishna_blessings["inspire"]
611
- if "strength" in user_input_lower:
612
  conversation_context["last_topic"] = "encourage"
613
  conversation_context["last_response"] = None
614
- conversation_context["last_activity"] = None
615
  return krishna_blessings["strength"]
616
- if "hope" in user_input_lower:
617
  conversation_context["last_topic"] = "encourage"
618
  conversation_context["last_response"] = None
619
- conversation_context["last_activity"] = None
620
  return krishna_blessings["hope"]
621
- if "believe" in user_input_lower:
622
  conversation_context["last_topic"] = "encourage"
623
  conversation_context["last_response"] = None
624
- conversation_context["last_activity"] = None
625
  return krishna_blessings["believe"]
626
- if "shine" in user_input_lower:
627
  conversation_context["last_topic"] = "encourage"
628
  conversation_context["last_response"] = None
629
- conversation_context["last_activity"] = None
630
  return krishna_blessings["shine"]
631
 
632
- if "friend" in user_input_lower:
633
  conversation_context["last_topic"] = "friend"
634
  conversation_context["last_response"] = None
635
- conversation_context["last_activity"] = None
636
  return krishna_blessings["friend"]
637
- if "smile" in user_input_lower:
638
  conversation_context["last_topic"] = "smile"
639
  conversation_context["last_response"] = None
640
- conversation_context["last_activity"] = None
641
  return krishna_blessings["smile"]
642
- if "magic" in user_input_lower:
643
  conversation_context["last_topic"] = "magic"
644
  conversation_context["last_response"] = None
645
- conversation_context["last_activity"] = None
646
  return krishna_blessings["magic"]
647
- if "adventure" in user_input_lower:
648
  conversation_context["last_topic"] = "adventure"
649
  conversation_context["last_response"] = None
650
- conversation_context["last_activity"] = "exploring"
651
  return krishna_blessings["adventure"]
652
- if "song" in user_input_lower:
653
  conversation_context["last_topic"] = "song"
654
  conversation_context["last_response"] = None
655
- conversation_context["last_activity"] = None
656
  return krishna_blessings["song"]
657
- if "dream" in user_input_lower:
658
  conversation_context["last_topic"] = "dream"
659
  conversation_context["last_response"] = None
660
- conversation_context["last_activity"] = None
661
  return krishna_blessings["dream"]
662
- if "story" in user_input_lower:
663
  conversation_context["last_topic"] = "story"
664
  conversation_context["last_response"] = None
665
- conversation_context["last_activity"] = None
666
  return krishna_blessings["story"]
667
- if "surprise" in user_input_lower:
668
  conversation_context["last_topic"] = "surprise"
669
  conversation_context["last_response"] = None
670
- conversation_context["last_activity"] = None
671
  return krishna_blessings["surprise"]
672
- if "celebrate" in user_input_lower:
673
  conversation_context["last_topic"] = "celebrate"
674
  conversation_context["last_response"] = None
675
- conversation_context["last_activity"] = None
676
  return krishna_blessings["celebrate"]
677
- if "blessing" in user_input_lower:
678
  conversation_context["last_topic"] = "blessing"
679
  conversation_context["last_response"] = None
680
- conversation_context["last_activity"] = None
681
  return krishna_blessings["blessing"]
682
 
683
- if conversation_context["last_topic"]:
684
  last_topic = conversation_context["last_topic"]
685
  if last_topic in krishna_blessings:
686
  conversation_context["last_response"] = None
687
- conversation_context["last_activity"] = None
688
  return krishna_blessings[last_topic] + " What else would you like to talk about, Manavi?"
689
 
690
  # Sentiment-based responses (only as a fallback, and avoid repetition)
691
- if sentiment == "negative" and "sad" not in user_input_lower and conversation_context["last_response"] != "Hare Manavi! I see a little cloud over your heart—let’s dance by the Yamuna to bring back your smile!":
692
  response = "Hare Manavi! I see a little cloud over your heart—let’s dance by the Yamuna to bring back your smile!"
693
  conversation_context["last_response"] = response
694
- conversation_context["last_activity"] = "dancing"
695
  return response
696
- if sentiment == "positive" and conversation_context["last_response"] != "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?":
697
  response = "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?"
698
  conversation_context["last_response"] = response
699
- conversation_context["last_activity"] = None
700
  return response
701
 
702
- # Fallback to multiple open-source AI models if no keywords match
 
703
  models_to_try = AI_MODELS.copy()
704
  random.shuffle(models_to_try)
705
 
@@ -710,34 +635,36 @@ def get_krishna_response(user_input):
710
 
711
  for model in models_to_try:
712
  try:
 
713
  if model["name"] == "Grok by xAI":
 
714
  response = (
715
  f"Hare Manavi! I’m Little Krishna, speaking through Grok by xAI. "
716
  f"Let me answer in my playful way: "
717
  )
 
718
  if "color" in user_input_lower:
719
  response += "I love the golden yellow of Vrindavan’s butter—it’s as sweet as your smile! What’s your favorite color?"
720
  elif "weather" in user_input_lower:
721
  response += "The Vrindavan sky is as clear as the Yamuna today—perfect for a flute melody! How’s your weather?"
722
  elif "sad" in user_input_lower:
723
  response += "Oh, my dear gopi, don’t be sad—let’s dance by the Yamuna, and I’ll play a tune to cheer you up!"
 
 
724
  else:
725
  response += f"I’m twirling my flute just for you! Shall we share a Vrindavan adventure today?"
726
  conversation_context["last_response"] = None
727
- conversation_context["last_activity"] = "exploring" if "adventure" in response else None
728
  return response
729
 
 
730
  payload = {
731
  "inputs": f"{SYSTEM_PROMPT} '{user_input}'",
732
  "parameters": model["parameters"]
733
  }
734
- response = requests.post(
735
- model["endpoint"],
736
- headers=headers,
737
- json=payload
738
- )
739
- if response.status_code == 200:
740
  result = response.json()
 
741
  if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
742
  response_text = result[0]["generated_text"].strip()
743
  elif isinstance(result, dict) and "generated_text" in result:
@@ -748,15 +675,14 @@ def get_krishna_response(user_input):
748
  print(f"Unexpected response format from {model['name']}: {result}")
749
  continue
750
  conversation_context["last_response"] = None
751
- conversation_context["last_activity"] = None
752
  return response_text
753
  else:
754
- print(f"Error with {model['name']}: {response.text}")
755
  continue
756
  except Exception as e:
757
  print(f"Error connecting to {model['name']}: {str(e)}")
758
  continue
759
 
 
760
  conversation_context["last_response"] = None
761
- conversation_context["last_activity"] = None
762
  return "Hare Manavi! I seem to be lost in Vrindavan’s magic—let’s try a different tune!"
 
1
  import os
2
  import requests
3
  import random
4
+ import time
5
  from dotenv import load_dotenv
6
  from messages import krishna_blessings, ayush_teasing
7
  from ayush_messages import ayush_surprises
 
111
  "User: 'how are you krishna'\n"
112
  "Response: 'Hare Manavi! I’m as joyful as a peacock dancing in Vrindavan—how about you, my friend?'\n\n"
113
  "User: 'yes'\n"
114
+ "Response: 'Hare Manavi! Wonderful—let’s make today as magical as Vrindavan’s sunsets!'\n\n"
115
+ "User: 'but how'\n"
116
+ "Response: 'Hare Manavi! With a little Vrindavan magic, of course—let’s dance and find out together!'\n\n"
117
+ "User: 'what'\n"
118
+ "Response: 'Hare Manavi! What, you say? Let’s share a Vrindavan tale—shall we?'\n\n"
119
  "Now, respond to the user’s input in a fun, Krishna-like way:"
120
  )
121
 
 
124
  "last_topic": None, # Store the last keyword matched (e.g., "birthday", "riddle")
125
  "message_count": 0, # Track the number of messages to trigger Ayush-teasing every 5th message
126
  "last_response": None, # Track the last response to avoid repetition and enable follow-ups
127
+ "last_yes_response": None # Track the last "yes" response to avoid repetition
128
  }
129
 
 
 
 
 
 
 
 
 
 
130
  def analyze_sentiment(user_input):
131
  """Analyze the sentiment of the user's input using a sentiment analysis model."""
132
  headers = {
 
136
  payload = {
137
  "inputs": user_input
138
  }
139
+ response = make_api_request(
140
+ "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english",
141
+ headers=headers,
142
+ json=payload
143
+ )
144
+ if response and response.status_code == 200:
145
+ result = response.json()
146
+ if isinstance(result, list) and len(result) > 0:
147
+ sentiment = result[0]
148
+ label = sentiment[0]["label"] # "POSITIVE" or "NEGATIVE"
149
+ return label.lower()
150
+ print("Error analyzing sentiment or API failed after retries.")
151
+ return "neutral"
152
+
153
+ def make_api_request(url, headers, payload, retries=3, delay=5):
154
+ """Helper function to make API requests with retry logic."""
155
+ for attempt in range(retries):
156
+ try:
157
+ response = requests.post(url, headers=headers, json=payload)
158
+ if response.status_code == 200:
159
+ return response
160
+ elif response.status_code == 429: # Rate limit
161
+ print(f"Rate limit hit on attempt {attempt + 1}. Retrying after {delay} seconds...")
162
+ time.sleep(delay)
163
+ continue
164
+ else:
165
+ print(f"API error: {response.text}")
166
+ return None
167
+ except Exception as e:
168
+ print(f"API request failed: {str(e)}")
169
+ if attempt < retries - 1:
170
+ time.sleep(delay)
171
+ continue
172
+ return None
173
 
174
  def get_krishna_response(user_input):
175
  """
176
  Generate a response from Little Krishna based on user input.
177
+ - Match user input to predefined messages with a chance to skip for model generation.
178
  - Use sentiment analysis to tailor responses based on Manavi's mood, but only as a fallback.
179
  - Use context to provide follow-up responses (e.g., after "yes").
180
  - Occasionally tease Manavi about Ayush (keyword-based or every 5th message).
 
188
  # Increment message count
189
  conversation_context["message_count"] += 1
190
 
191
+ # Random chance (30%) to skip predefined responses and let the model generate a response
192
+ use_model = random.random() < 0.3
193
+
194
  # Reset context if user starts a new conversation
195
  if "start over" in user_input_lower or "reset" in user_input_lower:
196
  conversation_context["last_topic"] = None
197
  conversation_context["message_count"] = 0
198
  conversation_context["last_response"] = None
199
+ conversation_context["last_yes_response"] = None
200
  return "Hare Manavi! Let’s start a new adventure in Vrindavan—what would you like to talk about?"
201
 
202
  # Check for follow-up responses based on context
203
  if conversation_context["last_response"] == "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?":
204
  if "yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower:
205
+ conversation_context["last_response"] = None # Reset to avoid infinite loop
 
206
  return "Hare Manavi! Let’s play a flute melody by the Yamuna—the peacocks will dance with us!"
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  # Check for Ayush-teasing triggers (keyword-based)
209
+ if "joke" in user_input_lower and not use_model:
210
  conversation_context["last_topic"] = "joke"
211
  conversation_context["last_response"] = None
212
+ # Randomly decide between a Krishna joke and an Ayush-teasing joke
213
  if random.choice([True, False]):
214
  return random.choice(ayush_teasing["joke"])
215
  return krishna_blessings["joke"]
216
+ if "i miss" in user_input_lower or "missing" in user_input_lower and not use_model:
217
  conversation_context["last_topic"] = "missing"
218
  conversation_context["last_response"] = None
 
219
  return random.choice(ayush_teasing["missing"])
220
+ if "bored" in user_input_lower and not use_model:
221
  conversation_context["last_topic"] = "bored"
222
  conversation_context["last_response"] = None
 
223
  return random.choice(ayush_teasing["bored"])
224
+ if "tired" in user_input_lower and not use_model:
225
  conversation_context["last_topic"] = "tired"
226
  conversation_context["last_response"] = None
 
227
  return random.choice(ayush_teasing["tired"])
228
+ if "lonely" in user_input_lower and not use_model:
229
  conversation_context["last_topic"] = "lonely"
230
  conversation_context["last_response"] = None
 
231
  return random.choice(ayush_teasing["lonely"])
232
+ if "manavi" in user_input_lower and not use_model:
233
  conversation_context["last_topic"] = "manavi"
234
  conversation_context["last_response"] = None
 
235
  return random.choice(ayush_teasing["manavi"])
236
+ if ("ayush" in user_input_lower or "krishna talk about ayush" in user_input_lower) and not use_model:
237
  conversation_context["last_topic"] = "ayush"
238
  conversation_context["last_response"] = None
 
239
  return random.choice(ayush_teasing["ayush"])
240
 
241
  # Trigger for "chat with you"
242
+ if ("chat with you" in user_input_lower or "want to chat" in user_input_lower) and not use_model:
243
  conversation_context["last_topic"] = "chat_with_you"
244
  conversation_context["last_response"] = None
 
245
  return krishna_blessings["chat_with_you"]
246
 
247
  # Every 5th message, randomly trigger an Ayush-teasing message (if no keyword match)
248
+ if conversation_context["message_count"] % 5 == 0 and not use_model:
249
+ # Randomly select a category from ayush_teasing
250
  category = random.choice(list(ayush_teasing.keys()))
251
  conversation_context["last_response"] = None
 
252
  return random.choice(ayush_teasing[category])
253
 
254
  # Existing keyword mappings for krishna_blessings and ayush_surprises
255
+ if ("hello" in user_input_lower or "hi" in user_input_lower or "hii" in user_input_lower) and not use_model:
256
  conversation_context["last_topic"] = "greeting"
257
  conversation_context["last_response"] = None
 
258
  return krishna_blessings["greeting"]
259
+ if "good morning" in user_input_lower and not use_model:
260
  conversation_context["last_topic"] = "greeting"
261
  conversation_context["last_response"] = None
 
262
  return krishna_blessings["good_morning"]
263
+ if "good afternoon" in user_input_lower and not use_model:
264
  conversation_context["last_topic"] = "greeting"
265
  conversation_context["last_response"] = None
 
266
  return krishna_blessings["good_afternoon"]
267
+ if "good evening" in user_input_lower and not use_model:
268
  conversation_context["last_topic"] = "greeting"
269
  conversation_context["last_response"] = None
 
270
  return krishna_blessings["good_evening"]
271
+ if "hey" in user_input_lower and not use_model:
272
  conversation_context["last_topic"] = "greeting"
273
  conversation_context["last_response"] = None
 
274
  return krishna_blessings["hey"]
275
+ if "howdy" in user_input_lower and not use_model:
276
  conversation_context["last_topic"] = "greeting"
277
  conversation_context["last_response"] = None
 
278
  return krishna_blessings["howdy"]
279
+ if "namaste" in user_input_lower and not use_model:
280
  conversation_context["last_topic"] = "greeting"
281
  conversation_context["last_response"] = None
 
282
  return krishna_blessings["namaste"]
283
+ if "welcome" in user_input_lower and not use_model:
284
  conversation_context["last_topic"] = "greeting"
285
  conversation_context["last_response"] = None
 
286
  return krishna_blessings["welcome"]
287
 
288
+ 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 or "what are you doing" in user_input_lower) and not use_model:
289
  conversation_context["last_topic"] = "identity"
290
  conversation_context["last_response"] = None
 
291
  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?"
292
 
293
+ if "how are you" in user_input_lower and not use_model:
294
  conversation_context["last_topic"] = "how_are_you"
295
  conversation_context["last_response"] = None
 
296
  return "Hare Manavi! I’m as joyful as a peacock dancing in Vrindavan—how about you, my friend?"
297
 
298
+ # Handle "how" questions (including typos like "hoe")
299
+ if ("how" in user_input_lower or "hoe" in user_input_lower) and not use_model:
300
+ conversation_context["last_topic"] = "how"
301
+ conversation_context["last_response"] = None
302
+ return "Hare Manavi! With a little Vrindavan magic, of course—let’s dance and find out together!"
303
+
304
+ # Specific handling for "what"
305
+ if "what" in user_input_lower and not ("what are you" in user_input_lower or "what are you doing" in user_input_lower) and not use_model:
306
+ conversation_context["last_topic"] = "what"
307
+ conversation_context["last_response"] = None
308
+ return "Hare Manavi! What, you say? Let’s share a Vrindavan tale—shall we?"
309
+
310
+ # Varied responses for "yes", avoiding repetition
311
+ yes_responses = [
312
+ "Hare Manavi! Wonderful—let’s make today as magical as Vrindavan’s sunsets!",
313
+ "Hare Manavi! Great—shall we chase some butterflies by the Yamuna?",
314
+ "Hare Manavi! Perfect—let’s share some butter under the kadamba tree!",
315
+ "Hare Manavi! Lovely—how about a dance with the gopis in Vrindavan’s fields?"
316
+ ]
317
+ if ("yes" in user_input_lower or "okay" in user_input_lower or "sure" in user_input_lower) and not use_model:
318
+ # If no context for "yes", provide a varied positive response
319
  conversation_context["last_topic"] = "yes"
320
  conversation_context["last_response"] = None
321
+ # Avoid repeating the last "yes" response
322
+ available_responses = [resp for resp in yes_responses if resp != conversation_context["last_yes_response"]]
323
+ if not available_responses: # If all responses have been used, reset
324
+ available_responses = yes_responses
325
+ selected_response = random.choice(available_responses)
326
+ conversation_context["last_yes_response"] = selected_response
327
+ return selected_response
328
 
329
+ if ("play" in user_input_lower or "fun" in user_input_lower) and not use_model:
330
  conversation_context["last_topic"] = "playful"
331
  conversation_context["last_response"] = None
 
332
  return krishna_blessings["playful"]
333
+ if "dance" in user_input_lower and not use_model:
334
  conversation_context["last_topic"] = "dance"
335
  conversation_context["last_response"] = None
 
336
  return krishna_blessings["dance"]
337
+ if "flute" in user_input_lower and not use_model:
338
  conversation_context["last_topic"] = "flute"
339
  conversation_context["last_response"] = None
 
340
  return krishna_blessings["flute"]
341
+ if "butter" in user_input_lower and not use_model:
342
  conversation_context["last_topic"] = "butter"
343
  conversation_context["last_response"] = None
 
344
  return krishna_blessings["butter"]
345
+ if ("mischief" in user_input_lower or "prank" in user_input_lower) and not use_model:
346
  conversation_context["last_topic"] = "mischief"
347
  conversation_context["last_response"] = None
 
348
  return krishna_blessings["mischief"]
349
+ if ("chase" in user_input_lower or "run" in user_input_lower) and not use_model:
350
  conversation_context["last_topic"] = "chase"
351
  conversation_context["last_response"] = None
 
352
  return krishna_blessings["chase"]
353
+ if "giggle" in user_input_lower and not use_model:
354
  conversation_context["last_topic"] = "giggle"
355
  conversation_context["last_response"] = None
 
356
  return krishna_blessings["giggle"]
357
+ if "swing" in user_input_lower and not use_model:
358
  conversation_context["last_topic"] = "swing"
359
  conversation_context["last_response"] = None
 
360
  return krishna_blessings["swing"]
361
 
362
+ if "shy" in user_input_lower and not use_model:
363
  conversation_context["last_topic"] = "shy"
364
  conversation_context["last_response"] = None
 
365
  return krishna_blessings["shy"]
366
+ if ("quiet" in user_input_lower or "calm" in user_input_lower) and not use_model:
367
  conversation_context["last_topic"] = "quiet"
368
  conversation_context["last_response"] = None
 
369
  return krishna_blessings["quiet"]
370
+ if ("peace" in user_input_lower or "serene" in user_input_lower) and not use_model:
371
  conversation_context["last_topic"] = "peace"
372
  conversation_context["last_response"] = None
 
373
  return krishna_blessings["peace"]
374
+ if ("still" in user_input_lower or "gentle" in user_input_lower) and not use_model:
375
  conversation_context["last_topic"] = "still"
376
  conversation_context["last_response"] = None
 
377
  return krishna_blessings["still"]
378
+ if ("thoughtful" in user_input_lower or "reflect" in user_input_lower) and not use_model:
379
  conversation_context["last_topic"] = "thoughtful"
380
  conversation_context["last_response"] = None
 
381
  return krishna_blessings["thoughtful"]
382
 
383
+ if "funny" in user_input_lower and not use_model:
384
  conversation_context["last_topic"] = "joke"
385
  conversation_context["last_response"] = None
 
386
  return krishna_blessings["funny"]
387
+ if ("laugh" in user_input_lower or "giggle" in user_input_lower) and not use_model:
388
  conversation_context["last_topic"] = "joke"
389
  conversation_context["last_response"] = None
 
390
  return krishna_blessings["giggle_joke"]
391
+ if "silly" in user_input_lower and not use_model:
392
  conversation_context["last_topic"] = "joke"
393
  conversation_context["last_response"] = None
 
394
  return krishna_blessings["silly"]
395
+ if "butter joke" in user_input_lower and not use_model:
396
  conversation_context["last_topic"] = "joke"
397
  conversation_context["last_response"] = None
 
398
  return krishna_blessings["butter_joke"]
399
+ if "cow joke" in user_input_lower and not use_model:
400
  conversation_context["last_topic"] = "joke"
401
  conversation_context["last_response"] = None
 
402
  return krishna_blessings["cow_joke"]
403
+ if "flute joke" in user_input_lower and not use_model:
404
  conversation_context["last_topic"] = "joke"
405
  conversation_context["last_response"] = None
 
406
  return krishna_blessings["flute_joke"]
407
+ if "dance joke" in user_input_lower and not use_model:
408
  conversation_context["last_topic"] = "joke"
409
  conversation_context["last_response"] = None
 
410
  return krishna_blessings["dance_joke"]
411
+ if "mischief joke" in user_input_lower and not use_model:
412
  conversation_context["last_topic"] = "joke"
413
  conversation_context["last_response"] = None
 
414
  return krishna_blessings["mischief_joke"]
415
 
416
+ if ("riddle" in user_input_lower or "puzzle" in user_input_lower) and not use_model:
417
  conversation_context["last_topic"] = "riddle"
418
  conversation_context["last_response"] = None
 
419
  return krishna_blessings["riddle"]
420
+ if ("mystery" in user_input_lower or "enigma" in user_input_lower) and not use_model:
421
  conversation_context["last_topic"] = "riddle"
422
  conversation_context["last_response"] = None
 
423
  return krishna_blessings["mystery"]
424
+ if "question" in user_input_lower and not use_model:
425
  conversation_context["last_topic"] = "riddle"
426
  conversation_context["last_response"] = None
 
427
  return krishna_blessings["question"]
428
 
429
+ if "birthday" in user_input_lower and not use_model:
430
  conversation_context["last_topic"] = "birthday"
431
  conversation_context["last_response"] = None
 
432
  return ayush_surprises["birthday"]
433
+ if "happy birthday" in user_input_lower and not use_model:
434
  conversation_context["last_topic"] = "birthday"
435
  conversation_context["last_response"] = None
 
436
  return krishna_blessings["happy_birthday"]
437
+ if "birthday wish" in user_input_lower and not use_model:
438
  conversation_context["last_topic"] = "birthday"
439
  conversation_context["last_response"] = None
 
440
  return krishna_blessings["birthday_wish"]
441
+ if "birthday blessing" in user_input_lower and not use_model:
442
  conversation_context["last_topic"] = "birthday"
443
  conversation_context["last_response"] = None
 
444
  return krishna_blessings["birthday_blessing"]
445
+ if "birthday dance" in user_input_lower and not use_model:
446
  conversation_context["last_topic"] = "birthday"
447
  conversation_context["last_response"] = None
 
448
  return krishna_blessings["birthday_dance"]
449
+ if "birthday song" in user_input_lower and not use_model:
450
  conversation_context["last_topic"] = "birthday"
451
  conversation_context["last_response"] = None
 
452
  return krishna_blessings["birthday_song"]
453
+ if "birthday gift" in user_input_lower and not use_model:
454
  conversation_context["last_topic"] = "birthday"
455
  conversation_context["last_response"] = None
 
456
  return krishna_blessings["birthday_gift"]
457
+ if "birthday smile" in user_input_lower and not use_model:
458
  conversation_context["last_topic"] = "birthday"
459
  conversation_context["last_response"] = None
 
460
  return krishna_blessings["birthday_smile"]
461
+ if "birthday love" in user_input_lower and not use_model:
462
  conversation_context["last_topic"] = "birthday"
463
  conversation_context["last_response"] = None
 
464
  return krishna_blessings["birthday_love"]
465
+ if "birthday magic" in user_input_lower and not use_model:
466
  conversation_context["last_topic"] = "birthday"
467
  conversation_context["last_response"] = None
 
468
  return krishna_blessings["birthday_magic"]
469
 
470
+ if ("wisdom" in user_input_lower or "advice" in user_input_lower) and not use_model:
471
  conversation_context["last_topic"] = "wisdom"
472
  conversation_context["last_response"] = None
 
473
  return krishna_blessings["wisdom"]
474
+ if ("lesson" in user_input_lower or "truth" in user_input_lower) and not use_model:
475
  conversation_context["last_topic"] = "wisdom"
476
  conversation_context["last_response"] = None
 
477
  return krishna_blessings["lesson"]
478
+ if "kindness" in user_input_lower and not use_model:
479
  conversation_context["last_topic"] = "wisdom"
480
  conversation_context["last_response"] = None
 
481
  return krishna_blessings["kindness"]
482
+ if "patience" in user_input_lower and not use_model:
483
  conversation_context["last_topic"] = "wisdom"
484
  conversation_context["last_response"] = None
 
485
  return krishna_blessings["patience"]
486
+ if "courage" in user_input_lower and not use_model:
487
  conversation_context["last_topic"] = "wisdom"
488
  conversation_context["last_response"] = None
 
489
  return krishna_blessings["courage"]
490
+ if "joy" in user_input_lower and not use_model:
491
  conversation_context["last_topic"] = "wisdom"
492
  conversation_context["last_response"] = None
 
493
  return krishna_blessings["joy"]
494
+ if "friendship" in user_input_lower and not use_model:
495
  conversation_context["last_topic"] = "wisdom"
496
  conversation_context["last_response"] = None
 
497
  return krishna_blessings["friendship"]
498
+ if "love" in user_input_lower and not use_model:
499
  conversation_context["last_topic"] = "wisdom"
500
  conversation_context["last_response"] = None
 
501
  return krishna_blessings["love"]
502
 
503
+ if ("nature" in user_input_lower or "vrindavan" in user_input_lower) and not use_model:
504
  conversation_context["last_topic"] = "nature"
505
  conversation_context["last_response"] = None
 
506
  return krishna_blessings["nature"]
507
+ if ("yamuna" in user_input_lower or "river" in user_input_lower) and not use_model:
508
  conversation_context["last_topic"] = "nature"
509
  conversation_context["last_response"] = None
 
510
  return krishna_blessings["yamuna"]
511
+ if "peacock" in user_input_lower and not use_model:
512
  conversation_context["last_topic"] = "nature"
513
  conversation_context["last_response"] = None
 
514
  return krishna_blessings["peacock"]
515
+ if "cow" in user_input_lower and not use_model:
516
  conversation_context["last_topic"] = "nature"
517
  conversation_context["last_response"] = None
 
518
  return krishna_blessings["cow"]
519
+ if "flower" in user_input_lower and not use_model:
520
  conversation_context["last_topic"] = "nature"
521
  conversation_context["last_response"] = None
 
522
  return krishna_blessings["flower"]
523
+ if "tree" in user_input_lower and not use_model:
524
  conversation_context["last_topic"] = "nature"
525
  conversation_context["last_response"] = None
 
526
  return krishna_blessings["tree"]
527
+ if "forest" in user_input_lower and not use_model:
528
  conversation_context["last_topic"] = "nature"
529
  conversation_context["last_response"] = None
 
530
  return krishna_blessings["forest"]
531
+ if "bird" in user_input_lower and not use_model:
532
  conversation_context["last_topic"] = "nature"
533
  conversation_context["last_response"] = None
 
534
  return krishna_blessings["bird"]
535
+ if "sunset" in user_input_lower and not use_model:
536
  conversation_context["last_topic"] = "nature"
537
  conversation_context["last_response"] = None
 
538
  return krishna_blessings["sunset"]
539
 
540
+ if ("encourage" in user_input_lower or "cheer" in user_input_lower) and not use_model:
541
  conversation_context["last_topic"] = "encourage"
542
  conversation_context["last_response"] = None
 
543
  return krishna_blessings["encourage"]
544
+ if ("support" in user_input_lower or "uplift" in user_input_lower) and not use_model:
545
  conversation_context["last_topic"] = "encourage"
546
  conversation_context["last_response"] = None
 
547
  return krishna_blessings["support"]
548
+ if ("inspire" in user_input_lower or "motivate" in user_input_lower) and not use_model:
549
  conversation_context["last_topic"] = "encourage"
550
  conversation_context["last_response"] = None
 
551
  return krishna_blessings["inspire"]
552
+ if "strength" in user_input_lower and not use_model:
553
  conversation_context["last_topic"] = "encourage"
554
  conversation_context["last_response"] = None
 
555
  return krishna_blessings["strength"]
556
+ if "hope" in user_input_lower and not use_model:
557
  conversation_context["last_topic"] = "encourage"
558
  conversation_context["last_response"] = None
 
559
  return krishna_blessings["hope"]
560
+ if "believe" in user_input_lower and not use_model:
561
  conversation_context["last_topic"] = "encourage"
562
  conversation_context["last_response"] = None
 
563
  return krishna_blessings["believe"]
564
+ if "shine" in user_input_lower and not use_model:
565
  conversation_context["last_topic"] = "encourage"
566
  conversation_context["last_response"] = None
 
567
  return krishna_blessings["shine"]
568
 
569
+ if "friend" in user_input_lower and not use_model:
570
  conversation_context["last_topic"] = "friend"
571
  conversation_context["last_response"] = None
 
572
  return krishna_blessings["friend"]
573
+ if "smile" in user_input_lower and not use_model:
574
  conversation_context["last_topic"] = "smile"
575
  conversation_context["last_response"] = None
 
576
  return krishna_blessings["smile"]
577
+ if "magic" in user_input_lower and not use_model:
578
  conversation_context["last_topic"] = "magic"
579
  conversation_context["last_response"] = None
 
580
  return krishna_blessings["magic"]
581
+ if "adventure" in user_input_lower and not use_model:
582
  conversation_context["last_topic"] = "adventure"
583
  conversation_context["last_response"] = None
 
584
  return krishna_blessings["adventure"]
585
+ if "song" in user_input_lower and not use_model:
586
  conversation_context["last_topic"] = "song"
587
  conversation_context["last_response"] = None
 
588
  return krishna_blessings["song"]
589
+ if "dream" in user_input_lower and not use_model:
590
  conversation_context["last_topic"] = "dream"
591
  conversation_context["last_response"] = None
 
592
  return krishna_blessings["dream"]
593
+ if "story" in user_input_lower and not use_model:
594
  conversation_context["last_topic"] = "story"
595
  conversation_context["last_response"] = None
 
596
  return krishna_blessings["story"]
597
+ if "surprise" in user_input_lower and not use_model:
598
  conversation_context["last_topic"] = "surprise"
599
  conversation_context["last_response"] = None
 
600
  return krishna_blessings["surprise"]
601
+ if "celebrate" in user_input_lower and not use_model:
602
  conversation_context["last_topic"] = "celebrate"
603
  conversation_context["last_response"] = None
 
604
  return krishna_blessings["celebrate"]
605
+ if "blessing" in user_input_lower and not use_model:
606
  conversation_context["last_topic"] = "blessing"
607
  conversation_context["last_response"] = None
 
608
  return krishna_blessings["blessing"]
609
 
610
+ if conversation_context["last_topic"] and not use_model:
611
  last_topic = conversation_context["last_topic"]
612
  if last_topic in krishna_blessings:
613
  conversation_context["last_response"] = None
 
614
  return krishna_blessings[last_topic] + " What else would you like to talk about, Manavi?"
615
 
616
  # Sentiment-based responses (only as a fallback, and avoid repetition)
617
+ if sentiment == "negative" and "sad" not in user_input_lower and conversation_context["last_response"] != "Hare Manavi! I see a little cloud over your heart—let’s dance by the Yamuna to bring back your smile!" and not use_model:
618
  response = "Hare Manavi! I see a little cloud over your heart—let’s dance by the Yamuna to bring back your smile!"
619
  conversation_context["last_response"] = response
 
620
  return response
621
+ if sentiment == "positive" and conversation_context["last_response"] != "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?" and not use_model:
622
  response = "Hare Manavi! Your joy lights up Vrindavan—shall we celebrate with a flute melody?"
623
  conversation_context["last_response"] = response
 
624
  return response
625
 
626
+ # Fallback to multiple open-source AI models if no keywords match or if use_model is True
627
+ # Shuffle the models to try them in random order
628
  models_to_try = AI_MODELS.copy()
629
  random.shuffle(models_to_try)
630
 
 
635
 
636
  for model in models_to_try:
637
  try:
638
+ # Special case for Grok by xAI (simulated directly)
639
  if model["name"] == "Grok by xAI":
640
+ # Simulate Grok's response (I, Grok, will generate the response directly)
641
  response = (
642
  f"Hare Manavi! I’m Little Krishna, speaking through Grok by xAI. "
643
  f"Let me answer in my playful way: "
644
  )
645
+ # Generate a Krishna-like response based on the input
646
  if "color" in user_input_lower:
647
  response += "I love the golden yellow of Vrindavan’s butter—it’s as sweet as your smile! What’s your favorite color?"
648
  elif "weather" in user_input_lower:
649
  response += "The Vrindavan sky is as clear as the Yamuna today—perfect for a flute melody! How’s your weather?"
650
  elif "sad" in user_input_lower:
651
  response += "Oh, my dear gopi, don’t be sad—let’s dance by the Yamuna, and I’ll play a tune to cheer you up!"
652
+ elif "what" in user_input_lower:
653
+ response += "What, you say? Let’s share a Vrindavan tale—shall we?"
654
  else:
655
  response += f"I’m twirling my flute just for you! Shall we share a Vrindavan adventure today?"
656
  conversation_context["last_response"] = None
 
657
  return response
658
 
659
+ # For other models, use the Hugging Face Inference API with retry logic
660
  payload = {
661
  "inputs": f"{SYSTEM_PROMPT} '{user_input}'",
662
  "parameters": model["parameters"]
663
  }
664
+ response = make_api_request(model["endpoint"], headers=headers, json=payload)
665
+ if response and response.status_code == 200:
 
 
 
 
666
  result = response.json()
667
+ # Handle different response formats based on the model
668
  if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
669
  response_text = result[0]["generated_text"].strip()
670
  elif isinstance(result, dict) and "generated_text" in result:
 
675
  print(f"Unexpected response format from {model['name']}: {result}")
676
  continue
677
  conversation_context["last_response"] = None
 
678
  return response_text
679
  else:
680
+ print(f"Error with {model['name']}: {response.text if response else 'No response'}")
681
  continue
682
  except Exception as e:
683
  print(f"Error connecting to {model['name']}: {str(e)}")
684
  continue
685
 
686
+ # If all models fail, return a default message
687
  conversation_context["last_response"] = None
 
688
  return "Hare Manavi! I seem to be lost in Vrindavan’s magic—let’s try a different tune!"