mertizci commited on
Commit
fc33683
·
verified ·
1 Parent(s): ac0ed95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -11,6 +11,8 @@ import struct
11
  import termios
12
  from pathlib import Path
13
 
 
 
14
 
15
  def set_terminal_size(fd, rows=24, cols=80):
16
  size = struct.pack('HHHH', rows, cols, 0, 0)
@@ -104,10 +106,14 @@ def make_terminal_html(content, cursor=True):
104
 
105
 
106
  def remove_watermark(file_obj, strength, steps):
107
- """Generator: yields (terminal_html, path_for_state)."""
108
  if file_obj is None:
109
  raise gr.Error("Please upload an image first.")
110
 
 
 
 
 
111
  input_path = Path(file_obj)
112
  suffix = input_path.suffix or ".png"
113
  tmp_dir = tempfile.mkdtemp()
@@ -140,25 +146,29 @@ def remove_watermark(file_obj, strength, steps):
140
  if is_done:
141
  display = escape_html(screen_text)
142
  if output_path.exists():
 
 
 
143
  display += "\n\n<span style='color:#50fa7b;font-weight:bold'>✅ Done! Click 📥 Download below.</span>"
144
- yield make_terminal_html(cmd_html + "\n\n" + display, cursor=False), str(output_path)
145
  else:
146
  display += "\n\n<span style='color:#ff5555;font-weight:bold'>❌ Error: output file not created.</span>"
147
- yield make_terminal_html(cmd_html + "\n\n" + display, cursor=False), ""
148
  return
149
 
150
  if now - last_yield > 0.5:
151
  display = escape_html(screen_text)
152
- yield make_terminal_html(cmd_html + "\n\n" + display), ""
153
  last_yield = now
154
 
155
 
156
- def do_download(path_str):
157
- """Reads path from State, returns file for gr.File."""
158
- print(f"[DEBUG] do_download called with path_str='{path_str}'")
159
- if path_str and os.path.exists(path_str):
160
- return path_str
161
- raise gr.Error("File not ready yet. Please wait for processing to complete, then click download.")
 
 
162
 
163
 
164
  def check_ai_metadata(file_obj):
@@ -258,23 +268,19 @@ with gr.Blocks(title="noai-watermark") as demo:
258
  run_btn = gr.Button("🚀 Remove Watermark", variant="primary", size="lg")
259
  wm_terminal = gr.HTML(value=make_terminal_html("<span style='color:#565f89'>Waiting for input...</span>"))
260
 
261
- # State stores path per-session
262
- path_state = gr.State(value="")
263
-
264
  download_btn = gr.Button("📥 Download Cleaned Image", variant="secondary")
265
  download_output = gr.File(label="Cleaned Image", interactive=False)
266
 
267
- # Generator → terminal + state
268
  run_btn.click(
269
  fn=remove_watermark,
270
  inputs=[upload_wm, strength, steps],
271
- outputs=[wm_terminal, path_state],
272
  )
273
 
274
- # Download: state → file (completely independent event)
275
  download_btn.click(
276
  fn=do_download,
277
- inputs=[path_state],
278
  outputs=[download_output],
279
  )
280
 
 
11
  import termios
12
  from pathlib import Path
13
 
14
+ LAST_OUTPUT_FILE = "/tmp/noai_last_output.txt"
15
+
16
 
17
  def set_terminal_size(fd, rows=24, cols=80):
18
  size = struct.pack('HHHH', rows, cols, 0, 0)
 
106
 
107
 
108
  def remove_watermark(file_obj, strength, steps):
109
+ """Generator: yields terminal HTML only. Writes output path to disk."""
110
  if file_obj is None:
111
  raise gr.Error("Please upload an image first.")
112
 
113
+ # Clear previous output
114
+ if os.path.exists(LAST_OUTPUT_FILE):
115
+ os.remove(LAST_OUTPUT_FILE)
116
+
117
  input_path = Path(file_obj)
118
  suffix = input_path.suffix or ".png"
119
  tmp_dir = tempfile.mkdtemp()
 
146
  if is_done:
147
  display = escape_html(screen_text)
148
  if output_path.exists():
149
+ # Write path to disk
150
+ with open(LAST_OUTPUT_FILE, "w") as f:
151
+ f.write(str(output_path))
152
  display += "\n\n<span style='color:#50fa7b;font-weight:bold'>✅ Done! Click 📥 Download below.</span>"
 
153
  else:
154
  display += "\n\n<span style='color:#ff5555;font-weight:bold'>❌ Error: output file not created.</span>"
155
+ yield make_terminal_html(cmd_html + "\n\n" + display, cursor=False)
156
  return
157
 
158
  if now - last_yield > 0.5:
159
  display = escape_html(screen_text)
160
+ yield make_terminal_html(cmd_html + "\n\n" + display)
161
  last_yield = now
162
 
163
 
164
+ def do_download():
165
+ """Reads path from disk, returns file."""
166
+ if os.path.exists(LAST_OUTPUT_FILE):
167
+ with open(LAST_OUTPUT_FILE) as f:
168
+ path = f.read().strip()
169
+ if path and os.path.exists(path):
170
+ return path
171
+ raise gr.Error("No file available. Please run watermark removal first.")
172
 
173
 
174
  def check_ai_metadata(file_obj):
 
268
  run_btn = gr.Button("🚀 Remove Watermark", variant="primary", size="lg")
269
  wm_terminal = gr.HTML(value=make_terminal_html("<span style='color:#565f89'>Waiting for input...</span>"))
270
 
271
+ # Download: completely independent, reads from disk
 
 
272
  download_btn = gr.Button("📥 Download Cleaned Image", variant="secondary")
273
  download_output = gr.File(label="Cleaned Image", interactive=False)
274
 
 
275
  run_btn.click(
276
  fn=remove_watermark,
277
  inputs=[upload_wm, strength, steps],
278
+ outputs=[wm_terminal],
279
  )
280
 
 
281
  download_btn.click(
282
  fn=do_download,
283
+ inputs=[],
284
  outputs=[download_output],
285
  )
286