alexnasa commited on
Commit
ba815e8
Β·
verified Β·
1 Parent(s): 7f5e4af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -112
app.py CHANGED
@@ -3,9 +3,11 @@ import subprocess
3
  import os
4
  import shutil
5
  from pathlib import Path
 
6
  from PIL import Image, ImageDraw
7
  import spaces
8
 
 
9
  # ------------------------------------------------------------------
10
  # CONFIGURE THESE PATHS TO MATCH YOUR PROJECT STRUCTURE
11
  # ------------------------------------------------------------------
@@ -85,102 +87,13 @@ def make_preview_with_boxes(image_path: str, scale_option: str) -> Image.Image:
85
  return base
86
 
87
 
88
- # ------------------------------------------------------------------
89
- # HELPER FUNCTIONS FOR INFERENCE & CAPTION (unchanged from your original)
90
- # ------------------------------------------------------------------
91
  @spaces.GPU(duration=120)
92
  def run_with_upload(uploaded_image_path, upscale_option):
93
- """
94
- 1) Clear INPUT_DIR
95
- 2) Save the uploaded file as input.png in INPUT_DIR
96
- 3) Read `upscale_option` (e.g. "1x", "2x", "4x") β†’ turn it into "1","2","4"
97
- 4) Call inference_coz.py with `--upscale <that_value>`
98
- 5) Return the FOUR output‐PNG file‐paths as a Python list, so that Gradio's Gallery
99
- can display them.
100
- """
101
- # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
102
- # (Copy‐paste exactly your existing code here; no changes needed)
103
- # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
104
-
105
- os.makedirs(INPUT_DIR, exist_ok=True)
106
- for fn in os.listdir(INPUT_DIR):
107
- full_path = os.path.join(INPUT_DIR, fn)
108
- try:
109
- if os.path.isfile(full_path) or os.path.islink(full_path):
110
- os.remove(full_path)
111
- elif os.path.isdir(full_path):
112
- shutil.rmtree(full_path)
113
- except Exception as e:
114
- print(f"Warning: could not delete {full_path}: {e}")
115
-
116
- if uploaded_image_path is None:
117
- return []
118
- try:
119
- pil_img = Image.open(uploaded_image_path).convert("RGB")
120
- except Exception as e:
121
- print(f"Error: could not open uploaded image: {e}")
122
- return []
123
- save_path = Path(INPUT_DIR) / "input.png"
124
- try:
125
- pil_img.save(save_path, format="PNG")
126
- except Exception as e:
127
- print(f"Error: could not save as PNG: {e}")
128
- return []
129
-
130
- upscale_value = upscale_option.replace("x", "") # e.g. "2x" β†’ "2"
131
- cmd = [
132
- "python", "inference_coz.py",
133
- "-i", INPUT_DIR,
134
- "-o", OUTPUT_DIR,
135
- "--rec_type", "recursive_multiscale",
136
- "--prompt_type", "vlm",
137
- "--upscale", upscale_value,
138
- "--lora_path", "ckpt/SR_LoRA/model_20001.pkl",
139
- "--vae_path", "ckpt/SR_VAE/vae_encoder_20001.pt",
140
- "--pretrained_model_name_or_path", "stabilityai/stable-diffusion-3-medium-diffusers",
141
- "--ram_ft_path", "ckpt/DAPE/DAPE.pth",
142
- "--ram_path", "ckpt/RAM/ram_swin_large_14m.pth"
143
- ]
144
- try:
145
- subprocess.run(cmd, check=True)
146
- except subprocess.CalledProcessError as err:
147
- print("Inference failed:", err)
148
- return []
149
-
150
- per_sample_dir = os.path.join(OUTPUT_DIR, "per-sample", "input")
151
- expected_files = [
152
- os.path.join(per_sample_dir, f"{i}.png")
153
- for i in range(1, 5)
154
- ]
155
- for fp in expected_files:
156
- if not os.path.isfile(fp):
157
- print(f"Warning: expected file not found: {fp}")
158
- return []
159
- return expected_files
160
-
161
-
162
- def get_caption(src_gallery, evt: gr.SelectData):
163
- """
164
- Given a clicked‐on image in the gallery, read the corresponding .txt in
165
- .../per-sample/input/txt and return its contents.
166
- """
167
- if not src_gallery or not os.path.isfile(src_gallery[evt.index][0]):
168
- return "No caption available."
169
 
170
- selected_image_path = src_gallery[evt.index][0]
171
- base = os.path.basename(selected_image_path) # e.g. "2.png"
172
- stem = os.path.splitext(base)[0] # e.g. "2"
173
- txt_folder = os.path.join(OUTPUT_DIR, "per-sample", "input", "txt")
174
- txt_path = os.path.join(txt_folder, f"{int(stem) - 1}.txt")
175
 
176
- if not os.path.isfile(txt_path):
177
- return f"Caption file not found: {int(stem) - 1}.txt"
178
- try:
179
- with open(txt_path, "r", encoding="utf-8") as f:
180
- caption = f.read().strip()
181
- return caption if caption else "(Caption file is empty.)"
182
- except Exception as e:
183
- return f"Error reading caption: {e}"
184
 
185
 
186
  # ------------------------------------------------------------------
@@ -248,13 +161,6 @@ with gr.Blocks(css=css) as demo:
248
  columns=[2], rows=[2]
249
  )
250
 
251
- # 6) Textbox under the gallery for showing captions
252
- caption_text = gr.Textbox(
253
- label="Caption",
254
- lines=4,
255
- placeholder="Click on any image above to see its caption here."
256
- )
257
-
258
  # ------------------------------------------------------------------
259
  # CALLBACK #1: Whenever the user uploads or changes the radio, update preview
260
  # ------------------------------------------------------------------
@@ -292,23 +198,10 @@ with gr.Blocks(css=css) as demo:
292
  outputs=[output_gallery]
293
  )
294
 
295
- # ------------------------------------------------------------------
296
- # CALLBACK #3: When an image in the gallery is clicked, show its caption
297
- # ------------------------------------------------------------------
298
-
299
- output_gallery.select(
300
- fn=get_caption,
301
- inputs=[output_gallery],
302
- outputs=[caption_text]
303
- )
304
 
305
  # ------------------------------------------------------------------
306
  # START THE GRADIO SERVER
307
  # ------------------------------------------------------------------
308
 
309
- # πŸ”§ 1) turn the global queue ON and set its default_concurrency_limit to 1
310
- demo.queue(default_concurrency_limit=1, # ≀ 1 worker per event
311
- max_size=20) # optional: allow 20 waiting jobs
312
-
313
  # πŸ”§ 2) launch as usual
314
  demo.launch(share=True)
 
3
  import os
4
  import shutil
5
  from pathlib import Path
6
+ from inference_coz_single import recursive_multiscale_sr
7
  from PIL import Image, ImageDraw
8
  import spaces
9
 
10
+
11
  # ------------------------------------------------------------------
12
  # CONFIGURE THESE PATHS TO MATCH YOUR PROJECT STRUCTURE
13
  # ------------------------------------------------------------------
 
87
  return base
88
 
89
 
 
 
 
90
  @spaces.GPU(duration=120)
91
  def run_with_upload(uploaded_image_path, upscale_option):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
+ upscale_value = upscale_option.replace("x", "") # e.g. "2x" β†’ "2"
94
+
95
+ return recursive_multiscale_sr(uploaded_image_path, int(upscale_value))[0]
 
 
96
 
 
 
 
 
 
 
 
 
97
 
98
 
99
  # ------------------------------------------------------------------
 
161
  columns=[2], rows=[2]
162
  )
163
 
 
 
 
 
 
 
 
164
  # ------------------------------------------------------------------
165
  # CALLBACK #1: Whenever the user uploads or changes the radio, update preview
166
  # ------------------------------------------------------------------
 
198
  outputs=[output_gallery]
199
  )
200
 
 
 
 
 
 
 
 
 
 
201
 
202
  # ------------------------------------------------------------------
203
  # START THE GRADIO SERVER
204
  # ------------------------------------------------------------------
205
 
 
 
 
 
206
  # πŸ”§ 2) launch as usual
207
  demo.launch(share=True)