import gradio as gr import torch from diffusers import DiffusionPipeline from PIL import Image # Load the diffusion model pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev") # Set the model to the appropriate device device = "cuda" if torch.cuda.is_available() else "cpu" pipeline.to(device) def generate_image(prompt, guidance_scale=7.5, num_inference_steps=50): # Generate an image based on the prompt with torch.no_grad(): # Generate images images = pipeline(prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images # Assuming pipeline returns a list of images, just take the first one img = images[0] # Convert PIL image to format suitable for Gradio return img # Set up Gradio interface with gr.Blocks() as demo: gr.Markdown("# Text to Image Generation") with gr.Row(): prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here...") guidance_scale = gr.Slider(minimum=1, maximum=15, step=0.1, value=7.5, label="Guidance Scale") num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, value=50, label="Number of Inference Steps") with gr.Row(): generate_button = gr.Button("Generate Image") result = gr.Image(label="Generated Image") # Connect the function to the button generate_button.click( fn=generate_image, inputs=[prompt, guidance_scale, num_inference_steps], outputs=result ) # Launch the app demo.launch()