File size: 2,345 Bytes
be8a0a3
106d939
81a9f63
5971629
6de78f2
d038e9b
5971629
6de78f2
d038e9b
 
5971629
 
 
 
6de78f2
5971629
22235f5
5971629
6de78f2
d038e9b
 
5971629
6de78f2
 
5971629
 
6de78f2
5971629
 
 
 
be8a0a3
81a9f63
 
 
728600e
81a9f63
728600e
d038e9b
9b7f2c8
5971629
 
 
4aa6719
9b7f2c8
 
 
d038e9b
 
81a9f63
0798773
 
81a9f63
0798773
81a9f63
 
 
 
 
 
 
 
 
 
 
 
 
6de78f2
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
import gradio as gr
import requests
import base64
import os
import time

# Get your Hugging Face access token from the secrets
HUGGING_FACE_API_KEY = os.getenv("HuggingFaceAccessToken")  # Ensure your access token is properly set

def generate_images_from_text(text):
    # Generate an image using Hugging Face's API
    headers = {
        "Authorization": f"Bearer {HUGGING_FACE_API_KEY}"
    }
    
    response = requests.post(
        "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev",  # Correct model name
        headers=headers,
        json={"inputs": text}
    )
    
    if response.status_code == 200:
        # The API response contains the image as raw binary data
        image_data = response.content
        
        # Convert the image to Base64 for embedding in HTML
        base64_image = base64.b64encode(image_data).decode('utf-8')
        return f"data:image/png;base64,{base64_image}"
    else:
        print(f"Error generating image: {response.text}")
        return None

def generate_comic(short_story):
    sentences = short_story.split('. ')
    images = []

    for sentence in sentences:
        if sentence.strip():
            try:
                # Generate one image for each sentence
                image_data = generate_images_from_text(sentence)
                if image_data:
                    images.append(image_data)

                # Sleep for a short duration to avoid hitting rate limits
                time.sleep(2)  # Adjust this time if needed

            except Exception as e:
                print(f"Error generating image: {e}")

    # Create comic HTML with reduced image sizes and panel layout
    comic_html = '<div style="display: flex; flex-wrap: wrap;">'
    for img in images:
        comic_html += f'<div style="flex: 1; margin: 5px;"><img src="{img}" style="max-width: 300px; max-height: 300px;" /></div>'
    comic_html += '</div>'
    
    return comic_html

# Gradio interface
iface = gr.Interface(
    fn=generate_comic,
    inputs=gr.Textbox(label="Your Short Story", lines=10, placeholder="Enter your short story here..."),
    outputs=gr.HTML(label="Generated Comic"),
    title="Narrative to Comic Generator",
    description="Enter a short story, and the app will generate a comic-style storybook with images and dialogues."
)

iface.launch()