Create image_api.py
Browse files- image_api.py +51 -0
image_api.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
7 |
+
|
8 |
+
def generate_krishna_image(prompt, size="512x512"):
|
9 |
+
try:
|
10 |
+
headers = {
|
11 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
12 |
+
"Content-Type": "application/json"
|
13 |
+
}
|
14 |
+
payload = {
|
15 |
+
"inputs": f"{prompt}, vibrant colors, cartoon style, Little Krishna theme with peacock feathers and Vrindavan background",
|
16 |
+
"parameters": {
|
17 |
+
"num_inference_steps": 50,
|
18 |
+
"guidance_scale": 7.5
|
19 |
+
}
|
20 |
+
}
|
21 |
+
response = requests.post(
|
22 |
+
"https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
|
23 |
+
headers=headers,
|
24 |
+
json=payload
|
25 |
+
)
|
26 |
+
if response.status_code == 200:
|
27 |
+
with open("temp_image.png", "wb") as f:
|
28 |
+
f.write(response.content)
|
29 |
+
return "https://via.placeholder.com/512x512.png?text=Stable+Diffusion+Image"
|
30 |
+
else:
|
31 |
+
print(f"Error generating image: {response.text}")
|
32 |
+
return None
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error generating image: {str(e)}")
|
35 |
+
return None
|
36 |
+
|
37 |
+
def generate_comic_strip():
|
38 |
+
panels = [
|
39 |
+
"Little Krishna seeing a shy girl reading alone in Vrindavan",
|
40 |
+
"Little Krishna appearing with a playful smile, holding a flute",
|
41 |
+
"Little Krishna handing the girl a book, surrounded by cows",
|
42 |
+
"Little Krishna and the girl smiling together, with peacocks dancing"
|
43 |
+
]
|
44 |
+
comic_images = []
|
45 |
+
for panel_prompt in panels:
|
46 |
+
image_url = generate_krishna_image(panel_prompt)
|
47 |
+
if image_url:
|
48 |
+
comic_images.append(image_url)
|
49 |
+
else:
|
50 |
+
comic_images.append("https://via.placeholder.com/512x512.png?text=Error+Generating+Image")
|
51 |
+
return comic_images
|