Update chatbot.py
Browse files- chatbot.py +33 -36
chatbot.py
CHANGED
@@ -1,52 +1,49 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
from dotenv import load_dotenv
|
|
|
|
|
4 |
|
5 |
# Load environment variables (Hugging Face Space secrets)
|
6 |
load_dotenv()
|
7 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
|
|
|
|
22 |
response = requests.post(
|
23 |
-
"https://api-inference.huggingface.co/models/
|
24 |
headers=headers,
|
25 |
json=payload
|
26 |
)
|
27 |
if response.status_code == 200:
|
28 |
-
|
29 |
-
|
30 |
-
return "https://via.placeholder.com/512x512.png?text=Stable+Diffusion+Image"
|
31 |
else:
|
32 |
-
print(f"Error
|
33 |
-
return
|
34 |
except Exception as e:
|
35 |
-
print(f"Error
|
36 |
-
return
|
37 |
-
|
38 |
-
def generate_comic_strip():
|
39 |
-
panels = [
|
40 |
-
"Little Krishna seeing a shy girl reading alone in Vrindavan",
|
41 |
-
"Little Krishna appearing with a playful smile, holding a flute",
|
42 |
-
"Little Krishna handing the girl a book, surrounded by cows",
|
43 |
-
"Little Krishna and the girl smiling together, with peacocks dancing"
|
44 |
-
]
|
45 |
-
comic_images = []
|
46 |
-
for panel_prompt in panels:
|
47 |
-
image_url = generate_krishna_image(panel_prompt)
|
48 |
-
if image_url:
|
49 |
-
comic_images.append(image_url)
|
50 |
-
else:
|
51 |
-
comic_images.append("https://via.placeholder.com/512x512.png?text=Error+Generating+Image")
|
52 |
-
return comic_images
|
|
|
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 |
+
"""
|
13 |
+
Generate a response from Little Krishna based on user input.
|
14 |
+
- If the input contains specific keywords, return predefined messages.
|
15 |
+
- Otherwise, use Hugging Face's Inference API with the Gemma model.
|
16 |
+
"""
|
17 |
+
if "birthday" in user_input.lower():
|
18 |
+
return ayush_surprises["birthday"]
|
19 |
+
if "shy" in user_input.lower():
|
20 |
+
return krishna_blessings["shy"]
|
21 |
+
if "hello" in user_input.lower() or "hi" in user_input.lower():
|
22 |
+
return krishna_blessings["greeting"]
|
23 |
+
|
24 |
+
headers = {
|
25 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
26 |
+
"Content-Type": "application/json"
|
27 |
+
}
|
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": 60,
|
32 |
+
"temperature": 0.9
|
33 |
}
|
34 |
+
}
|
35 |
+
try:
|
36 |
response = requests.post(
|
37 |
+
"https://api-inference.huggingface.co/models/google/gemma-2b",
|
38 |
headers=headers,
|
39 |
json=payload
|
40 |
)
|
41 |
if response.status_code == 200:
|
42 |
+
result = response.json()
|
43 |
+
return result[0]["generated_text"].strip()
|
|
|
44 |
else:
|
45 |
+
print(f"Error with Gemma: {response.text}")
|
46 |
+
return "Sorry, I couldn’t generate a response. Let’s try something else!"
|
47 |
except Exception as e:
|
48 |
+
print(f"Error connecting to Hugging Face Inference API: {str(e)}")
|
49 |
+
return "Hare Krishna! I seem to be lost in Vrindavan’s magic—let’s try again!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|