renderfy commited on
Commit
770c21e
·
verified ·
1 Parent(s): 82f286d

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +63 -64
streamlit_app.py CHANGED
@@ -1,4 +1,5 @@
1
- # streamlit_app.py — AI LightBox · Jewelry (pre-run previews, uniform cards, loop video, vendor-agnostic)
 
2
  import os, io, base64, zipfile, requests, streamlit as st
3
  from PIL import Image
4
 
@@ -64,8 +65,7 @@ def post_video(image_url: str, duration="6", resolution="768P", prompt_optimizer
64
 
65
  @st.cache_data(ttl=300, show_spinner=False)
66
  def fetch_bytes(url_or_path: str):
67
- if not url_or_path:
68
- return None
69
  try:
70
  url = _abs_backend_url(url_or_path)
71
  headers = HEADERS if _needs_auth(url) else None
@@ -130,6 +130,7 @@ def inject_preview_css(aspect: str, fit_mode="contain"):
130
  }}
131
  .lb-video {{ display:flex; justify-content:center; }}
132
  .lb-video video {{ border-radius:8px; max-width: 540px; width:100%; height:auto; }}
 
133
  </style>
134
  """, unsafe_allow_html=True)
135
 
@@ -200,10 +201,10 @@ demo_files = load_local_demo()
200
  # ================= Sayfa =================
201
  st.set_page_config(page_title="AI LightBox · Jewelry", layout="wide", page_icon="💎")
202
  st.title("💎 AI LightBox · Jewelry")
203
- st.caption("Önce preview gösterir; üretince native dosyaları indirtebilirsin. Packshot = arka planı temiz PNG (native).")
204
 
205
  ok = backend_ok()
206
-
207
  # -------- Üst Kontrol Şeridi (Üretim Parametreleri) --------
208
  col1, col2, col3, col4, col5, col6, col7 = st.columns([1.6, 1.6, 1.0, 1.2, 0.9, 1.1, 0.9])
209
  with col1:
@@ -213,10 +214,10 @@ with col2:
213
  with col3:
214
  jewel_type = st.selectbox("Tür", ["auto","earring","ring","necklace","bracelet","pendant","brooch","set","watch"], index=0, key="jewel_type")
215
  with col4:
216
- preview_ratio = st.number_input("Preview ölçeği (0.10–0.95)", min_value=0.10, max_value=0.95, value=0.50, step=0.01,
217
  help="Sadece önizleme kadrajında büyüklüğü etkiler.", key="preview_ratio")
218
  with col5:
219
- upscale = st.checkbox("Super Resolution", False, key="upscale")
220
  with col6:
221
  upscale_stage = st.selectbox("SR aşaması", ["both","final","packshot"], index=0, help="Varsayılan: both", key="upscale_stage")
222
  with col7:
@@ -248,11 +249,11 @@ with colT3:
248
  # Inject CSS (uniform kartlar)
249
  inject_preview_css(preview_aspect, fit_mode="contain" if fit_mode=="fit" else "cover")
250
 
251
- # ================= PRE-RUN PREVIEWS =================
252
  st.subheader("Önizlemeler (Çalıştırmadan önce)")
253
  col_in, col_pack, col_pad, col_vid = st.columns(4)
254
 
255
- # Input bytes (yüklenen ilk dosya veya URL)
256
  input_bytes = None
257
  if file_list:
258
  try: input_bytes = file_list[0].getvalue()
@@ -266,24 +267,24 @@ show_tile_card(col_in, "Input preview",
266
  bytes_data=input_bytes if input_bytes else demo_files.get("input.jpg"),
267
  filename_stub="input", key_prefix="pre")
268
 
269
- # Packshot preview (yerel demo varsa göster)
270
- pack_bytes = demo_files.get("packshot.jpg")
271
  show_tile_card(col_pack, "Packshot preview",
272
- bytes_data=pack_bytes, filename_stub="packshot", key_prefix="pre")
273
 
274
- # Padded preview (packshot.jpg → seçilen kadraja UI-only frame)
275
  padded_bytes = None
276
- if pack_bytes:
277
  try:
278
- padded_bytes = make_preview_frame(pack_bytes, aspect=preview_aspect,
279
  long_edge=int(preview_long_edge),
280
  ratio=float(preview_ratio))
281
  except Exception:
282
  padded_bytes = None
283
- show_tile_card(col_pad, "Padded preview",
284
- bytes_data=padded_bytes or pack_bytes, filename_stub="padded", key_prefix="pre")
285
 
286
- # Video preview (loop)
287
  vid_demo = demo_files.get("tryon.mp4")
288
  col_vid.markdown('<div class="lb-card-title">Video preview (loop)</div>', unsafe_allow_html=True)
289
  if vid_demo:
@@ -310,7 +311,6 @@ if run:
310
  st.error("En az bir görsel girişi yapın (dosya yükle veya URL).")
311
  else:
312
  try:
313
- # Eğer kullanıcı dosya/URL vermezse DEMO input.jpg'i backend’e göndermeyiz; sadece preview içindir.
314
  img_urls = image_url or ""
315
  files_payload = []
316
  if file_list:
@@ -324,7 +324,7 @@ if run:
324
  "image_urls": img_urls,
325
  "mannequin_image_url": "", # opsiyonel
326
  "identity_lock": "true" if identity_lock else "false",
327
- # Preview controls (backend v0.4.0)
328
  "preview_aspect": preview_aspect,
329
  "preview_long_edge": str(int(preview_long_edge)),
330
  "preview_ratio": str(float(preview_ratio)),
@@ -346,23 +346,30 @@ if run:
346
 
347
  st.session_state["job_counter"] += 1
348
 
349
- # ---- ÇIKTILAR ----
350
- # Packshot (native/preview)
351
- pack_prev = None; pack_nat = None
352
- try:
353
- pack_prev = (out["packshot_step"]["result"]["images_preview"] or [{}])[0].get("url")
354
- except Exception: pass
355
  try:
356
- pack_nat = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
357
  except Exception: pass
358
 
359
- # Placed (native/preview)
360
- placed_prev = None; placed_nat = None
361
- try:
362
- placed_prev = (out["image_step"]["result"]["images_preview"] or [{}])[0].get("url")
363
- except Exception: pass
 
 
 
 
 
 
 
 
 
 
364
  try:
365
- placed_nat = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
366
  except Exception: pass
367
 
368
  # Video (zincirde üretildiyse)
@@ -380,10 +387,9 @@ if run:
380
  st.session_state["results"] = {
381
  "job_id": out.get("job_id"),
382
  "schema_version": out.get("schema_version"),
383
- "packshot_preview_url": pack_prev,
384
- "packshot_native_url": pack_nat,
385
- "placed_preview_url": placed_prev,
386
- "placed_native_url": placed_nat,
387
  "video_url": vurl,
388
  "api_base": API_BASE,
389
  "has_auth_header": bool(HEADERS),
@@ -401,31 +407,32 @@ if res:
401
  st.subheader("Üretilen Sonuçlar")
402
  c_pack, c_pad, c_model, c_vid = st.columns(4)
403
 
404
- # Packshot preview (backend generated)
405
  p_url, p_bytes = show_tile_card(
406
- c_pack, "Packshot preview (backend)",
407
- url=res.get("packshot_preview_url"),
408
- filename_stub="packshot_preview",
409
  key_prefix=f"job{st.session_state['job_counter']}"
410
  )
411
 
412
- # "Padded preview" = yine preview kadrajı (packshot’ı göstermek için)
413
- pad_url, pad_bytes = show_tile_card(
414
- c_pad, "Padded preview (backend)",
415
- url=res.get("packshot_preview_url"),
416
- filename_stub="padded_preview",
 
417
  key_prefix=f"job{st.session_state['job_counter']}"
418
  )
419
 
420
- # Placed/model preview
421
  m_url, m_bytes = show_tile_card(
422
- c_model, "Model üzerinde (preview)",
423
- url=res.get("placed_preview_url"),
424
- filename_stub="model_preview",
425
  key_prefix=f"job{st.session_state['job_counter']}"
426
  )
427
 
428
- # Video: varsa göster; yoksa kullanıcı isterse üretecek
429
  vurl = res.get("video_url")
430
  c_vid.markdown('<div class="lb-card-title">Video</div>', unsafe_allow_html=True)
431
  if vurl:
@@ -447,7 +454,7 @@ if res:
447
  else:
448
  c_vid.markdown('<div class="lb-frame"></div>', unsafe_allow_html=True)
449
  if st.checkbox("Bu sonuçtan video üret", value=False, key="video_toggle"):
450
- # Öncelik: placed_native → değilse packshot_native
451
  src_for_video = res.get("placed_native_url") or res.get("packshot_native_url")
452
  if not src_for_video:
453
  st.warning("Video için kullanılacak native görsel bulunamadı.")
@@ -466,23 +473,15 @@ if res:
466
  st.session_state["results"]["video_url"] = vurl2
467
  if isinstance(ui_hints, dict):
468
  st.session_state["video_ui_max_px"] = int(ui_hints.get("suggested_video_max_px", VIDEO_MAX_PX_DEFAULT))
469
- st.success("Video hazır. Sayfayı aşağıdaki Video kartında görebilirsin.")
470
  else:
471
  st.info("Video URL gelmedi.")
472
 
473
- # Native ZIP (packshot_native + placed_native + video)
474
  named = []
475
- # native packshot (arka planı temiz PNG)
476
- if res.get("packshot_native_url"):
477
- b = fetch_bytes(res["packshot_native_url"])
478
- if b: named.append((f"packshot_native{infer_ext(b)}", b))
479
- # native placed
480
- if res.get("placed_native_url"):
481
- b = fetch_bytes(res["placed_native_url"])
482
- if b: named.append((f"model_native{infer_ext(b)}", b))
483
- # video
484
  if vbytes: named.append(("video.mp4", vbytes))
485
-
486
  if named:
487
  zip_bytes = make_zip(named)
488
  st.download_button("Hepsini ZIP indir", data=zip_bytes,
 
1
+ # streamlit_app.py — AI LightBox · Jewelry
2
+ # Pre-run sabit 4 kart (Input/Packshot/Padded/Video loop) + Run sonrası native odaklı kartlar
3
  import os, io, base64, zipfile, requests, streamlit as st
4
  from PIL import Image
5
 
 
65
 
66
  @st.cache_data(ttl=300, show_spinner=False)
67
  def fetch_bytes(url_or_path: str):
68
+ if not url_or_path: return None
 
69
  try:
70
  url = _abs_backend_url(url_or_path)
71
  headers = HEADERS if _needs_auth(url) else None
 
130
  }}
131
  .lb-video {{ display:flex; justify-content:center; }}
132
  .lb-video video {{ border-radius:8px; max-width: 540px; width:100%; height:auto; }}
133
+ /* kartlar sabit kalsın */
134
  </style>
135
  """, unsafe_allow_html=True)
136
 
 
201
  # ================= Sayfa =================
202
  st.set_page_config(page_title="AI LightBox · Jewelry", layout="wide", page_icon="💎")
203
  st.title("💎 AI LightBox · Jewelry")
204
+ st.caption("Pre-run: sabit preview kartları. Run sonrası: Packshot/Model çıktıları native indirilir. Padded sadece ara adımdır.")
205
 
206
  ok = backend_ok()
207
+
208
  # -------- Üst Kontrol Şeridi (Üretim Parametreleri) --------
209
  col1, col2, col3, col4, col5, col6, col7 = st.columns([1.6, 1.6, 1.0, 1.2, 0.9, 1.1, 0.9])
210
  with col1:
 
214
  with col3:
215
  jewel_type = st.selectbox("Tür", ["auto","earring","ring","necklace","bracelet","pendant","brooch","set","watch"], index=0, key="jewel_type")
216
  with col4:
217
+ preview_ratio = st.number_input("Preview ölçeği (0.30–0.99)", min_value=0.30, max_value=0.99, value=0.90, step=0.01,
218
  help="Sadece önizleme kadrajında büyüklüğü etkiler.", key="preview_ratio")
219
  with col5:
220
+ upscale = st.checkbox("Super Resolution", True, key="upscale")
221
  with col6:
222
  upscale_stage = st.selectbox("SR aşaması", ["both","final","packshot"], index=0, help="Varsayılan: both", key="upscale_stage")
223
  with col7:
 
249
  # Inject CSS (uniform kartlar)
250
  inject_preview_css(preview_aspect, fit_mode="contain" if fit_mode=="fit" else "cover")
251
 
252
+ # ================= PRE-RUN PREVIEWS (her zaman sabit) =================
253
  st.subheader("Önizlemeler (Çalıştırmadan önce)")
254
  col_in, col_pack, col_pad, col_vid = st.columns(4)
255
 
256
+ # Input bytes (upload/URL öncelikli; yoksa demo)
257
  input_bytes = None
258
  if file_list:
259
  try: input_bytes = file_list[0].getvalue()
 
267
  bytes_data=input_bytes if input_bytes else demo_files.get("input.jpg"),
268
  filename_stub="input", key_prefix="pre")
269
 
270
+ # Packshot preview (demo dosyası varsa; yoksa input görüntü)
271
+ pack_bytes_demo = demo_files.get("packshot.jpg") or input_bytes
272
  show_tile_card(col_pack, "Packshot preview",
273
+ bytes_data=pack_bytes_demo, filename_stub="packshot", key_prefix="pre")
274
 
275
+ # Padded preview (packshot.jpg veya input.jpg → seçilen kadraja, UI-only)
276
  padded_bytes = None
277
+ if pack_bytes_demo:
278
  try:
279
+ padded_bytes = make_preview_frame(pack_bytes_demo, aspect=preview_aspect,
280
  long_edge=int(preview_long_edge),
281
  ratio=float(preview_ratio))
282
  except Exception:
283
  padded_bytes = None
284
+ show_tile_card(col_pad, "Padded preview (UI only)",
285
+ bytes_data=padded_bytes or pack_bytes_demo, filename_stub="padded", key_prefix="pre")
286
 
287
+ # Video preview (loop; yerel tryon.mp4 varsa)
288
  vid_demo = demo_files.get("tryon.mp4")
289
  col_vid.markdown('<div class="lb-card-title">Video preview (loop)</div>', unsafe_allow_html=True)
290
  if vid_demo:
 
311
  st.error("En az bir görsel girişi yapın (dosya yükle veya URL).")
312
  else:
313
  try:
 
314
  img_urls = image_url or ""
315
  files_payload = []
316
  if file_list:
 
324
  "image_urls": img_urls,
325
  "mannequin_image_url": "", # opsiyonel
326
  "identity_lock": "true" if identity_lock else "false",
327
+ # Preview controls (backend v0.4.0 — sadece UI çerçevesi için)
328
  "preview_aspect": preview_aspect,
329
  "preview_long_edge": str(int(preview_long_edge)),
330
  "preview_ratio": str(float(preview_ratio)),
 
346
 
347
  st.session_state["job_counter"] += 1
348
 
349
+ # ---- ÇIKTILAR (NATIVE ODAKLI) ----
350
+ # Packshot NATIVE (arka planı temiz PNG)
351
+ pack_native = None
 
 
 
352
  try:
353
+ pack_native = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
354
  except Exception: pass
355
 
356
+ # Padded BACKEND (ara adım olarak packshot native'den UI'da üretiyoruz; download mümkün)
357
+ # 0.5 oranlı 9:16 görünüm — native dosyayı etkilemez
358
+ pad_backend_bytes = None
359
+ if pack_native:
360
+ nb = fetch_bytes(pack_native)
361
+ if nb:
362
+ try:
363
+ pad_backend_bytes = make_preview_frame(nb, aspect=preview_aspect,
364
+ long_edge=int(preview_long_edge),
365
+ ratio=0.50)
366
+ except Exception:
367
+ pad_backend_bytes = None
368
+
369
+ # Model NATIVE
370
+ placed_native = None
371
  try:
372
+ placed_native = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
373
  except Exception: pass
374
 
375
  # Video (zincirde üretildiyse)
 
387
  st.session_state["results"] = {
388
  "job_id": out.get("job_id"),
389
  "schema_version": out.get("schema_version"),
390
+ "packshot_native_url": pack_native,
391
+ "padded_backend_bytes": pad_backend_bytes, # ara adım UI-only
392
+ "placed_native_url": placed_native,
 
393
  "video_url": vurl,
394
  "api_base": API_BASE,
395
  "has_auth_header": bool(HEADERS),
 
407
  st.subheader("Üretilen Sonuçlar")
408
  c_pack, c_pad, c_model, c_vid = st.columns(4)
409
 
410
+ # Packshot (backend) — NATIVE
411
  p_url, p_bytes = show_tile_card(
412
+ c_pack, "Packshot (backend)",
413
+ url=res.get("packshot_native_url"),
414
+ filename_stub="packshot_native",
415
  key_prefix=f"job{st.session_state['job_counter']}"
416
  )
417
 
418
+ # Padded (backend) — ara adım (UI’da packshot native’den 0.5 oranlı kadraj)
419
+ pad_bytes = res.get("padded_backend_bytes")
420
+ _, _ = show_tile_card(
421
+ c_pad, "Padded (backend) • ara adım",
422
+ bytes_data=pad_bytes if pad_bytes else (p_bytes or b""),
423
+ filename_stub="padded_backend",
424
  key_prefix=f"job{st.session_state['job_counter']}"
425
  )
426
 
427
+ # Model üzerinde (backend) — NATIVE
428
  m_url, m_bytes = show_tile_card(
429
+ c_model, "Model üzerinde (backend)",
430
+ url=res.get("placed_native_url"),
431
+ filename_stub="model_native",
432
  key_prefix=f"job{st.session_state['job_counter']}"
433
  )
434
 
435
+ # Video: varsa göster; yoksa kullanıcı isterse üretecek (loop)
436
  vurl = res.get("video_url")
437
  c_vid.markdown('<div class="lb-card-title">Video</div>', unsafe_allow_html=True)
438
  if vurl:
 
454
  else:
455
  c_vid.markdown('<div class="lb-frame"></div>', unsafe_allow_html=True)
456
  if st.checkbox("Bu sonuçtan video üret", value=False, key="video_toggle"):
457
+ # Öncelik: model native → değilse packshot native
458
  src_for_video = res.get("placed_native_url") or res.get("packshot_native_url")
459
  if not src_for_video:
460
  st.warning("Video için kullanılacak native görsel bulunamadı.")
 
473
  st.session_state["results"]["video_url"] = vurl2
474
  if isinstance(ui_hints, dict):
475
  st.session_state["video_ui_max_px"] = int(ui_hints.get("suggested_video_max_px", VIDEO_MAX_PX_DEFAULT))
476
+ st.success("Video hazır. Yukarıdaki Video kartında döngü halinde oynatılıyor.")
477
  else:
478
  st.info("Video URL gelmedi.")
479
 
480
+ # Native ZIP (packshot_native + model_native + video)
481
  named = []
482
+ if p_bytes: named.append((f"packshot_native{infer_ext(p_bytes)}", p_bytes))
483
+ if m_bytes: named.append((f"model_native{infer_ext(m_bytes)}", m_bytes))
 
 
 
 
 
 
 
484
  if vbytes: named.append(("video.mp4", vbytes))
 
485
  if named:
486
  zip_bytes = make_zip(named)
487
  st.download_button("Hepsini ZIP indir", data=zip_bytes,