lpuzzolo-claude Claude Opus 5 (1M context) commited on
Commit
8cf7a08
·
1 Parent(s): be29cb4

Add 4x upscale button for any History image via PiD-Image-Upscaler API

Browse files

Click an image in the History gallery, then "Upscale 4x (PiD)": the image is
sent to prithivMLmods/PiD-Image-Upscaler's /upscaler_run endpoint, so the model
runs on that Space's hardware instead of ours. With the visitor's OAuth token
(hf_oauth) the ZeroGPU quota for that call is billed to them; without login the
call goes out anonymously.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Files changed (2) hide show
  1. README.md +3 -0
  2. app.py +60 -1
README.md CHANGED
@@ -7,6 +7,9 @@ sdk: gradio
7
  sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: true
 
 
 
10
  short_description: Explore Lichorosario Qwen-Image-2512 LoRAs
11
  tags:
12
  - anycoder
 
7
  sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: true
10
+ hf_oauth: true
11
+ hf_oauth_scopes:
12
+ - inference-api
13
  short_description: Explore Lichorosario Qwen-Image-2512 LoRAs
14
  tags:
15
  - anycoder
app.py CHANGED
@@ -38,7 +38,9 @@ import re
38
  import math
39
  import numpy as np
40
  import traceback
 
41
  from prompt_rewrite import rewrite
 
42
  import hashlib
43
  from functools import partial
44
 
@@ -191,6 +193,39 @@ def update_history(new_images, history):
191
  def clear_history():
192
  """Devuelve una lista vacía para limpiar la galería de historial."""
193
  return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  ### FIN DE LA MODIFICACIÓN 1 ###
195
 
196
 
@@ -884,7 +919,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
884
  <h3 style=\"margin-top: -10px\">LoRA🦜 ChoquinLabs Explorer</h3>""",
885
  elem_id="title",
886
  )
887
-
 
 
 
 
888
  selected_index = gr.State(None)
889
 
890
  with gr.Row():
@@ -977,6 +1016,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
977
  height="auto",
978
  interactive=False
979
  )
 
 
 
 
 
 
 
980
  ### FIN DE LA MODIFICACIÓN 2 ###
981
 
982
  with gr.Row():
@@ -1112,6 +1158,19 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
1112
  inputs=None,
1113
  outputs=[history_state, history_gallery]
1114
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
1115
  ### FIN DE LA MODIFICACIÓN 3 ###
1116
 
1117
  aspect_ratio.change(
 
38
  import math
39
  import numpy as np
40
  import traceback
41
+ import tempfile
42
  from prompt_rewrite import rewrite
43
+ from gradio_client import Client, handle_file
44
  import hashlib
45
  from functools import partial
46
 
 
193
  def clear_history():
194
  """Devuelve una lista vacía para limpiar la galería de historial."""
195
  return []
196
+
197
+
198
+ PID_UPSCALER_SPACE = "prithivMLmods/PiD-Image-Upscaler"
199
+
200
+
201
+ def select_history_image(history, evt: gr.SelectData):
202
+ """Guarda la imagen del historial que se acaba de clickear."""
203
+ if not history or evt.index is None or evt.index >= len(history):
204
+ return None
205
+ return history[evt.index][0]
206
+
207
+
208
+ def upscale_history_image(image, oauth_token: gr.OAuthToken | None = None):
209
+ """Escala 4x la imagen elegida del historial llamando a la API del space
210
+ prithivMLmods/PiD-Image-Upscaler: el modelo corre en el hardware de ese
211
+ space, no en el nuestro. Con el token OAuth del visitante la cuota ZeroGPU
212
+ de esa llamada se le factura a él; sin login sale anónima."""
213
+ if image is None:
214
+ raise gr.Error("Click an image in the History gallery first.")
215
+
216
+ input_path = os.path.join(tempfile.mkdtemp(), "upscale_input.png")
217
+ image.save(input_path)
218
+
219
+ try:
220
+ client = Client(PID_UPSCALER_SPACE, hf_token=getattr(oauth_token, "token", None))
221
+ _, slider = client.predict(handle_file(input_path), "", api_name="/upscaler_run")
222
+ except Exception as e:
223
+ raise gr.Error(f"PiD upscaler failed: {e}")
224
+
225
+ # El endpoint remoto devuelve el gr.update(...) sin resolver: un dict
226
+ # {"value": [original, upscalada]}, no la tupla ya lista.
227
+ original, upscaled = slider["value"]
228
+ return gr.update(visible=True, value=(original, upscaled))
229
  ### FIN DE LA MODIFICACIÓN 1 ###
230
 
231
 
 
919
  <h3 style=\"margin-top: -10px\">LoRA🦜 ChoquinLabs Explorer</h3>""",
920
  elem_id="title",
921
  )
922
+
923
+ # Login opcional: si el visitante entra con su cuenta, el upscale 4x se
924
+ # cobra a su cupo de ZeroGPU en vez de salir anónimo (cupo por IP).
925
+ gr.LoginButton(size="sm")
926
+
927
  selected_index = gr.State(None)
928
 
929
  with gr.Row():
 
1016
  height="auto",
1017
  interactive=False
1018
  )
1019
+
1020
+ selected_history_image = gr.State(None)
1021
+ upscale_button = gr.Button("🔍 Upscale 4x (PiD)", size="sm")
1022
+ upscaled_slider = gr.ImageSlider(
1023
+ label="Original ↔ PiD 4x upscale",
1024
+ visible=False
1025
+ )
1026
  ### FIN DE LA MODIFICACIÓN 2 ###
1027
 
1028
  with gr.Row():
 
1158
  inputs=None,
1159
  outputs=[history_state, history_gallery]
1160
  )
1161
+
1162
+ # Upscale 4x de cualquier imagen del historial, vía API del space PiD
1163
+ history_gallery.select(
1164
+ fn=select_history_image,
1165
+ inputs=[history_state],
1166
+ outputs=[selected_history_image]
1167
+ )
1168
+
1169
+ upscale_button.click(
1170
+ fn=upscale_history_image,
1171
+ inputs=[selected_history_image],
1172
+ outputs=[upscaled_slider]
1173
+ )
1174
  ### FIN DE LA MODIFICACIÓN 3 ###
1175
 
1176
  aspect_ratio.change(