Update chatbot.py
Browse files- chatbot.py +277 -11
chatbot.py
CHANGED
@@ -8,19 +8,285 @@ from ayush_messages import ayush_surprises
|
|
8 |
load_dotenv()
|
9 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
def get_krishna_response(user_input):
|
12 |
"""
|
13 |
Generate a response from Little Krishna based on user input.
|
14 |
-
-
|
15 |
-
-
|
|
|
16 |
"""
|
17 |
-
|
18 |
-
|
19 |
-
if
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
return krishna_blessings["greeting"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
24 |
headers = {
|
25 |
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
26 |
"Content-Type": "application/json"
|
@@ -28,8 +294,8 @@ def get_krishna_response(user_input):
|
|
28 |
payload = {
|
29 |
"inputs": f"You are Little Krishna, playful and wise. Respond to this in a fun, Krishna-like way: '{user_input}'",
|
30 |
"parameters": {
|
31 |
-
"max_length":
|
32 |
-
"temperature": 0.
|
33 |
}
|
34 |
}
|
35 |
try:
|
@@ -43,7 +309,7 @@ def get_krishna_response(user_input):
|
|
43 |
return result[0]["generated_text"].strip()
|
44 |
else:
|
45 |
print(f"Error with Gemma: {response.text}")
|
46 |
-
return "
|
47 |
except Exception as e:
|
48 |
print(f"Error connecting to Hugging Face Inference API: {str(e)}")
|
49 |
-
return "Hare
|
|
|
8 |
load_dotenv()
|
9 |
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):
|
17 |
"""
|
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"]
|
34 |
+
if "good morning" in user_input_lower:
|
35 |
+
conversation_context["last_topic"] = "greeting"
|
36 |
+
return krishna_blessings["good_morning"]
|
37 |
+
if "good afternoon" in user_input_lower:
|
38 |
+
conversation_context["last_topic"] = "greeting"
|
39 |
+
return krishna_blessings["good_afternoon"]
|
40 |
+
if "good evening" in user_input_lower:
|
41 |
+
conversation_context["last_topic"] = "greeting"
|
42 |
+
return krishna_blessings["good_evening"]
|
43 |
+
if "hey" in user_input_lower:
|
44 |
+
conversation_context["last_topic"] = "greeting"
|
45 |
+
return krishna_blessings["hey"]
|
46 |
+
if "howdy" in user_input_lower:
|
47 |
+
conversation_context["last_topic"] = "greeting"
|
48 |
+
return krishna_blessings["howdy"]
|
49 |
+
if "namaste" in user_input_lower:
|
50 |
+
conversation_context["last_topic"] = "greeting"
|
51 |
+
return krishna_blessings["namaste"]
|
52 |
+
if "welcome" in user_input_lower:
|
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"]
|
65 |
+
if "dance" in user_input_lower:
|
66 |
+
conversation_context["last_topic"] = "dance"
|
67 |
+
return krishna_blessings["dance"]
|
68 |
+
if "flute" in user_input_lower:
|
69 |
+
conversation_context["last_topic"] = "flute"
|
70 |
+
return krishna_blessings["flute"]
|
71 |
+
if "butter" in user_input_lower:
|
72 |
+
conversation_context["last_topic"] = "butter"
|
73 |
+
return krishna_blessings["butter"]
|
74 |
+
if "mischief" in user_input_lower or "prank" in user_input_lower:
|
75 |
+
conversation_context["last_topic"] = "mischief"
|
76 |
+
return krishna_blessings["mischief"]
|
77 |
+
if "chase" in user_input_lower or "run" in user_input_lower:
|
78 |
+
conversation_context["last_topic"] = "chase"
|
79 |
+
return krishna_blessings["chase"]
|
80 |
+
if "giggle" in user_input_lower:
|
81 |
+
conversation_context["last_topic"] = "giggle"
|
82 |
+
return krishna_blessings["giggle"]
|
83 |
+
if "swing" in user_input_lower:
|
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"]
|
91 |
+
if "quiet" in user_input_lower or "calm" in user_input_lower:
|
92 |
+
conversation_context["last_topic"] = "quiet"
|
93 |
+
return krishna_blessings["quiet"]
|
94 |
+
if "peace" in user_input_lower or "serene" in user_input_lower:
|
95 |
+
conversation_context["last_topic"] = "peace"
|
96 |
+
return krishna_blessings["peace"]
|
97 |
+
if "still" in user_input_lower or "gentle" in user_input_lower:
|
98 |
+
conversation_context["last_topic"] = "still"
|
99 |
+
return krishna_blessings["still"]
|
100 |
+
if "thoughtful" in user_input_lower or "reflect" in user_input_lower:
|
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"]
|
111 |
+
if "silly" in user_input_lower:
|
112 |
+
conversation_context["last_topic"] = "joke"
|
113 |
+
return krishna_blessings["silly"]
|
114 |
+
if "butter joke" in user_input_lower:
|
115 |
+
conversation_context["last_topic"] = "joke"
|
116 |
+
return krishna_blessings["butter_joke"]
|
117 |
+
if "cow joke" in user_input_lower:
|
118 |
+
conversation_context["last_topic"] = "joke"
|
119 |
+
return krishna_blessings["cow_joke"]
|
120 |
+
if "flute joke" in user_input_lower:
|
121 |
+
conversation_context["last_topic"] = "joke"
|
122 |
+
return krishna_blessings["flute_joke"]
|
123 |
+
if "dance joke" in user_input_lower:
|
124 |
+
conversation_context["last_topic"] = "joke"
|
125 |
+
return krishna_blessings["dance_joke"]
|
126 |
+
if "mischief joke" in user_input_lower:
|
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"]
|
134 |
+
if "mystery" in user_input_lower or "enigma" in user_input_lower:
|
135 |
+
conversation_context["last_topic"] = "riddle"
|
136 |
+
return krishna_blessings["mystery"]
|
137 |
+
if "question" in user_input_lower:
|
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"]
|
145 |
+
if "happy birthday" in user_input_lower:
|
146 |
+
conversation_context["last_topic"] = "birthday"
|
147 |
+
return krishna_blessings["happy_birthday"]
|
148 |
+
if "birthday wish" in user_input_lower:
|
149 |
+
conversation_context["last_topic"] = "birthday"
|
150 |
+
return krishna_blessings["birthday_wish"]
|
151 |
+
if "birthday blessing" in user_input_lower:
|
152 |
+
conversation_context["last_topic"] = "birthday"
|
153 |
+
return krishna_blessings["birthday_blessing"]
|
154 |
+
if "birthday dance" in user_input_lower:
|
155 |
+
conversation_context["last_topic"] = "birthday"
|
156 |
+
return krishna_blessings["birthday_dance"]
|
157 |
+
if "birthday song" in user_input_lower:
|
158 |
+
conversation_context["last_topic"] = "birthday"
|
159 |
+
return krishna_blessings["birthday_song"]
|
160 |
+
if "birthday gift" in user_input_lower:
|
161 |
+
conversation_context["last_topic"] = "birthday"
|
162 |
+
return krishna_blessings["birthday_gift"]
|
163 |
+
if "birthday smile" in user_input_lower:
|
164 |
+
conversation_context["last_topic"] = "birthday"
|
165 |
+
return krishna_blessings["birthday_smile"]
|
166 |
+
if "birthday love" in user_input_lower:
|
167 |
+
conversation_context["last_topic"] = "birthday"
|
168 |
+
return krishna_blessings["birthday_love"]
|
169 |
+
if "birthday magic" in user_input_lower:
|
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"]
|
177 |
+
if "lesson" in user_input_lower or "truth" in user_input_lower:
|
178 |
+
conversation_context["last_topic"] = "wisdom"
|
179 |
+
return krishna_blessings["lesson"]
|
180 |
+
if "kindness" in user_input_lower:
|
181 |
+
conversation_context["last_topic"] = "wisdom"
|
182 |
+
return krishna_blessings["kindness"]
|
183 |
+
if "patience" in user_input_lower:
|
184 |
+
conversation_context["last_topic"] = "wisdom"
|
185 |
+
return krishna_blessings["patience"]
|
186 |
+
if "courage" in user_input_lower:
|
187 |
+
conversation_context["last_topic"] = "wisdom"
|
188 |
+
return krishna_blessings["courage"]
|
189 |
+
if "joy" in user_input_lower:
|
190 |
+
conversation_context["last_topic"] = "wisdom"
|
191 |
+
return krishna_blessings["joy"]
|
192 |
+
if "friendship" in user_input_lower:
|
193 |
+
conversation_context["last_topic"] = "wisdom"
|
194 |
+
return krishna_blessings["friendship"]
|
195 |
+
if "love" in user_input_lower:
|
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"]
|
203 |
+
if "yamuna" in user_input_lower or "river" in user_input_lower:
|
204 |
+
conversation_context["last_topic"] = "nature"
|
205 |
+
return krishna_blessings["yamuna"]
|
206 |
+
if "peacock" in user_input_lower:
|
207 |
+
conversation_context["last_topic"] = "nature"
|
208 |
+
return krishna_blessings["peacock"]
|
209 |
+
if "cow" in user_input_lower:
|
210 |
+
conversation_context["last_topic"] = "nature"
|
211 |
+
return krishna_blessings["cow"]
|
212 |
+
if "flower" in user_input_lower:
|
213 |
+
conversation_context["last_topic"] = "nature"
|
214 |
+
return krishna_blessings["flower"]
|
215 |
+
if "tree" in user_input_lower:
|
216 |
+
conversation_context["last_topic"] = "nature"
|
217 |
+
return krishna_blessings["tree"]
|
218 |
+
if "forest" in user_input_lower:
|
219 |
+
conversation_context["last_topic"] = "nature"
|
220 |
+
return krishna_blessings["forest"]
|
221 |
+
if "bird" in user_input_lower:
|
222 |
+
conversation_context["last_topic"] = "nature"
|
223 |
+
return krishna_blessings["bird"]
|
224 |
+
if "sunset" in user_input_lower:
|
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"]
|
232 |
+
if "support" in user_input_lower or "uplift" in user_input_lower:
|
233 |
+
conversation_context["last_topic"] = "encourage"
|
234 |
+
return krishna_blessings["support"]
|
235 |
+
if "inspire" in user_input_lower or "motivate" in user_input_lower:
|
236 |
+
conversation_context["last_topic"] = "encourage"
|
237 |
+
return krishna_blessings["inspire"]
|
238 |
+
if "strength" in user_input_lower:
|
239 |
+
conversation_context["last_topic"] = "encourage"
|
240 |
+
return krishna_blessings["strength"]
|
241 |
+
if "hope" in user_input_lower:
|
242 |
+
conversation_context["last_topic"] = "encourage"
|
243 |
+
return krishna_blessings["hope"]
|
244 |
+
if "believe" in user_input_lower:
|
245 |
+
conversation_context["last_topic"] = "encourage"
|
246 |
+
return krishna_blessings["believe"]
|
247 |
+
if "shine" in user_input_lower:
|
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"]
|
255 |
+
if "smile" in user_input_lower:
|
256 |
+
conversation_context["last_topic"] = "smile"
|
257 |
+
return krishna_blessings["smile"]
|
258 |
+
if "magic" in user_input_lower:
|
259 |
+
conversation_context["last_topic"] = "magic"
|
260 |
+
return krishna_blessings["magic"]
|
261 |
+
if "adventure" in user_input_lower:
|
262 |
+
conversation_context["last_topic"] = "adventure"
|
263 |
+
return krishna_blessings["adventure"]
|
264 |
+
if "song" in user_input_lower:
|
265 |
+
conversation_context["last_topic"] = "song"
|
266 |
+
return krishna_blessings["song"]
|
267 |
+
if "dream" in user_input_lower:
|
268 |
+
conversation_context["last_topic"] = "dream"
|
269 |
+
return krishna_blessings["dream"]
|
270 |
+
if "story" in user_input_lower:
|
271 |
+
conversation_context["last_topic"] = "story"
|
272 |
+
return krishna_blessings["story"]
|
273 |
+
if "surprise" in user_input_lower:
|
274 |
+
conversation_context["last_topic"] = "surprise"
|
275 |
+
return krishna_blessings["surprise"]
|
276 |
+
if "celebrate" in user_input_lower:
|
277 |
+
conversation_context["last_topic"] = "celebrate"
|
278 |
+
return krishna_blessings["celebrate"]
|
279 |
+
if "blessing" in user_input_lower:
|
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:
|
287 |
+
return krishna_blessings[last_topic] + " What else would you like to talk about, Manavi?"
|
288 |
|
289 |
+
# Fallback to Hugging Face Inference API if no keywords match
|
290 |
headers = {
|
291 |
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
292 |
"Content-Type": "application/json"
|
|
|
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:
|
|
|
309 |
return result[0]["generated_text"].strip()
|
310 |
else:
|
311 |
print(f"Error with Gemma: {response.text}")
|
312 |
+
return "Hare Manavi! My flute got a bit shy—let’s try something else, shall we?"
|
313 |
except Exception as e:
|
314 |
print(f"Error connecting to Hugging Face Inference API: {str(e)}")
|
315 |
+
return "Hare Manavi! I seem to be lost in Vrindavan’s magic—let’s try a different tune!"
|