Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,31 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
-
import torch
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 11 |
-
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
torch_dtype = torch.float16
|
| 14 |
else:
|
| 15 |
torch_dtype = torch.float32
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
seed,
|
| 29 |
randomize_seed,
|
| 30 |
width,
|
| 31 |
-
height,
|
| 32 |
guidance_scale,
|
| 33 |
num_inference_steps,
|
| 34 |
progress=gr.Progress(track_tqdm=True),
|
|
@@ -36,119 +33,112 @@ def infer(
|
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
image = pipe(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
num_inference_steps=num_inference_steps,
|
| 46 |
-
|
| 47 |
-
height=height,
|
| 48 |
generator=generator,
|
| 49 |
).images[0]
|
|
|
|
| 50 |
|
| 51 |
return image, seed
|
| 52 |
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"A
|
|
|
|
| 58 |
]
|
| 59 |
|
|
|
|
| 60 |
css = """
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
margin: 0 auto;
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
with gr.Blocks(css=css) as demo:
|
| 68 |
-
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
prompt = gr.Text(
|
| 73 |
-
label="Prompt",
|
| 74 |
-
show_label=False,
|
| 75 |
-
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
-
)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
with gr.
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
visible=False,
|
| 90 |
)
|
| 91 |
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
label="Width",
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
|
|
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
from src.pipeline import DiT360Pipeline
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
torch_dtype = torch.float16
|
| 12 |
else:
|
| 13 |
torch_dtype = torch.float32
|
| 14 |
|
| 15 |
+
model_repo = "/media/nfs/tmp_data/fenghr/download/FLUX.1-dev"
|
| 16 |
+
lora_weights = "/media/nfs/tmp_data/fenghr/DiT360-FLUX-Panorama-Lora"
|
| 17 |
|
| 18 |
+
pipe = DiT360Pipeline.from_pretrained(model_repo, torch_dtype=torch_dtype).to(device)
|
| 19 |
+
pipe.load_lora_weights(lora_weights)
|
| 20 |
|
| 21 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
+
MAX_IMAGE_SIZE = 2048
|
| 23 |
|
|
|
|
| 24 |
def infer(
|
| 25 |
prompt,
|
|
|
|
| 26 |
seed,
|
| 27 |
randomize_seed,
|
| 28 |
width,
|
|
|
|
| 29 |
guidance_scale,
|
| 30 |
num_inference_steps,
|
| 31 |
progress=gr.Progress(track_tqdm=True),
|
|
|
|
| 33 |
if randomize_seed:
|
| 34 |
seed = random.randint(0, MAX_SEED)
|
| 35 |
|
| 36 |
+
height = width // 2
|
| 37 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 38 |
+
|
| 39 |
+
full_prompt = f"This is a panorama. The images shows {prompt.strip()}"
|
| 40 |
|
| 41 |
image = pipe(
|
| 42 |
+
full_prompt,
|
| 43 |
+
width=int(width),
|
| 44 |
+
height=int(height),
|
| 45 |
num_inference_steps=num_inference_steps,
|
| 46 |
+
guidance_scale=guidance_scale,
|
|
|
|
| 47 |
generator=generator,
|
| 48 |
).images[0]
|
| 49 |
+
image.save("test.png")
|
| 50 |
|
| 51 |
return image, seed
|
| 52 |
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
+
"A medieval castle stands proudly on a hilltop surrounded by autumn forests, with golden light spilling across the landscape.",
|
| 56 |
+
"A futuristic cityscape under a starry night sky.",
|
| 57 |
+
"A tranquil beach with palm trees and turquoise water at sunset.",
|
| 58 |
+
"A snowy mountain village under northern lights, with cozy cabins and smoke rising from chimneys.",
|
| 59 |
]
|
| 60 |
|
| 61 |
+
|
| 62 |
css = """
|
| 63 |
+
#main-container {
|
| 64 |
+
display: flex;
|
| 65 |
+
flex-direction: row;
|
| 66 |
+
justify-content: center;
|
| 67 |
+
align-items: flex-start;
|
| 68 |
+
gap: 2rem;
|
| 69 |
+
margin-top: 1rem;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
#image-panel {
|
| 73 |
+
flex: 2; /* 占2/3 */
|
| 74 |
+
max-width: 900px;
|
| 75 |
margin: 0 auto;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
#settings-panel {
|
| 79 |
+
flex: 1; /* 占1/3 */
|
| 80 |
+
max-width: 280px;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
#prompt-box textarea {
|
| 84 |
+
resize: none !important; /* 去掉上下箭头 */
|
| 85 |
}
|
| 86 |
"""
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
with gr.Blocks(css=css) as demo:
|
| 90 |
+
gr.Markdown("# 🌀 DiT360: High-Fidelity Panoramic Image Generation")
|
| 91 |
+
gr.Markdown("Official Gradio demo for **DiT360**, a panoramic image generation model based on hybrid training.")
|
| 92 |
+
|
| 93 |
+
with gr.Row(elem_id="main-container"):
|
| 94 |
+
with gr.Column(elem_id="image-panel"):
|
| 95 |
+
result = gr.Image(label="Generated Panorama", show_label=False, type="pil", height=360)
|
| 96 |
+
|
| 97 |
+
with gr.Column():
|
| 98 |
+
prompt = gr.Textbox(
|
| 99 |
+
elem_id="prompt-box",
|
| 100 |
+
placeholder="Describe your panoramic scene here...",
|
| 101 |
+
show_label=False,
|
| 102 |
+
lines=2,
|
| 103 |
+
container=False,
|
| 104 |
+
)
|
| 105 |
+
run_button = gr.Button("Generate", variant="primary")
|
| 106 |
|
| 107 |
+
gr.Examples(examples=examples, inputs=[prompt])
|
| 108 |
|
| 109 |
+
with gr.Column(elem_id="settings-panel"):
|
| 110 |
+
gr.Markdown("### ⚙️ Settings")
|
| 111 |
+
gr.Markdown(
|
| 112 |
+
"For best results, it is **not recommended** to modify `width` or `guidance scale`. "
|
| 113 |
+
"The height is automatically set to half the width (2:1 aspect ratio)."
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
+
seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
|
| 117 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
width = gr.Slider(1024, MAX_IMAGE_SIZE, value=2048, step=64, label="Width (fixed 2:1)")
|
| 120 |
+
height_display = gr.Number(value=1024, label="Height", interactive=False)
|
| 121 |
|
| 122 |
+
guidance_scale = gr.Slider(0.0, 10.0, value=2.8, step=0.1, label="Guidance Scale")
|
| 123 |
+
num_inference_steps = gr.Slider(28, 100, value=50, step=1, label="Inference Steps")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
def update_height(width):
|
| 126 |
+
return width // 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
width.change(fn=update_height, inputs=width, outputs=height_display)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
gr.Markdown(
|
| 131 |
+
"💡 *Tip: Try descriptive prompts like “A mountain village at sunrise with mist over the valley.” "
|
| 132 |
+
"DiT360 will automatically add a trigger word.*"
|
| 133 |
+
)
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
| 135 |
gr.on(
|
| 136 |
triggers=[run_button.click, prompt.submit],
|
| 137 |
fn=infer,
|
| 138 |
+
inputs=[prompt, seed, randomize_seed, width, guidance_scale, num_inference_steps],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
outputs=[result, seed],
|
| 140 |
)
|
| 141 |
|
| 142 |
+
|
| 143 |
if __name__ == "__main__":
|
| 144 |
demo.launch()
|