File size: 1,845 Bytes
381c527
 
 
01e2279
 
381c527
 
 
 
 
01e2279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed107b6
01e2279
 
381c527
01e2279
381c527
 
 
 
01e2279
 
381c527
01e2279
 
381c527
01e2279
 
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
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")

def get_krishna_response(user_input):
    """
    Generate a response from Little Krishna based on user input.
    - If the input contains specific keywords, return predefined messages.
    - Otherwise, use Hugging Face's Inference API with the Gemma model.
    """
    if "birthday" in user_input.lower():
        return ayush_surprises["birthday"]
    if "shy" in user_input.lower():
        return krishna_blessings["shy"]
    if "hello" in user_input.lower() or "hi" in user_input.lower():
        return krishna_blessings["greeting"]

    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": 60,
            "temperature": 0.9
        }
    }
    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 "Sorry, I couldn’t generate a response. Let’s try something else!"
    except Exception as e:
        print(f"Error connecting to Hugging Face Inference API: {str(e)}")
        return "Hare Krishna! I seem to be lost in Vrindavan’s magic—let’s try again!"