File size: 1,834 Bytes
82ea229
 
 
 
4347ff0
82ea229
 
 
 
 
 
4347ff0
 
 
82ea229
 
 
 
286723a
82ea229
 
 
4347ff0
7153980
286723a
7153980
 
82ea229
4347ff0
286723a
 
 
 
 
82ea229
 
 
 
 
 
 
4347ff0
7153980
82ea229
 
 
 
 
 
 
 
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
import gradio as gr
import requests
from PIL import Image
import io
import os

# Define the API URL for the Craiyon model (lightweight text-to-image generator)
API_URL = "https://api-inference.huggingface.co/models/dalle-mini"

# Function to call the model and generate images
def generate_comic(prompt):
    api_token = os.environ.get("API_TOKEN")  # Securely access the token
    
    headers = {"Authorization": f"Bearer {api_token}"}
    
    # Sending request to the model with user prompt
    response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
    
    # Check if the response is successful
    if response.status_code != 200:
        return f"Error: {response.status_code}, {response.text}"
    
    # Extracting the response data
    images = response.json().get("generated_images", [])

    if not images:
        return "No images were generated, please try again with a different prompt."
    
    # Process the images (assuming they are base64 encoded or URL)
    pil_images = []
    for img in images:
        image_data = base64.b64decode(img)
        image = Image.open(io.BytesIO(image_data))
        pil_images.append(image)
    
    return pil_images  # Return the list of PIL images

# Gradio interface setup
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## GenArt Narrative - Turn Your Story into Comic Panels!")
        prompt = gr.Textbox(label="Enter your short story description", placeholder="Once upon a time...")
        output_gallery = gr.Gallery(label="Generated Comic Panels", columns=3, height=300)
        submit_button = gr.Button("Generate Comic")
        submit_button.click(fn=generate_comic, inputs=prompt, outputs=output_gallery)
    return demo

# Run the Gradio app
if __name__ == "__main__":
    app = gradio_interface()
    app.launch()