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()