File size: 14,381 Bytes
381c527 01e2279 381c527 e75ce3b 01e2279 e75ce3b 01e2279 e75ce3b 01e2279 e75ce3b 01e2279 e75ce3b 01e2279 e75ce3b ed107b6 01e2279 381c527 01e2279 381c527 01e2279 381c527 01e2279 e75ce3b 381c527 01e2279 e75ce3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
import os
import requests
from dotenv import load_dotenv
from messages import krishna_blessings
from ayush_messages import ayush_surprises
# Load environment variables (Hugging Face Space secrets)
load_dotenv()
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
# Simple context tracking (e.g., last topic discussed)
conversation_context = {
"last_topic": None # Store the last keyword matched (e.g., "birthday", "riddle")
}
def get_krishna_response(user_input):
"""
Generate a response from Little Krishna based on user input.
- Match user input to predefined messages in krishna_blessings or ayush_surprises using keywords.
- Use context to provide more coherent responses.
- Fall back to Hugging Face's Inference API for unmatched inputs.
"""
user_input_lower = user_input.lower().strip()
# Reset context if user starts a new conversation
if "start over" in user_input_lower or "reset" in user_input_lower:
conversation_context["last_topic"] = None
return "Hare Manavi! Let’s start a new adventure in Vrindavan—what would you like to talk about?"
# Greetings
if "hello" in user_input_lower or "hi" in user_input_lower or "hii" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["greeting"]
if "good morning" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["good_morning"]
if "good afternoon" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["good_afternoon"]
if "good evening" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["good_evening"]
if "hey" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["hey"]
if "howdy" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["howdy"]
if "namaste" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["namaste"]
if "welcome" in user_input_lower:
conversation_context["last_topic"] = "greeting"
return krishna_blessings["welcome"]
# Identity Questions (e.g., "Who are you")
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:
conversation_context["last_topic"] = "identity"
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?"
# Playful Responses
if "play" in user_input_lower or "fun" in user_input_lower:
conversation_context["last_topic"] = "playful"
return krishna_blessings["playful"]
if "dance" in user_input_lower:
conversation_context["last_topic"] = "dance"
return krishna_blessings["dance"]
if "flute" in user_input_lower:
conversation_context["last_topic"] = "flute"
return krishna_blessings["flute"]
if "butter" in user_input_lower:
conversation_context["last_topic"] = "butter"
return krishna_blessings["butter"]
if "mischief" in user_input_lower or "prank" in user_input_lower:
conversation_context["last_topic"] = "mischief"
return krishna_blessings["mischief"]
if "chase" in user_input_lower or "run" in user_input_lower:
conversation_context["last_topic"] = "chase"
return krishna_blessings["chase"]
if "giggle" in user_input_lower:
conversation_context["last_topic"] = "giggle"
return krishna_blessings["giggle"]
if "swing" in user_input_lower:
conversation_context["last_topic"] = "swing"
return krishna_blessings["swing"]
# Shy or Quiet Moments
if "shy" in user_input_lower:
conversation_context["last_topic"] = "shy"
return krishna_blessings["shy"]
if "quiet" in user_input_lower or "calm" in user_input_lower:
conversation_context["last_topic"] = "quiet"
return krishna_blessings["quiet"]
if "peace" in user_input_lower or "serene" in user_input_lower:
conversation_context["last_topic"] = "peace"
return krishna_blessings["peace"]
if "still" in user_input_lower or "gentle" in user_input_lower:
conversation_context["last_topic"] = "still"
return krishna_blessings["still"]
if "thoughtful" in user_input_lower or "reflect" in user_input_lower:
conversation_context["last_topic"] = "thoughtful"
return krishna_blessings["thoughtful"]
# Jokes
if "joke" in user_input_lower or "funny" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["joke"]
if "laugh" in user_input_lower or "giggle" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["giggle_joke"]
if "silly" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["silly"]
if "butter joke" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["butter_joke"]
if "cow joke" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["cow_joke"]
if "flute joke" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["flute_joke"]
if "dance joke" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["dance_joke"]
if "mischief joke" in user_input_lower:
conversation_context["last_topic"] = "joke"
return krishna_blessings["mischief_joke"]
# Riddles
if "riddle" in user_input_lower or "puzzle" in user_input_lower:
conversation_context["last_topic"] = "riddle"
return krishna_blessings["riddle"]
if "mystery" in user_input_lower or "enigma" in user_input_lower:
conversation_context["last_topic"] = "riddle"
return krishna_blessings["mystery"]
if "question" in user_input_lower:
conversation_context["last_topic"] = "riddle"
return krishna_blessings["question"]
# Birthday Wishes
if "birthday" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return ayush_surprises["birthday"]
if "happy birthday" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["happy_birthday"]
if "birthday wish" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_wish"]
if "birthday blessing" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_blessing"]
if "birthday dance" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_dance"]
if "birthday song" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_song"]
if "birthday gift" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_gift"]
if "birthday smile" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_smile"]
if "birthday love" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_love"]
if "birthday magic" in user_input_lower:
conversation_context["last_topic"] = "birthday"
return krishna_blessings["birthday_magic"]
# Wisdom and Advice
if "wisdom" in user_input_lower or "advice" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["wisdom"]
if "lesson" in user_input_lower or "truth" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["lesson"]
if "kindness" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["kindness"]
if "patience" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["patience"]
if "courage" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["courage"]
if "joy" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["joy"]
if "friendship" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["friendship"]
if "love" in user_input_lower:
conversation_context["last_topic"] = "wisdom"
return krishna_blessings["love"]
# Nature and Vrindavan
if "nature" in user_input_lower or "vrindavan" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["nature"]
if "yamuna" in user_input_lower or "river" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["yamuna"]
if "peacock" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["peacock"]
if "cow" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["cow"]
if "flower" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["flower"]
if "tree" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["tree"]
if "forest" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["forest"]
if "bird" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["bird"]
if "sunset" in user_input_lower:
conversation_context["last_topic"] = "nature"
return krishna_blessings["sunset"]
# Encouragement
if "encourage" in user_input_lower or "cheer" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["encourage"]
if "support" in user_input_lower or "uplift" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["support"]
if "inspire" in user_input_lower or "motivate" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["inspire"]
if "strength" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["strength"]
if "hope" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["hope"]
if "believe" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["believe"]
if "shine" in user_input_lower:
conversation_context["last_topic"] = "encourage"
return krishna_blessings["shine"]
# Miscellaneous
if "friend" in user_input_lower:
conversation_context["last_topic"] = "friend"
return krishna_blessings["friend"]
if "smile" in user_input_lower:
conversation_context["last_topic"] = "smile"
return krishna_blessings["smile"]
if "magic" in user_input_lower:
conversation_context["last_topic"] = "magic"
return krishna_blessings["magic"]
if "adventure" in user_input_lower:
conversation_context["last_topic"] = "adventure"
return krishna_blessings["adventure"]
if "song" in user_input_lower:
conversation_context["last_topic"] = "song"
return krishna_blessings["song"]
if "dream" in user_input_lower:
conversation_context["last_topic"] = "dream"
return krishna_blessings["dream"]
if "story" in user_input_lower:
conversation_context["last_topic"] = "story"
return krishna_blessings["story"]
if "surprise" in user_input_lower:
conversation_context["last_topic"] = "surprise"
return krishna_blessings["surprise"]
if "celebrate" in user_input_lower:
conversation_context["last_topic"] = "celebrate"
return krishna_blessings["celebrate"]
if "blessing" in user_input_lower:
conversation_context["last_topic"] = "blessing"
return krishna_blessings["blessing"]
# Context-Based Responses (if no direct match, use the last topic)
if conversation_context["last_topic"]:
last_topic = conversation_context["last_topic"]
if last_topic in krishna_blessings:
return krishna_blessings[last_topic] + " What else would you like to talk about, Manavi?"
# Fallback to Hugging Face Inference API if no keywords match
headers = {
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"inputs": f"You are Little Krishna, playful and wise. Respond to this in a fun, Krishna-like way: '{user_input}'",
"parameters": {
"max_length": 50, # Reduced for shorter, more playful responses
"temperature": 0.7 # Slightly lower for more coherent responses
}
}
try:
response = requests.post(
"https://api-inference.huggingface.co/models/google/gemma-2b",
headers=headers,
json=payload
)
if response.status_code == 200:
result = response.json()
return result[0]["generated_text"].strip()
else:
print(f"Error with Gemma: {response.text}")
return "Hare Manavi! My flute got a bit shy—let’s try something else, shall we?"
except Exception as e:
print(f"Error connecting to Hugging Face Inference API: {str(e)}")
return "Hare Manavi! I seem to be lost in Vrindavan’s magic—let’s try a different tune!" |