ayush2917 commited on
Commit
b9143a8
·
verified ·
1 Parent(s): f03ea3f

Update backend/image_api.py

Browse files
Files changed (1) hide show
  1. backend/image_api.py +29 -20
backend/image_api.py CHANGED
@@ -2,41 +2,45 @@ import os
2
  import requests
3
  from dotenv import load_dotenv
4
 
 
5
  load_dotenv()
6
- STABILITY_API_KEY = os.getenv("STABILITY_API_KEY")
7
 
8
  def generate_krishna_image(prompt, size="512x512"):
9
- """Generate a Krishna-themed image using Stable Diffusion XL."""
 
 
 
 
 
 
 
 
 
10
  try:
11
  headers = {
12
- "Authorization": f"Bearer {STABILITY_API_KEY}",
13
- "Content-Type": "application/json",
14
- "Accept": "image/png"
15
  }
16
  payload = {
17
- "text_prompts": [
18
- {
19
- "text": f"{prompt}, vibrant colors, cartoon style, Little Krishna theme with peacock feathers and Vrindavan background",
20
- "weight": 1
21
- }
22
- ],
23
- "height": 512,
24
- "width": 512,
25
- "steps": 50,
26
- "cfg_scale": 7
27
  }
28
  response = requests.post(
29
- "https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image",
30
  headers=headers,
31
  json=payload
32
  )
33
  if response.status_code == 200:
34
- # Save the image temporarily and return a URL (or upload to Firebase Storage)
35
  with open("temp_image.png", "wb") as f:
36
  f.write(response.content)
37
  # In a real app, upload to Firebase Storage and return the URL
38
- # For simplicity, return a placeholder URL (you’d need to implement upload logic)
39
- return "https://via.placeholder.com/512x512.png?text=SDXL+Image"
40
  else:
41
  print(f"Error generating image: {response.text}")
42
  return None
@@ -45,7 +49,12 @@ def generate_krishna_image(prompt, size="512x512"):
45
  return None
46
 
47
  def generate_comic_strip():
48
- """Generate a 4-panel comic strip of Little Krishna helping Manavi."""
 
 
 
 
 
49
  panels = [
50
  "Little Krishna seeing a shy girl reading alone in Vrindavan",
51
  "Little Krishna appearing with a playful smile, holding a flute",
 
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
+ """
11
+ Generate a Krishna-themed image using Stable Diffusion.
12
+
13
+ Args:
14
+ prompt (str): The prompt for image generation.
15
+ size (str): Image size (default: "512x512").
16
+
17
+ Returns:
18
+ str: URL of the generated image (or placeholder if failed).
19
+ """
20
  try:
21
  headers = {
22
+ "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
23
+ "Content-Type": "application/json"
 
24
  }
25
  payload = {
26
+ "inputs": f"{prompt}, vibrant colors, cartoon style, Little Krishna theme with peacock feathers and Vrindavan background",
27
+ "parameters": {
28
+ "num_inference_steps": 50,
29
+ "guidance_scale": 7.5
30
+ }
 
 
 
 
 
31
  }
32
  response = requests.post(
33
+ "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
34
  headers=headers,
35
  json=payload
36
  )
37
  if response.status_code == 200:
38
+ # The response is a binary image; save it temporarily
39
  with open("temp_image.png", "wb") as f:
40
  f.write(response.content)
41
  # In a real app, upload to Firebase Storage and return the URL
42
+ # For simplicity, return a placeholder URL
43
+ return "https://via.placeholder.com/512x512.png?text=Stable+Diffusion+Image"
44
  else:
45
  print(f"Error generating image: {response.text}")
46
  return None
 
49
  return None
50
 
51
  def generate_comic_strip():
52
+ """
53
+ Generate a 4-panel comic strip of Little Krishna helping Manavi.
54
+
55
+ Returns:
56
+ list: List of image URLs for the 4 panels.
57
+ """
58
  panels = [
59
  "Little Krishna seeing a shy girl reading alone in Vrindavan",
60
  "Little Krishna appearing with a playful smile, holding a flute",