BirthdayM / chatbot.py
ayush2917's picture
Update chatbot.py
e75ce3b verified
raw
history blame
14.4 kB
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!"