Surn commited on
Commit
ca6fa71
·
1 Parent(s): 4e27a59

Add permalink_short textbox

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -157,12 +157,11 @@ def load_data(request: gr.Request, model_3d, image_slider):
157
  query_params = _resolve_short_id_to_query_params(query_params)
158
 
159
  # Extract URLs from query parameters (which may have been updated)
 
160
  model_url = query_params.get("3d", None)
161
  hm_url = query_params.get("hm", None)
162
  img_url = query_params.get("image", None)
163
 
164
- # Check if any relevant parameters are set to decide if we should clear query_params for permalink generation
165
- # This logic ensures that if only 'sid' was passed and it resolved, query_params will now contain 3d, hm, image.
166
  # If 'sid' was passed but didn't resolve, or no params were passed, then model_url, hm_url, img_url will be None.
167
  has_loadable_params = bool(model_url or hm_url or img_url)
168
 
@@ -199,17 +198,25 @@ def load_data(request: gr.Request, model_3d, image_slider):
199
 
200
  # If any loadable query parameters were effectively present (either directly or via sid), generate a permalink.
201
  permalink = ""
 
202
  if has_loadable_params:
 
203
  try:
204
  permalink_model_url = query_params.get("3d", model_url)
205
  permalink_hm_url = query_params.get("hm", hm_url if len(slider_images) > 1 and hm_url else (slider_images[1] if len(slider_images) > 1 else None) )
206
  permalink_img_url = query_params.get("image", img_url if slider_images and img_url else (slider_images[0] if slider_images else None) )
207
-
208
  permalink = storage.generate_permalink_from_urls(permalink_model_url, permalink_hm_url, permalink_img_url)
 
 
 
 
 
209
  except Exception as e:
210
  print(f"Error generating permalink: {e}")
 
 
211
 
212
- return model_url, slider_images, permalink
213
 
214
  def process_upload(files, current_model, current_images):
215
  """
@@ -368,16 +375,25 @@ with gr.Blocks(css_paths="style_20250503.css", title="3D viewer", theme='Surn/be
368
  key="permalink",
369
  elem_classes="solid centered",
370
  max_lines=5,
371
- lines=3
372
  )
373
  gr.Markdown("### Copy the permalink to share your model and images.", elem_classes="solid centered",)
 
 
 
 
 
 
 
 
 
374
  with gr.Row():
375
  gr.HTML(value=getVersions(), visible=True, elem_id="versions")
376
 
377
  viewer3d.load(
378
  load_data,
379
  inputs=[model_3d, image_slider],
380
- outputs=[model_3d, image_slider, permalink],
381
  scroll_to_output=True
382
  ).then(
383
  lambda link: (gr.update(visible=True), gr.update(interactive=False))
 
157
  query_params = _resolve_short_id_to_query_params(query_params)
158
 
159
  # Extract URLs from query parameters (which may have been updated)
160
+ short_id = query_params.get("sid", None)
161
  model_url = query_params.get("3d", None)
162
  hm_url = query_params.get("hm", None)
163
  img_url = query_params.get("image", None)
164
 
 
 
165
  # If 'sid' was passed but didn't resolve, or no params were passed, then model_url, hm_url, img_url will be None.
166
  has_loadable_params = bool(model_url or hm_url or img_url)
167
 
 
198
 
199
  # If any loadable query parameters were effectively present (either directly or via sid), generate a permalink.
200
  permalink = ""
201
+ permalink_short = ""
202
  if has_loadable_params:
203
+ # Generate permalink using the processed URLs.
204
  try:
205
  permalink_model_url = query_params.get("3d", model_url)
206
  permalink_hm_url = query_params.get("hm", hm_url if len(slider_images) > 1 and hm_url else (slider_images[1] if len(slider_images) > 1 else None) )
207
  permalink_img_url = query_params.get("image", img_url if slider_images and img_url else (slider_images[0] if slider_images else None) )
 
208
  permalink = storage.generate_permalink_from_urls(permalink_model_url, permalink_hm_url, permalink_img_url)
209
+ if not short_id:
210
+ # If no short ID was provided, generate a new one.
211
+ result, short_id = storage.gen_full_url(full_url=permalink, repo_id=constants.HF_REPO_ID, json_file=constants.SHORTENER_JSON_FILE)
212
+ permalink_short = f"{constants.APP_BASE_URL}/?sid={short_id}"
213
+ print(f"Generated permalink: {result} (short ID: {short_id})")
214
  except Exception as e:
215
  print(f"Error generating permalink: {e}")
216
+
217
+
218
 
219
+ return model_url, slider_images, permalink, permalink_short
220
 
221
  def process_upload(files, current_model, current_images):
222
  """
 
375
  key="permalink",
376
  elem_classes="solid centered",
377
  max_lines=5,
378
+ lines=4
379
  )
380
  gr.Markdown("### Copy the permalink to share your model and images.", elem_classes="solid centered",)
381
+ permalink_short = gr.Textbox(
382
+ show_copy_button=True,
383
+ label="Shortened Permalink",
384
+ elem_id="short_permalink",
385
+ key="permalink",
386
+ elem_classes="solid centered",
387
+ max_lines=5,
388
+ lines=3
389
+ )
390
  with gr.Row():
391
  gr.HTML(value=getVersions(), visible=True, elem_id="versions")
392
 
393
  viewer3d.load(
394
  load_data,
395
  inputs=[model_3d, image_slider],
396
+ outputs=[model_3d, image_slider, permalink, permalink_short],
397
  scroll_to_output=True
398
  ).then(
399
  lambda link: (gr.update(visible=True), gr.update(interactive=False))