Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import List
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from ultraflux.pipeline_flux import FluxPipeline
|
| 8 |
+
from ultraflux.transformer_flux import FluxTransformer2DModel
|
| 9 |
+
from ultraflux.autoencoder_kl import AutoencoderKL
|
| 10 |
+
|
| 11 |
+
torch.set_num_threads(os.cpu_count())
|
| 12 |
+
torch.set_float32_matmul_precision("high")
|
| 13 |
+
|
| 14 |
+
local_vae = AutoencoderKL.from_pretrained(
|
| 15 |
+
"Owen777/UltraFlux-v1",
|
| 16 |
+
subfolder="vae",
|
| 17 |
+
torch_dtype=torch.float32
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
transformer = FluxTransformer2DModel.from_pretrained(
|
| 21 |
+
"Owen777/UltraFlux-v1-1-Transformer",
|
| 22 |
+
torch_dtype=torch.float32
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
pipe = FluxPipeline.from_pretrained(
|
| 26 |
+
"Owen777/UltraFlux-v1",
|
| 27 |
+
vae=local_vae,
|
| 28 |
+
torch_dtype=torch.float32,
|
| 29 |
+
transformer=transformer
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 33 |
+
pipe.scheduler.config.use_dynamic_shifting = False
|
| 34 |
+
pipe.scheduler.config.time_shift = 4
|
| 35 |
+
pipe = pipe.to("cpu")
|
| 36 |
+
|
| 37 |
+
os.makedirs("results", exist_ok=True)
|
| 38 |
+
|
| 39 |
+
def generate_ultraflux(prompt: str, seed: int = 0, steps: int = 50, size: int = 1024, guidance: float = 4.0):
|
| 40 |
+
out_path = Path("results") / f"ultra_flux.png"
|
| 41 |
+
with torch.inference_mode():
|
| 42 |
+
image = pipe(
|
| 43 |
+
prompt,
|
| 44 |
+
height=size,
|
| 45 |
+
width=size,
|
| 46 |
+
guidance_scale=guidance,
|
| 47 |
+
num_inference_steps=steps,
|
| 48 |
+
max_sequence_length=512,
|
| 49 |
+
generator=torch.Generator("cpu").manual_seed(seed)
|
| 50 |
+
).images[0]
|
| 51 |
+
image.save(out_path)
|
| 52 |
+
return out_path
|
| 53 |
+
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=generate_ultraflux,
|
| 56 |
+
inputs=[
|
| 57 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
|
| 58 |
+
gr.Number(label="Seed", value=0),
|
| 59 |
+
gr.Slider(10, 100, step=1, value=50, label="Inference Steps"),
|
| 60 |
+
gr.Slider(256, 2048, step=128, value=1024, label="Image Size"),
|
| 61 |
+
gr.Slider(1.0, 10.0, step=0.1, value=4.0, label="Guidance Scale")
|
| 62 |
+
],
|
| 63 |
+
outputs=gr.Image(type="filepath"),
|
| 64 |
+
title="UltraFlux CPU Demo",
|
| 65 |
+
description="Generate high-quality images with UltraFlux on CPU."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|