Create chatbot.py
Browse files- chatbot.py +44 -0
chatbot.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from messages import krishna_blessings
|
5 |
+
from ayush_messages import ayush_surprises
|
6 |
+
|
7 |
+
# Load environment variables (Hugging Face Space secrets)
|
8 |
+
load_dotenv()
|
9 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
10 |
+
|
11 |
+
def get_krishna_response(user_input):
|
12 |
+
if "birthday" in user_input.lower():
|
13 |
+
return ayush_surprises["birthday"]
|
14 |
+
if "shy" in user_input.lower():
|
15 |
+
return krishna_blessings["shy"]
|
16 |
+
if "hello" in user_input.lower() or "hi" in user_input.lower():
|
17 |
+
return krishna_blessings["greeting"]
|
18 |
+
|
19 |
+
headers = {
|
20 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
21 |
+
"Content-Type": "application/json"
|
22 |
+
}
|
23 |
+
payload = {
|
24 |
+
"inputs": f"You are Little Krishna, playful and wise. Respond to this in a fun, Krishna-like way: '{user_input}'",
|
25 |
+
"parameters": {
|
26 |
+
"max_length": 60,
|
27 |
+
"temperature": 0.9
|
28 |
+
}
|
29 |
+
}
|
30 |
+
try:
|
31 |
+
response = requests.post(
|
32 |
+
"https://api-inference.huggingface.co/models/google/gemma-2b",
|
33 |
+
headers=headers,
|
34 |
+
json=payload
|
35 |
+
)
|
36 |
+
if response.status_code == 200:
|
37 |
+
result = response.json()
|
38 |
+
return result[0]["generated_text"].strip()
|
39 |
+
else:
|
40 |
+
print(f"Error with Gemma: {response.text}")
|
41 |
+
return "Sorry, I couldn’t generate a response. Let’s try something else!"
|
42 |
+
except Exception as e:
|
43 |
+
print(f"Error connecting to Hugging Face Inference API: {str(e)}")
|
44 |
+
return "Hare Krishna! I seem to be lost in Vrindavan’s magic—let’s try again!"
|