Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system("pip uninstall torchvision -y")
|
| 3 |
+
os.system("pip install torchvision --force-reinstall --no-cache-dir")
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import AutoPipelineForText2Image
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import spaces
|
| 10 |
+
|
| 11 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
+
"ostris/Flex.2-preview",
|
| 13 |
+
custom_pipeline="Flex.2-preview/pipeline.py",
|
| 14 |
+
torch_dtype=torch.bfloat16,
|
| 15 |
+
).to("cuda")
|
| 16 |
+
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def generate_image(
|
| 19 |
+
prompt: str,
|
| 20 |
+
inpaint_img: Image.Image,
|
| 21 |
+
inpaint_mask: Image.Image,
|
| 22 |
+
control_img: Image.Image,
|
| 23 |
+
height: int,
|
| 24 |
+
width: int,
|
| 25 |
+
guidance_scale: float,
|
| 26 |
+
num_inference_steps: int,
|
| 27 |
+
seed: int,
|
| 28 |
+
control_strength: float,
|
| 29 |
+
control_stop: float,
|
| 30 |
+
):
|
| 31 |
+
gen = torch.Generator(device="cuda").manual_seed(seed)
|
| 32 |
+
|
| 33 |
+
inp_img = inpaint_img.convert("RGB")
|
| 34 |
+
inp_mask = inpaint_mask.convert("RGB")
|
| 35 |
+
ctrl_img = control_img.convert("RGB")
|
| 36 |
+
|
| 37 |
+
result = pipe(
|
| 38 |
+
prompt=prompt,
|
| 39 |
+
inpaint_image=inp_img,
|
| 40 |
+
inpaint_mask=inp_mask,
|
| 41 |
+
control_image=ctrl_img,
|
| 42 |
+
control_strength=control_strength,
|
| 43 |
+
control_stop=control_stop,
|
| 44 |
+
height=height,
|
| 45 |
+
width=width,
|
| 46 |
+
guidance_scale=guidance_scale,
|
| 47 |
+
num_inference_steps=num_inference_steps,
|
| 48 |
+
generator=gen,
|
| 49 |
+
)
|
| 50 |
+
return result.images[0]
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(title="Flex.2-preview Image Generator") as demo:
|
| 53 |
+
gr.Markdown("# Flex.2-preview Text→Image Generator")
|
| 54 |
+
|
| 55 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt...", lines=2)
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
inpaint_img = gr.Image(type="pil", label="Inpaint Image")
|
| 59 |
+
inpaint_mask = gr.Image(type="pil", label="Inpaint Mask")
|
| 60 |
+
control_img = gr.Image(type="pil", label="Control Image")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
height = gr.Slider(64, 2048, value=512, step=64, label="Height")
|
| 64 |
+
width = gr.Slider(64, 2048, value=512, step=64, label="Width")
|
| 65 |
+
with gr.Row():
|
| 66 |
+
guidance_scale = gr.Slider(0.0, 20.0, value=3.5, step=0.1, label="Guidance Scale")
|
| 67 |
+
num_inference_steps = gr.Slider(1, 100, value=50, step=1, label="Inference Steps")
|
| 68 |
+
seed = gr.Number(value=42, precision=0, label="Random Seed")
|
| 69 |
+
control_strength = gr.Slider(0.0, 1.0, value=0.0, step=0.01, label="Control Strength")
|
| 70 |
+
control_stop = gr.Slider(0.0, 1.0, value=1.0, step=0.01, label="Control Stop")
|
| 71 |
+
|
| 72 |
+
generate_btn = gr.Button("Generate")
|
| 73 |
+
output = gr.Image(type="pil", label="Generated Image")
|
| 74 |
+
|
| 75 |
+
generate_btn.click(
|
| 76 |
+
fn=generate_image,
|
| 77 |
+
inputs=[
|
| 78 |
+
prompt,
|
| 79 |
+
inpaint_img,
|
| 80 |
+
inpaint_mask,
|
| 81 |
+
control_img,
|
| 82 |
+
height,
|
| 83 |
+
width,
|
| 84 |
+
guidance_scale,
|
| 85 |
+
num_inference_steps,
|
| 86 |
+
seed,
|
| 87 |
+
control_strength,
|
| 88 |
+
control_stop,
|
| 89 |
+
],
|
| 90 |
+
outputs=[output],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch(share=True)
|