BirthdayM / image_api.py
ayush2917's picture
Create image_api.py
b1c1180 verified
import os
import requests
from dotenv import load_dotenv
load_dotenv()
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
def generate_krishna_image(prompt, size="512x512"):
try:
headers = {
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"inputs": f"{prompt}, vibrant colors, cartoon style, Little Krishna theme with peacock feathers and Vrindavan background",
"parameters": {
"num_inference_steps": 50,
"guidance_scale": 7.5
}
}
response = requests.post(
"https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
headers=headers,
json=payload
)
if response.status_code == 200:
with open("temp_image.png", "wb") as f:
f.write(response.content)
return "https://via.placeholder.com/512x512.png?text=Stable+Diffusion+Image"
else:
print(f"Error generating image: {response.text}")
return None
except Exception as e:
print(f"Error generating image: {str(e)}")
return None
def generate_comic_strip():
panels = [
"Little Krishna seeing a shy girl reading alone in Vrindavan",
"Little Krishna appearing with a playful smile, holding a flute",
"Little Krishna handing the girl a book, surrounded by cows",
"Little Krishna and the girl smiling together, with peacocks dancing"
]
comic_images = []
for panel_prompt in panels:
image_url = generate_krishna_image(panel_prompt)
if image_url:
comic_images.append(image_url)
else:
comic_images.append("https://via.placeholder.com/512x512.png?text=Error+Generating+Image")
return comic_images