File size: 2,583 Bytes
0846a1d
 
 
 
b9143a8
0846a1d
b9143a8
0846a1d
 
b9143a8
 
 
 
 
 
 
 
 
 
0846a1d
 
b9143a8
 
0846a1d
 
b9143a8
 
 
 
 
0846a1d
 
b9143a8
0846a1d
 
 
 
b9143a8
0846a1d
 
 
b9143a8
 
0846a1d
 
 
 
 
 
 
 
b9143a8
 
 
 
 
 
0846a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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