Spaces:
Paused
Paused
Delete app.py
Browse files
app.py
DELETED
@@ -1,184 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
import torch
|
5 |
-
from PIL import Image
|
6 |
-
import model_loader
|
7 |
-
import pipeline
|
8 |
-
from transformers import CLIPTokenizer
|
9 |
-
from config import Config, DeviceConfig
|
10 |
-
import os
|
11 |
-
from huggingface_hub import hf_hub_download
|
12 |
-
from pathlib import Path
|
13 |
-
|
14 |
-
# Create data directory if it doesn't exist
|
15 |
-
data_dir = Path("data")
|
16 |
-
data_dir.mkdir(exist_ok=True)
|
17 |
-
|
18 |
-
# Model configuration
|
19 |
-
MODEL_REPO = "stabilityai/stable-diffusion-v1-5"
|
20 |
-
MODEL_FILENAME = "v1-5-pruned-emaonly.ckpt"
|
21 |
-
model_file = data_dir / MODEL_FILENAME
|
22 |
-
|
23 |
-
# Download model if it doesn't exist
|
24 |
-
if not model_file.exists():
|
25 |
-
print(f"Downloading model from {MODEL_REPO}...")
|
26 |
-
model_file = hf_hub_download(
|
27 |
-
repo_id=MODEL_REPO,
|
28 |
-
filename=MODEL_FILENAME,
|
29 |
-
local_dir=data_dir,
|
30 |
-
local_dir_use_symlinks=False
|
31 |
-
)
|
32 |
-
print("Model downloaded successfully!")
|
33 |
-
|
34 |
-
# Device configuration
|
35 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
36 |
-
print(f"Using device: {device}")
|
37 |
-
|
38 |
-
# Initialize configuration
|
39 |
-
config = Config(
|
40 |
-
device=DeviceConfig(device=device),
|
41 |
-
tokenizer=CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
|
42 |
-
)
|
43 |
-
|
44 |
-
# Load models with SE blocks enabled
|
45 |
-
config.models = model_loader.load_models(str(model_file), device, use_se=True)
|
46 |
-
|
47 |
-
MAX_SEED = np.iinfo(np.int32).max
|
48 |
-
MAX_IMAGE_SIZE = 1024
|
49 |
-
|
50 |
-
def infer(
|
51 |
-
prompt,
|
52 |
-
negative_prompt,
|
53 |
-
seed,
|
54 |
-
randomize_seed,
|
55 |
-
width,
|
56 |
-
height,
|
57 |
-
guidance_scale,
|
58 |
-
num_inference_steps,
|
59 |
-
progress=gr.Progress(track_tqdm=True),
|
60 |
-
):
|
61 |
-
if randomize_seed:
|
62 |
-
seed = random.randint(0, MAX_SEED)
|
63 |
-
|
64 |
-
# Update config with user settings
|
65 |
-
config.seed = seed
|
66 |
-
config.diffusion.cfg_scale = guidance_scale
|
67 |
-
config.diffusion.n_inference_steps = num_inference_steps
|
68 |
-
config.model.width = width
|
69 |
-
config.model.height = height
|
70 |
-
|
71 |
-
# Generate image
|
72 |
-
output_image = pipeline.generate(
|
73 |
-
prompt=prompt,
|
74 |
-
uncond_prompt=negative_prompt,
|
75 |
-
config=config
|
76 |
-
)
|
77 |
-
|
78 |
-
# Convert numpy array to PIL Image
|
79 |
-
image = Image.fromarray(output_image)
|
80 |
-
|
81 |
-
return image, seed
|
82 |
-
|
83 |
-
examples = [
|
84 |
-
"A ultra sharp photorealtici painting of a futuristic cityscape at night with neon lights and flying cars",
|
85 |
-
"A serene mountain landscape at sunset with snow-capped peaks and a clear lake reflection",
|
86 |
-
"A detailed portrait of a cyberpunk character with glowing neon implants and holographic tattoos",
|
87 |
-
]
|
88 |
-
|
89 |
-
css = """
|
90 |
-
#col-container {
|
91 |
-
margin: 0 auto;
|
92 |
-
max-width: 640px;
|
93 |
-
}
|
94 |
-
"""
|
95 |
-
|
96 |
-
with gr.Blocks(css=css) as demo:
|
97 |
-
with gr.Column(elem_id="col-container"):
|
98 |
-
gr.Markdown(" # Custom Diffusion Model Text-to-Image Generator")
|
99 |
-
|
100 |
-
with gr.Row():
|
101 |
-
prompt = gr.Text(
|
102 |
-
label="Prompt",
|
103 |
-
show_label=False,
|
104 |
-
max_lines=1,
|
105 |
-
placeholder="Enter your prompt",
|
106 |
-
container=False,
|
107 |
-
)
|
108 |
-
|
109 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
110 |
-
|
111 |
-
result = gr.Image(label="Result", show_label=False)
|
112 |
-
|
113 |
-
with gr.Accordion("Advanced Settings", open=False):
|
114 |
-
negative_prompt = gr.Text(
|
115 |
-
label="Negative prompt",
|
116 |
-
max_lines=1,
|
117 |
-
placeholder="Enter a negative prompt",
|
118 |
-
visible=False,
|
119 |
-
)
|
120 |
-
|
121 |
-
seed = gr.Slider(
|
122 |
-
label="Seed",
|
123 |
-
minimum=0,
|
124 |
-
maximum=MAX_SEED,
|
125 |
-
step=1,
|
126 |
-
value=42,
|
127 |
-
)
|
128 |
-
|
129 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
130 |
-
|
131 |
-
with gr.Row():
|
132 |
-
width = gr.Slider(
|
133 |
-
label="Width",
|
134 |
-
minimum=256,
|
135 |
-
maximum=MAX_IMAGE_SIZE,
|
136 |
-
step=32,
|
137 |
-
value=512,
|
138 |
-
)
|
139 |
-
|
140 |
-
height = gr.Slider(
|
141 |
-
label="Height",
|
142 |
-
minimum=256,
|
143 |
-
maximum=MAX_IMAGE_SIZE,
|
144 |
-
step=32,
|
145 |
-
value=512,
|
146 |
-
)
|
147 |
-
|
148 |
-
with gr.Row():
|
149 |
-
guidance_scale = gr.Slider(
|
150 |
-
label="Guidance scale",
|
151 |
-
minimum=0.0,
|
152 |
-
maximum=10.0,
|
153 |
-
step=0.1,
|
154 |
-
value=7.5,
|
155 |
-
)
|
156 |
-
|
157 |
-
num_inference_steps = gr.Slider(
|
158 |
-
label="Number of inference steps",
|
159 |
-
minimum=1,
|
160 |
-
maximum=50,
|
161 |
-
step=1,
|
162 |
-
value=50,
|
163 |
-
)
|
164 |
-
|
165 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
166 |
-
|
167 |
-
gr.on(
|
168 |
-
triggers=[run_button.click, prompt.submit],
|
169 |
-
fn=infer,
|
170 |
-
inputs=[
|
171 |
-
prompt,
|
172 |
-
negative_prompt,
|
173 |
-
seed,
|
174 |
-
randomize_seed,
|
175 |
-
width,
|
176 |
-
height,
|
177 |
-
guidance_scale,
|
178 |
-
num_inference_steps,
|
179 |
-
],
|
180 |
-
outputs=[result, seed],
|
181 |
-
)
|
182 |
-
|
183 |
-
if __name__ == "__main__":
|
184 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|