Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,19 @@
|
|
1 |
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import logging
|
3 |
import random
|
4 |
import warnings
|
5 |
-
import os
|
6 |
import io
|
7 |
import base64
|
|
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
10 |
import spaces
|
@@ -44,9 +53,9 @@ logging.info(f"Selected device: {device} | Data type: {torch_dtype}")
|
|
44 |
# --- Authentication and Model Download ---
|
45 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
46 |
|
47 |
-
flux_model_id
|
48 |
controlnet_model_id = "jasperai/Flux.1-dev-Controlnet-Upscaler"
|
49 |
-
local_model_dir
|
50 |
pipe = None
|
51 |
|
52 |
try:
|
@@ -78,20 +87,20 @@ except Exception as e:
|
|
78 |
logging.error(f"Model load failed: {e}", exc_info=True)
|
79 |
raise SystemExit(f"Fatal: could not load models: {e}")
|
80 |
|
81 |
-
|
82 |
-
# --- Constants & Helpers ---
|
83 |
MAX_SEED = 2**32 - 1
|
84 |
MAX_PIXEL_BUDGET = 1280 * 1280
|
85 |
INTERNAL_PROCESSING_FACTOR = 4
|
86 |
|
87 |
def process_input(input_image):
|
88 |
if input_image is None:
|
89 |
-
raise gr.Error("No input image!")
|
90 |
input_image = ImageOps.exif_transpose(input_image)
|
91 |
if input_image.mode != 'RGB':
|
92 |
input_image = input_image.convert('RGB')
|
93 |
w, h = input_image.size
|
94 |
-
|
|
|
95 |
target_pixels = (w*INTERNAL_PROCESSING_FACTOR)*(h*INTERNAL_PROCESSING_FACTOR)
|
96 |
if target_pixels > MAX_PIXEL_BUDGET:
|
97 |
max_in = MAX_PIXEL_BUDGET / (INTERNAL_PROCESSING_FACTOR**2)
|
@@ -101,11 +110,13 @@ def process_input(input_image):
|
|
101 |
was_resized = True
|
102 |
else:
|
103 |
was_resized = False
|
104 |
-
|
|
|
105 |
w2, h2 = input_image.size
|
106 |
-
w2 -= w2%8; h2 -= h2%8
|
107 |
if input_image.size != (w2,h2):
|
108 |
input_image = input_image.resize((w2,h2), Image.Resampling.LANCZOS)
|
|
|
109 |
return input_image, w, h, was_resized
|
110 |
|
111 |
@spaces.GPU(duration=75)
|
@@ -125,11 +136,13 @@ def infer(
|
|
125 |
seed = random.randint(0, MAX_SEED)
|
126 |
seed = int(seed)
|
127 |
final_upscale_factor = int(final_upscale_factor)
|
|
|
128 |
processed, w0, h0, resized_flag = process_input(input_image)
|
129 |
w_proc, h_proc = processed.size
|
130 |
-
|
131 |
cw, ch = w_proc*INTERNAL_PROCESSING_FACTOR, h_proc*INTERNAL_PROCESSING_FACTOR
|
132 |
control_img = processed.resize((cw, ch), Image.Resampling.LANCZOS)
|
|
|
133 |
generator = torch.Generator(device=device).manual_seed(seed)
|
134 |
with torch.inference_mode():
|
135 |
result = pipe(
|
@@ -141,21 +154,23 @@ def infer(
|
|
141 |
height=ch, width=cw,
|
142 |
generator=generator,
|
143 |
).images[0]
|
144 |
-
|
|
|
145 |
if resized_flag:
|
146 |
final_w, final_h = w_proc*final_upscale_factor, h_proc*final_upscale_factor
|
147 |
else:
|
148 |
final_w, final_h = w0*final_upscale_factor, h0*final_upscale_factor
|
|
|
149 |
if (final_w, final_h) != result.size:
|
150 |
result = result.resize((final_w, final_h), Image.Resampling.LANCZOS)
|
151 |
-
|
152 |
buf = io.BytesIO()
|
153 |
result.save(buf, format="WEBP", quality=90)
|
154 |
b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
155 |
-
return [[input_image, result], seed, f"data:image/webp;base64,{b64}"]
|
156 |
|
|
|
157 |
|
158 |
-
# --- Gradio
|
159 |
with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Flux Upscaler Demo") as demo:
|
160 |
gr.Markdown(f"""
|
161 |
# ⚡ Flux.1‑dev Upscaler
|
@@ -200,5 +215,5 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Flux Upscaler Demo") as d
|
|
200 |
api_name="upscale"
|
201 |
)
|
202 |
|
203 |
-
#
|
204 |
demo.queue(max_size=10).launch(share=False, show_api=True)
|
|
|
1 |
# app.py
|
2 |
+
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
# --- Install Dependencies ---
|
7 |
+
print("Installing required packages: diffusers, gradio_imageslider, huggingface-hub...")
|
8 |
+
os.system("pip install --no-input diffusers gradio_imageslider huggingface-hub")
|
9 |
+
|
10 |
+
# --- Standard Imports ---
|
11 |
import logging
|
12 |
import random
|
13 |
import warnings
|
|
|
14 |
import io
|
15 |
import base64
|
16 |
+
|
17 |
import gradio as gr
|
18 |
import numpy as np
|
19 |
import spaces
|
|
|
53 |
# --- Authentication and Model Download ---
|
54 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
55 |
|
56 |
+
flux_model_id = "black-forest-labs/FLUX.1-dev"
|
57 |
controlnet_model_id = "jasperai/Flux.1-dev-Controlnet-Upscaler"
|
58 |
+
local_model_dir = flux_model_id.split('/')[-1]
|
59 |
pipe = None
|
60 |
|
61 |
try:
|
|
|
87 |
logging.error(f"Model load failed: {e}", exc_info=True)
|
88 |
raise SystemExit(f"Fatal: could not load models: {e}")
|
89 |
|
90 |
+
# --- Constants & Helper Functions ---
|
|
|
91 |
MAX_SEED = 2**32 - 1
|
92 |
MAX_PIXEL_BUDGET = 1280 * 1280
|
93 |
INTERNAL_PROCESSING_FACTOR = 4
|
94 |
|
95 |
def process_input(input_image):
|
96 |
if input_image is None:
|
97 |
+
raise gr.Error("No input image provided!")
|
98 |
input_image = ImageOps.exif_transpose(input_image)
|
99 |
if input_image.mode != 'RGB':
|
100 |
input_image = input_image.convert('RGB')
|
101 |
w, h = input_image.size
|
102 |
+
|
103 |
+
# Check intermediate budget
|
104 |
target_pixels = (w*INTERNAL_PROCESSING_FACTOR)*(h*INTERNAL_PROCESSING_FACTOR)
|
105 |
if target_pixels > MAX_PIXEL_BUDGET:
|
106 |
max_in = MAX_PIXEL_BUDGET / (INTERNAL_PROCESSING_FACTOR**2)
|
|
|
110 |
was_resized = True
|
111 |
else:
|
112 |
was_resized = False
|
113 |
+
|
114 |
+
# Round to multiple of 8
|
115 |
w2, h2 = input_image.size
|
116 |
+
w2 -= w2 % 8; h2 -= h2 % 8
|
117 |
if input_image.size != (w2,h2):
|
118 |
input_image = input_image.resize((w2,h2), Image.Resampling.LANCZOS)
|
119 |
+
|
120 |
return input_image, w, h, was_resized
|
121 |
|
122 |
@spaces.GPU(duration=75)
|
|
|
136 |
seed = random.randint(0, MAX_SEED)
|
137 |
seed = int(seed)
|
138 |
final_upscale_factor = int(final_upscale_factor)
|
139 |
+
|
140 |
processed, w0, h0, resized_flag = process_input(input_image)
|
141 |
w_proc, h_proc = processed.size
|
142 |
+
|
143 |
cw, ch = w_proc*INTERNAL_PROCESSING_FACTOR, h_proc*INTERNAL_PROCESSING_FACTOR
|
144 |
control_img = processed.resize((cw, ch), Image.Resampling.LANCZOS)
|
145 |
+
|
146 |
generator = torch.Generator(device=device).manual_seed(seed)
|
147 |
with torch.inference_mode():
|
148 |
result = pipe(
|
|
|
154 |
height=ch, width=cw,
|
155 |
generator=generator,
|
156 |
).images[0]
|
157 |
+
|
158 |
+
# Final resize
|
159 |
if resized_flag:
|
160 |
final_w, final_h = w_proc*final_upscale_factor, h_proc*final_upscale_factor
|
161 |
else:
|
162 |
final_w, final_h = w0*final_upscale_factor, h0*final_upscale_factor
|
163 |
+
|
164 |
if (final_w, final_h) != result.size:
|
165 |
result = result.resize((final_w, final_h), Image.Resampling.LANCZOS)
|
166 |
+
|
167 |
buf = io.BytesIO()
|
168 |
result.save(buf, format="WEBP", quality=90)
|
169 |
b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
|
|
170 |
|
171 |
+
return [[input_image, result], seed, f"data:image/webp;base64,{b64}"]
|
172 |
|
173 |
+
# --- Gradio Interface ---
|
174 |
with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Flux Upscaler Demo") as demo:
|
175 |
gr.Markdown(f"""
|
176 |
# ⚡ Flux.1‑dev Upscaler
|
|
|
215 |
api_name="upscale"
|
216 |
)
|
217 |
|
218 |
+
# Expose JSON API at /run/upscale
|
219 |
demo.queue(max_size=10).launch(share=False, show_api=True)
|