ayush2917 commited on
Commit
01e2279
·
verified ·
1 Parent(s): 6a63f2a

Update chatbot.py

Browse files
Files changed (1) hide show
  1. 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 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
 
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!"