Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image | |
import torch | |
from diffusers import StableDiffusionPipeline | |
# Load the model | |
model_id = "models/black-forest-labs/FLUX.1-dev" | |
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) | |
pipe = pipe.to("cuda") | |
def generate_image(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height): | |
image = pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, | |
num_inference_steps=num_inference_steps, | |
guidance_scale=guidance_scale, | |
width=width, | |
height=height | |
).images[0] | |
return image | |
def edit_image(image, brightness, contrast, saturation): | |
edited = Image.fromarray(image) | |
edited = edited.convert('RGB') | |
edited = edited.point(lambda p: p * brightness) | |
edited = edited.point(lambda p: 128 + (p - 128) * contrast) | |
edited = edited.convert('HSV') | |
h, s, v = edited.split() | |
s = s.point(lambda p: p * saturation) | |
edited = Image.merge('HSV', (h, s, v)).convert('RGB') | |
return edited | |
with gr.Blocks(css="style.css") as demo: | |
gr.Markdown("# FLUX.1 Image Generator") | |
with gr.Tab("Generate"): | |
with gr.Row(): | |
with gr.Column(): | |
prompt = gr.Textbox(label="Prompt") | |
negative_prompt = gr.Textbox(label="Negative Prompt") | |
steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Inference Steps") | |
guidance = gr.Slider(minimum=1, maximum=20, value=7.5, step=0.1, label="Guidance Scale") | |
width = gr.Slider(minimum=256, maximum=1024, value=512, step=64, label="Width") | |
height = gr.Slider(minimum=256, maximum=1024, value=512, step=64, label="Height") | |
generate_btn = gr.Button("Generate") | |
with gr.Column(): | |
output = gr.Image(label="Generated Image") | |
with gr.Tab("Edit"): | |
with gr.Row(): | |
with gr.Column(): | |
input_image = gr.Image(label="Input Image") | |
brightness = gr.Slider(minimum=0.5, maximum=1.5, value=1.0, step=0.1, label="Brightness") | |
contrast = gr.Slider(minimum=0.5, maximum=1.5, value=1.0, step=0.1, label="Contrast") | |
saturation = gr.Slider(minimum=0.5, maximum=1.5, value=1.0, step=0.1, label="Saturation") | |
edit_btn = gr.Button("Apply Edits") | |
with gr.Column(): | |
edited_output = gr.Image(label="Edited Image") | |
generate_btn.click(generate_image, inputs=[prompt, negative_prompt, steps, guidance, width, height], outputs=output) | |
edit_btn.click(edit_image, inputs=[input_image, brightness, contrast, saturation], outputs=edited_output) | |
demo.launch() |