Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,49 +149,46 @@ def upscale_image(image, model_selection: str, progress=gr.Progress(track_tqdm=T
|
|
| 149 |
print(f"An error occurred: {traceback.format_exc()}")
|
| 150 |
raise gr.Error(f"An error occurred during processing: {e}")
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@spaces.GPU
|
| 153 |
def upscale_with_all_models(image, progress=gr.Progress(track_tqdm=True)):
|
| 154 |
-
"""Upscale image with all available models and return results as HTML."""
|
| 155 |
if image is None:
|
| 156 |
raise gr.Error("No image uploaded. Please upload an image to upscale.")
|
| 157 |
-
|
| 158 |
try:
|
|
|
|
| 159 |
original = load_image(image)
|
| 160 |
-
|
|
|
|
| 161 |
total_models = len(MODELS)
|
| 162 |
|
| 163 |
-
for idx, (model_name,
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
upscaler = get_upscaler(model_name)
|
| 167 |
-
upscaled = upscaler(original, tiling=True, tile_width=1024, tile_height=1024)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
progress(1.0, desc="All
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
html = '<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px;">'
|
| 178 |
-
for img_path, model_name in all_results:
|
| 179 |
-
html += f'''
|
| 180 |
-
<div style="border: 1px solid #ddd; padding: 10px; border-radius: 8px;">
|
| 181 |
-
<img src="file={img_path}" style="width: 100%; height: auto; border-radius: 4px;">
|
| 182 |
-
<p style="text-align: center; margin-top: 8px; font-weight: bold;">{model_name}</p>
|
| 183 |
-
</div>
|
| 184 |
-
'''
|
| 185 |
-
html += '</div>'
|
| 186 |
-
|
| 187 |
-
return html
|
| 188 |
-
|
| 189 |
except Exception as e:
|
| 190 |
print(f"An error occurred: {traceback.format_exc()}")
|
| 191 |
-
raise gr.Error(f"An error occurred during processing: {e}")
|
| 192 |
-
|
| 193 |
-
def clear_outputs():
|
| 194 |
-
return None, None
|
| 195 |
|
| 196 |
# --- Gradio Interface Definition ---
|
| 197 |
title = """<h1 align="center">Image Upscaler</h1>
|
|
@@ -213,7 +210,7 @@ with gr.Blocks(delete_cache=(3600, 3600)) as demo:
|
|
| 213 |
label="Model (alphabetically sorted)",
|
| 214 |
)
|
| 215 |
run_button = gr.Button("Upscale", variant="primary")
|
| 216 |
-
|
| 217 |
|
| 218 |
with gr.Column(scale=2):
|
| 219 |
result_slider = ImageSlider(
|
|
@@ -230,9 +227,19 @@ with gr.Blocks(delete_cache=(3600, 3600)) as demo:
|
|
| 230 |
)
|
| 231 |
|
| 232 |
download_output = gr.File(label="Download Full-Quality Upscaled Image (Lossless PNG)")
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
# --- Event Handling ---
|
| 238 |
run_button.click(
|
|
@@ -246,10 +253,15 @@ with gr.Blocks(delete_cache=(3600, 3600)) as demo:
|
|
| 246 |
outputs=[result_slider, download_output],
|
| 247 |
)
|
| 248 |
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
fn=upscale_with_all_models,
|
| 251 |
inputs=[input_image],
|
| 252 |
-
outputs=
|
| 253 |
)
|
| 254 |
|
| 255 |
# --- Pre-load the default model for a faster first-time user experience ---
|
|
|
|
| 149 |
print(f"An error occurred: {traceback.format_exc()}")
|
| 150 |
raise gr.Error(f"An error occurred during processing: {e}")
|
| 151 |
|
| 152 |
+
def clear_outputs():
|
| 153 |
+
return None, None
|
| 154 |
+
|
| 155 |
+
# --- Batch Upscaling Function ---
|
| 156 |
@spaces.GPU
|
| 157 |
def upscale_with_all_models(image, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 158 |
if image is None:
|
| 159 |
raise gr.Error("No image uploaded. Please upload an image to upscale.")
|
| 160 |
+
|
| 161 |
try:
|
| 162 |
+
progress(0, desc="Loading image...")
|
| 163 |
original = load_image(image)
|
| 164 |
+
|
| 165 |
+
results = []
|
| 166 |
total_models = len(MODELS)
|
| 167 |
|
| 168 |
+
for idx, (model_name, model_path) in enumerate(MODELS.items()):
|
| 169 |
+
progress_val = idx / total_models
|
| 170 |
+
progress(progress_val, desc=f"Processing with {model_name} ({idx+1}/{total_models})...")
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
try:
|
| 173 |
+
upscaler = get_upscaler(model_name)
|
| 174 |
+
upscaled_pil_image = upscaler(original, tiling=True, tile_width=1024, tile_height=1024)
|
| 175 |
+
|
| 176 |
+
# Save result with model name label
|
| 177 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 178 |
+
upscaled_pil_image.save(temp_file.name, "PNG")
|
| 179 |
+
results.append((temp_file.name, model_name))
|
| 180 |
+
|
| 181 |
+
except Exception as e:
|
| 182 |
+
print(f"Error processing {model_name}: {e}")
|
| 183 |
+
# Continue with next model even if one fails
|
| 184 |
+
continue
|
| 185 |
|
| 186 |
+
progress(1.0, desc="All models processed!")
|
| 187 |
+
return results
|
| 188 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
except Exception as e:
|
| 190 |
print(f"An error occurred: {traceback.format_exc()}")
|
| 191 |
+
raise gr.Error(f"An error occurred during batch processing: {e}")
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
# --- Gradio Interface Definition ---
|
| 194 |
title = """<h1 align="center">Image Upscaler</h1>
|
|
|
|
| 210 |
label="Model (alphabetically sorted)",
|
| 211 |
)
|
| 212 |
run_button = gr.Button("Upscale", variant="primary")
|
| 213 |
+
batch_button = gr.Button("Upscale with All Models (takes time!)", variant="secondary")
|
| 214 |
|
| 215 |
with gr.Column(scale=2):
|
| 216 |
result_slider = ImageSlider(
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
download_output = gr.File(label="Download Full-Quality Upscaled Image (Lossless PNG)")
|
| 230 |
+
|
| 231 |
+
# --- Gallery for batch results ---
|
| 232 |
+
with gr.Row():
|
| 233 |
+
with gr.Column():
|
| 234 |
+
gr.Markdown("### Batch Processing Results")
|
| 235 |
+
batch_gallery = gr.Gallery(
|
| 236 |
+
label="All Models Results",
|
| 237 |
+
show_label=True,
|
| 238 |
+
columns=4,
|
| 239 |
+
rows=None,
|
| 240 |
+
height="auto",
|
| 241 |
+
object_fit="contain"
|
| 242 |
+
)
|
| 243 |
|
| 244 |
# --- Event Handling ---
|
| 245 |
run_button.click(
|
|
|
|
| 253 |
outputs=[result_slider, download_output],
|
| 254 |
)
|
| 255 |
|
| 256 |
+
batch_button.click(
|
| 257 |
+
fn=lambda: None,
|
| 258 |
+
inputs=None,
|
| 259 |
+
outputs=batch_gallery,
|
| 260 |
+
queue=False
|
| 261 |
+
).then(
|
| 262 |
fn=upscale_with_all_models,
|
| 263 |
inputs=[input_image],
|
| 264 |
+
outputs=batch_gallery,
|
| 265 |
)
|
| 266 |
|
| 267 |
# --- Pre-load the default model for a faster first-time user experience ---
|