Rename backend/dalle_api.py to backend/image_api.py
Browse files
backend/{dalle_api.py → image_api.py}
RENAMED
@@ -1,24 +1,51 @@
|
|
1 |
-
import openai
|
2 |
import os
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
load_dotenv()
|
6 |
-
|
7 |
|
8 |
def generate_krishna_image(prompt, size="512x512"):
|
|
|
9 |
try:
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
)
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
except Exception as e:
|
18 |
print(f"Error generating image: {str(e)}")
|
19 |
return None
|
20 |
|
21 |
def generate_comic_strip():
|
|
|
22 |
panels = [
|
23 |
"Little Krishna seeing a shy girl reading alone in Vrindavan",
|
24 |
"Little Krishna appearing with a playful smile, holding a flute",
|
|
|
|
|
1 |
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
|
43 |
except Exception as e:
|
44 |
print(f"Error generating image: {str(e)}")
|
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",
|