File size: 1,568 Bytes
b32b9e5
2ee0042
b32b9e5
 
62641a0
 
 
 
 
 
 
 
 
 
 
 
 
 
b32b9e5
62641a0
2ee0042
 
 
62641a0
 
 
 
 
 
 
 
 
2ee0042
 
 
62641a0
2ee0042
62641a0
2ee0042
 
62641a0
 
 
 
2ee0042
 
62641a0
2ee0042
 
62641a0
 
 
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
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch

# ----------------------------------------
# Load PosterCraft model from Hugging Face
# ----------------------------------------
# If your Space uses a free CPU runtime, you can leave torch_dtype=None.
# On GPU, float16 is faster and uses less memory.
model_id = "PosterCraft/PosterCraft-v1_RL"

try:
    pipe = StableDiffusionPipeline.from_pretrained(
        model_id,
        torch_dtype=torch.float16 if torch.cuda.is_available() else None
    )
except Exception as e:
    raise RuntimeError(f"Error loading model {model_id}: {e}")

# Select device
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe.to(device)

# ----------------------------------------
# Generation function
# ----------------------------------------
def generate_poster(prompt: str):
    """
    Generate a poster/flyer based on the user's text prompt.
    """
    if not prompt.strip():
        return None
    image = pipe(prompt).images[0]
    return image

# ----------------------------------------
# Gradio UI
# ----------------------------------------
demo = gr.Interface(
    fn=generate_poster,
    inputs=gr.Textbox(
        label="Enter your poster prompt",
        placeholder="e.g. Modern music festival flyer with bold typography and neon lights"
    ),
    outputs=gr.Image(type="pil", label="Generated Poster"),
    title="AI Poster Generator",
    description="Enter a description and get an AI-designed poster or flyer."
)

# Launch the app
if __name__ == "__main__":
    demo.launch()