ayush2917 commited on
Commit
ed107b6
·
verified ·
1 Parent(s): 9887ddc

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +36 -28
chatbot.py CHANGED
@@ -1,44 +1,52 @@
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!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 generate_krishna_image(prompt, size="512x512"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  try:
11
+ headers = {
12
+ "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
13
+ "Content-Type": "application/json"
14
+ }
15
+ payload = {
16
+ "inputs": f"{prompt}, vibrant colors, cartoon style, Little Krishna theme with peacock feathers and Vrindavan background",
17
+ "parameters": {
18
+ "num_inference_steps": 50,
19
+ "guidance_scale": 7.5
20
+ }
21
+ }
22
  response = requests.post(
23
+ "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
24
  headers=headers,
25
  json=payload
26
  )
27
  if response.status_code == 200:
28
+ with open("temp_image.png", "wb") as f:
29
+ f.write(response.content)
30
+ return "https://via.placeholder.com/512x512.png?text=Stable+Diffusion+Image"
31
  else:
32
+ print(f"Error generating image: {response.text}")
33
+ return None
34
  except Exception as e:
35
+ print(f"Error generating image: {str(e)}")
36
+ return None
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