import os import requests from dotenv import load_dotenv # Load environment variables (Hugging Face Space secrets) load_dotenv() HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN") def generate_krishna_image(prompt, size="512x512"): """ Generate a Krishna-themed image using Stable Diffusion. Args: prompt (str): The prompt for image generation. size (str): Image size (default: "512x512"). Returns: str: URL of the generated image (or placeholder if failed). """ 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: # The response is a binary image; save it temporarily with open("temp_image.png", "wb") as f: f.write(response.content) # In a real app, upload to Firebase Storage and return the URL # For simplicity, return a placeholder URL 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(): """ Generate a 4-panel comic strip of Little Krishna helping Manavi. Returns: list: List of image URLs for the 4 panels. """ 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