Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,170 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
-
import os
|
| 4 |
-
import shutil
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import spaces
|
| 8 |
-
|
| 9 |
-
# -----------------------------------------------------------------------------
|
| 10 |
-
# CONFIGURE THESE PATHS TO MATCH YOUR PROJECT STRUCTURE
|
| 11 |
-
# -----------------------------------------------------------------------------
|
| 12 |
-
|
| 13 |
-
INPUT_DIR = "samples"
|
| 14 |
-
OUTPUT_DIR = "inference_results/coz_vlmprompt"
|
| 15 |
-
|
| 16 |
-
# -----------------------------------------------------------------------------
|
| 17 |
-
# HELPER FUNCTION TO RUN INFERENCE AND RETURN THE OUTPUT IMAGE
|
| 18 |
-
# -----------------------------------------------------------------------------
|
| 19 |
-
|
| 20 |
-
@spaces.GPU()
|
| 21 |
-
def run_with_upload(uploaded_image_path):
|
| 22 |
-
"""
|
| 23 |
-
1) Clear out INPUT_DIR (so old samples don’t linger).
|
| 24 |
-
2) Copy the uploaded image into INPUT_DIR.
|
| 25 |
-
3) Run your inference_coz.py command (which reads from -i INPUT_DIR).
|
| 26 |
-
4) After it finishes, find the most recently‐modified PNG in OUTPUT_DIR.
|
| 27 |
-
5) Return a PIL.Image, which Gradio will display.
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
# 1) Make sure INPUT_DIR exists; if it does, delete everything inside.
|
| 31 |
-
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 32 |
-
for fn in os.listdir(INPUT_DIR):
|
| 33 |
-
full_path = os.path.join(INPUT_DIR, fn)
|
| 34 |
-
try:
|
| 35 |
-
if os.path.isfile(full_path) or os.path.islink(full_path):
|
| 36 |
-
os.remove(full_path)
|
| 37 |
-
elif os.path.isdir(full_path):
|
| 38 |
-
shutil.rmtree(full_path)
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"Warning: could not delete {full_path}: {e}")
|
| 41 |
-
|
| 42 |
-
# 2) Copy the uploaded image into INPUT_DIR.
|
| 43 |
-
# Gradio will give us a path like "/tmp/gradio_xyz.png"
|
| 44 |
-
if uploaded_image_path is None:
|
| 45 |
-
return None
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
# Open with PIL (this handles JPEG, BMP, TIFF, etc.)
|
| 49 |
-
pil_img = Image.open(uploaded_image_path).convert("RGB")
|
| 50 |
-
except Exception as e:
|
| 51 |
-
print(f"Error: could not open uploaded image: {e}")
|
| 52 |
-
return None
|
| 53 |
-
|
| 54 |
-
# Save it as "input.png" in our INPUT_DIR
|
| 55 |
-
save_path = Path(INPUT_DIR) / "input.png"
|
| 56 |
-
try:
|
| 57 |
-
pil_img.save(save_path, format="PNG")
|
| 58 |
-
except Exception as e:
|
| 59 |
-
print(f"Error: could not save as PNG: {e}")
|
| 60 |
-
return None
|
| 61 |
-
|
| 62 |
-
# 3) Build and run your inference_coz.py command.
|
| 63 |
-
# This will block until it completes.
|
| 64 |
-
cmd = [
|
| 65 |
-
"python", "inference_coz.py",
|
| 66 |
-
"-i", INPUT_DIR,
|
| 67 |
-
"-o", OUTPUT_DIR,
|
| 68 |
-
"--rec_type", "recursive_multiscale",
|
| 69 |
-
"--prompt_type", "vlm",
|
| 70 |
-
"--upscale", "2",
|
| 71 |
-
"--lora_path", "ckpt/SR_LoRA/model_20001.pkl",
|
| 72 |
-
"--vae_path", "ckpt/SR_VAE/vae_encoder_20001.pt",
|
| 73 |
-
"--pretrained_model_name_or_path", "stabilityai/stable-diffusion-3-medium-diffusers",
|
| 74 |
-
"--ram_ft_path", "ckpt/DAPE/DAPE.pth",
|
| 75 |
-
"--ram_path", "ckpt/RAM/ram_swin_large_14m.pth"
|
| 76 |
-
]
|
| 77 |
-
try:
|
| 78 |
-
subprocess.run(cmd, check=True)
|
| 79 |
-
except subprocess.CalledProcessError as err:
|
| 80 |
-
# If inference_coz.py crashes, we can print/log the error.
|
| 81 |
-
print("Inference failed:", err)
|
| 82 |
-
return None
|
| 83 |
-
|
| 84 |
-
# 4) After it finishes, scan OUTPUT_DIR for .png files.
|
| 85 |
-
|
| 86 |
-
RECUSIVE_DIR = f'{OUTPUT_DIR}/recursive'
|
| 87 |
-
|
| 88 |
-
if not os.path.isdir(RECUSIVE_DIR):
|
| 89 |
-
return None
|
| 90 |
-
|
| 91 |
-
png_files = [
|
| 92 |
-
os.path.join(RECUSIVE_DIR, fn)
|
| 93 |
-
for fn in os.listdir(RECUSIVE_DIR)
|
| 94 |
-
if fn.lower().endswith(".png")
|
| 95 |
-
]
|
| 96 |
-
if not png_files:
|
| 97 |
-
return None
|
| 98 |
-
|
| 99 |
-
# 5) Pick the most recently‐modified PNG
|
| 100 |
-
latest_png = max(png_files, key=os.path.getmtime)
|
| 101 |
-
|
| 102 |
-
# 6) Open and return a PIL.Image. Gradio will display it automatically.
|
| 103 |
-
try:
|
| 104 |
-
img = Image.open(latest_png).convert("RGB")
|
| 105 |
-
except Exception as e:
|
| 106 |
-
print(f"Error opening {latest_png}: {e}")
|
| 107 |
-
return None
|
| 108 |
-
|
| 109 |
-
return img
|
| 110 |
-
|
| 111 |
-
# -----------------------------------------------------------------------------
|
| 112 |
-
# BUILD THE GRADIO INTERFACE
|
| 113 |
-
# -----------------------------------------------------------------------------
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
#
|
| 143 |
-
#
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import spaces
|
| 8 |
+
|
| 9 |
+
# -----------------------------------------------------------------------------
|
| 10 |
+
# CONFIGURE THESE PATHS TO MATCH YOUR PROJECT STRUCTURE
|
| 11 |
+
# -----------------------------------------------------------------------------
|
| 12 |
+
|
| 13 |
+
INPUT_DIR = "samples"
|
| 14 |
+
OUTPUT_DIR = "inference_results/coz_vlmprompt"
|
| 15 |
+
|
| 16 |
+
# -----------------------------------------------------------------------------
|
| 17 |
+
# HELPER FUNCTION TO RUN INFERENCE AND RETURN THE OUTPUT IMAGE
|
| 18 |
+
# -----------------------------------------------------------------------------
|
| 19 |
+
|
| 20 |
+
@spaces.GPU()
|
| 21 |
+
def run_with_upload(uploaded_image_path):
|
| 22 |
+
"""
|
| 23 |
+
1) Clear out INPUT_DIR (so old samples don’t linger).
|
| 24 |
+
2) Copy the uploaded image into INPUT_DIR.
|
| 25 |
+
3) Run your inference_coz.py command (which reads from -i INPUT_DIR).
|
| 26 |
+
4) After it finishes, find the most recently‐modified PNG in OUTPUT_DIR.
|
| 27 |
+
5) Return a PIL.Image, which Gradio will display.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
# 1) Make sure INPUT_DIR exists; if it does, delete everything inside.
|
| 31 |
+
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 32 |
+
for fn in os.listdir(INPUT_DIR):
|
| 33 |
+
full_path = os.path.join(INPUT_DIR, fn)
|
| 34 |
+
try:
|
| 35 |
+
if os.path.isfile(full_path) or os.path.islink(full_path):
|
| 36 |
+
os.remove(full_path)
|
| 37 |
+
elif os.path.isdir(full_path):
|
| 38 |
+
shutil.rmtree(full_path)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Warning: could not delete {full_path}: {e}")
|
| 41 |
+
|
| 42 |
+
# 2) Copy the uploaded image into INPUT_DIR.
|
| 43 |
+
# Gradio will give us a path like "/tmp/gradio_xyz.png"
|
| 44 |
+
if uploaded_image_path is None:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Open with PIL (this handles JPEG, BMP, TIFF, etc.)
|
| 49 |
+
pil_img = Image.open(uploaded_image_path).convert("RGB")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error: could not open uploaded image: {e}")
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
# Save it as "input.png" in our INPUT_DIR
|
| 55 |
+
save_path = Path(INPUT_DIR) / "input.png"
|
| 56 |
+
try:
|
| 57 |
+
pil_img.save(save_path, format="PNG")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error: could not save as PNG: {e}")
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
# 3) Build and run your inference_coz.py command.
|
| 63 |
+
# This will block until it completes.
|
| 64 |
+
cmd = [
|
| 65 |
+
"python", "inference_coz.py",
|
| 66 |
+
"-i", INPUT_DIR,
|
| 67 |
+
"-o", OUTPUT_DIR,
|
| 68 |
+
"--rec_type", "recursive_multiscale",
|
| 69 |
+
"--prompt_type", "vlm",
|
| 70 |
+
"--upscale", "2",
|
| 71 |
+
"--lora_path", "ckpt/SR_LoRA/model_20001.pkl",
|
| 72 |
+
"--vae_path", "ckpt/SR_VAE/vae_encoder_20001.pt",
|
| 73 |
+
"--pretrained_model_name_or_path", "stabilityai/stable-diffusion-3-medium-diffusers",
|
| 74 |
+
"--ram_ft_path", "ckpt/DAPE/DAPE.pth",
|
| 75 |
+
"--ram_path", "ckpt/RAM/ram_swin_large_14m.pth"
|
| 76 |
+
]
|
| 77 |
+
try:
|
| 78 |
+
subprocess.run(cmd, check=True)
|
| 79 |
+
except subprocess.CalledProcessError as err:
|
| 80 |
+
# If inference_coz.py crashes, we can print/log the error.
|
| 81 |
+
print("Inference failed:", err)
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
# 4) After it finishes, scan OUTPUT_DIR for .png files.
|
| 85 |
+
|
| 86 |
+
RECUSIVE_DIR = f'{OUTPUT_DIR}/recursive'
|
| 87 |
+
|
| 88 |
+
if not os.path.isdir(RECUSIVE_DIR):
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
png_files = [
|
| 92 |
+
os.path.join(RECUSIVE_DIR, fn)
|
| 93 |
+
for fn in os.listdir(RECUSIVE_DIR)
|
| 94 |
+
if fn.lower().endswith(".png")
|
| 95 |
+
]
|
| 96 |
+
if not png_files:
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
# 5) Pick the most recently‐modified PNG
|
| 100 |
+
latest_png = max(png_files, key=os.path.getmtime)
|
| 101 |
+
|
| 102 |
+
# 6) Open and return a PIL.Image. Gradio will display it automatically.
|
| 103 |
+
try:
|
| 104 |
+
img = Image.open(latest_png).convert("RGB")
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error opening {latest_png}: {e}")
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
return img
|
| 110 |
+
|
| 111 |
+
# -----------------------------------------------------------------------------
|
| 112 |
+
# BUILD THE GRADIO INTERFACE
|
| 113 |
+
# -----------------------------------------------------------------------------
|
| 114 |
+
|
| 115 |
+
css="""
|
| 116 |
+
#col-container {
|
| 117 |
+
margin: 0 auto;
|
| 118 |
+
max-width: 720px;
|
| 119 |
+
}
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
with gr.Blocks(css=css) as demo:
|
| 123 |
+
|
| 124 |
+
gr.HTML(
|
| 125 |
+
"""
|
| 126 |
+
<div style="text-align: center;">
|
| 127 |
+
<h1>Chain-of-Zoom</h1>
|
| 128 |
+
<p style="font-size:16px;">Extreme Super-Resolution via Scale Autoregression and Preference Alignment </p>
|
| 129 |
+
</div>
|
| 130 |
+
<br>
|
| 131 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 132 |
+
<a href="https://github.com/bryanswkim/Chain-of-Zoom">
|
| 133 |
+
<img src='https://img.shields.io/badge/GitHub-Repo-blue'>
|
| 134 |
+
</a>
|
| 135 |
+
</div>
|
| 136 |
+
"""
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
with gr.Column(elem_id="col-container"):
|
| 140 |
+
gr.Markdown("## Upload an image, then click **Run Inference** to process it.")
|
| 141 |
+
|
| 142 |
+
# 1) Image upload component. We set type="filepath" so the callback
|
| 143 |
+
# (run_with_upload) will receive a local path to the uploaded file.
|
| 144 |
+
upload_image = gr.Image(
|
| 145 |
+
label="Upload your input image",
|
| 146 |
+
type="filepath"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# 2) A button that the user will click to launch inference.
|
| 150 |
+
run_button = gr.Button("Run Inference")
|
| 151 |
+
|
| 152 |
+
# 3) An output <Image> where we will show the final PNG.
|
| 153 |
+
output_image = gr.Image(
|
| 154 |
+
label="Inference Result",
|
| 155 |
+
type="pil" # because run_with_upload() returns a PIL.Image
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Wire the button: when clicked, call run_with_upload(upload_image), put
|
| 159 |
+
# its return value into output_image.
|
| 160 |
+
run_button.click(
|
| 161 |
+
fn=run_with_upload,
|
| 162 |
+
inputs=upload_image,
|
| 163 |
+
outputs=output_image
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# -----------------------------------------------------------------------------
|
| 167 |
+
# START THE GRADIO SERVER
|
| 168 |
+
# -----------------------------------------------------------------------------
|
| 169 |
+
|
| 170 |
+
demo.launch(share=True)
|