Spaces:
Sleeping
Sleeping
Upload streamlit_app.py
Browse files- streamlit_app.py +49 -60
streamlit_app.py
CHANGED
|
@@ -75,7 +75,7 @@ def fetch_bytes(url_or_path: str):
|
|
| 75 |
except Exception:
|
| 76 |
return None
|
| 77 |
|
| 78 |
-
def image_bytes_to_data_url(b: bytes, mime="image/
|
| 79 |
enc = base64.b64encode(b).decode("ascii")
|
| 80 |
return f"data:{mime};base64,{enc}"
|
| 81 |
|
|
@@ -142,7 +142,7 @@ def show_tile_card_display_download(
|
|
| 142 |
filename_stub: str = "image",
|
| 143 |
key_prefix: str = "",
|
| 144 |
):
|
| 145 |
-
"""
|
| 146 |
col.markdown(f'<div class="lb-card-title">{title}</div>', unsafe_allow_html=True)
|
| 147 |
|
| 148 |
# ---- Display ----
|
|
@@ -160,13 +160,11 @@ def show_tile_card_display_download(
|
|
| 160 |
# ---- Native (caption + download) ----
|
| 161 |
native = download_bytes or (fetch_bytes(download_url) if download_url else None)
|
| 162 |
if not native:
|
| 163 |
-
|
| 164 |
-
native = display_bytes
|
| 165 |
|
| 166 |
if native:
|
| 167 |
sz = image_size_from_bytes(native)
|
| 168 |
if sz: col.caption(f"Gerçek çözünürlük: {sz[0]}×{sz[1]} px")
|
| 169 |
-
|
| 170 |
ext = infer_ext(native)
|
| 171 |
mime = "image/jpeg" if ext.lower() in [".jpg",".jpeg"] else ("image/png" if ext.lower()==".png" else "image/webp")
|
| 172 |
col.download_button("İndir (tam kalite)", data=native,
|
|
@@ -177,28 +175,6 @@ def show_tile_card_display_download(
|
|
| 177 |
|
| 178 |
return display_url or "data", native
|
| 179 |
|
| 180 |
-
def make_preview_frame(img_bytes: bytes, aspect: str, long_edge: int, ratio: float, bg_rgb=(255,255,255)) -> bytes:
|
| 181 |
-
"""Sadece UI için: seçilen aspect’te tuval + içerik oranı (JPEG)."""
|
| 182 |
-
im = Image.open(io.BytesIO(img_bytes)).convert("RGBA")
|
| 183 |
-
aw, ah = _parse_aspect_str(aspect)
|
| 184 |
-
if aw >= ah:
|
| 185 |
-
W = max(256, min(8192, int(long_edge))); H = max(1, int(W * ah / aw))
|
| 186 |
-
else:
|
| 187 |
-
H = max(256, min(8192, int(long_edge))); W = max(1, int(H * aw / ah))
|
| 188 |
-
ratio = max(0.10, min(0.99, float(ratio)))
|
| 189 |
-
target = int(min(W, H) * ratio if aw == ah else (H * ratio if aw < ah else W * ratio))
|
| 190 |
-
w, h = im.size
|
| 191 |
-
if w >= h:
|
| 192 |
-
new_w, new_h = target, max(1, int(h * (target / max(1, w))))
|
| 193 |
-
else:
|
| 194 |
-
new_h, new_w = target, max(1, int(w * (target / max(1, h))))
|
| 195 |
-
im_resized = im.resize((new_w, new_h), Image.LANCZOS)
|
| 196 |
-
canvas = Image.new("RGBA", (W, H), (*bg_rgb, 255))
|
| 197 |
-
off = ((W - new_w)//2, (H - new_h)//2)
|
| 198 |
-
canvas.paste(im_resized, off, im_resized)
|
| 199 |
-
out = io.BytesIO(); canvas.convert("RGB").save(out, format="JPEG", quality=90, subsampling=1)
|
| 200 |
-
return out.getvalue()
|
| 201 |
-
|
| 202 |
@st.cache_data(ttl=300, show_spinner=False)
|
| 203 |
def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280) -> bytes:
|
| 204 |
"""Ağır PNG native'i hafif JPEG önizlemeye çevir (içeriği değiştirmez)."""
|
|
@@ -214,6 +190,25 @@ def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280) ->
|
|
| 214 |
out = io.BytesIO(); im2.save(out, format="JPEG", quality=88, subsampling=1)
|
| 215 |
return out.getvalue()
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
# ================= Yerel DEMO dosyaları =================
|
| 218 |
def load_local_demo():
|
| 219 |
demo = {}
|
|
@@ -384,35 +379,32 @@ if run:
|
|
| 384 |
st.session_state["job_counter"] += 1
|
| 385 |
|
| 386 |
# ---- ÇIKTILAR ----
|
| 387 |
-
# Packshot NATIVE
|
| 388 |
-
pack_native_url = None
|
| 389 |
-
pack_preview_url = None
|
| 390 |
try:
|
| 391 |
pack_native_url = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
|
|
|
| 395 |
|
| 396 |
-
# Padded NATIVE
|
| 397 |
-
padded_native_url = None
|
| 398 |
-
padded_display_jpeg = None
|
| 399 |
try:
|
| 400 |
padded_native_url = (out["packshot_step"]["result"]["padded_native"] or [{}])[0].get("url")
|
| 401 |
if padded_native_url:
|
| 402 |
nb = fetch_bytes(padded_native_url)
|
| 403 |
-
if nb:
|
| 404 |
-
|
| 405 |
-
except Exception:
|
| 406 |
-
pass
|
| 407 |
|
| 408 |
-
# Model NATIVE
|
| 409 |
-
placed_native_url = None
|
| 410 |
-
placed_preview_url = None
|
| 411 |
try:
|
| 412 |
placed_native_url = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
|
|
|
| 416 |
|
| 417 |
# Video (zincirde üretildiyse)
|
| 418 |
vurl = None
|
|
@@ -430,11 +422,11 @@ if run:
|
|
| 430 |
"job_id": out.get("job_id"),
|
| 431 |
"schema_version": out.get("schema_version"),
|
| 432 |
"packshot_native_url": pack_native_url,
|
| 433 |
-
"
|
| 434 |
"padded_native_url": padded_native_url,
|
| 435 |
"padded_display_jpeg": padded_display_jpeg,
|
| 436 |
"placed_native_url": placed_native_url,
|
| 437 |
-
"
|
| 438 |
"video_url": vurl,
|
| 439 |
"api_base": API_BASE,
|
| 440 |
"has_auth_header": bool(HEADERS),
|
|
@@ -453,36 +445,34 @@ if res:
|
|
| 453 |
st.subheader("Üretilen Sonuçlar")
|
| 454 |
c_pack, c_pad, c_model, c_vid = st.columns(4)
|
| 455 |
|
| 456 |
-
# Packshot (backend) —
|
| 457 |
show_tile_card_display_download(
|
| 458 |
c_pack, "Packshot (backend)",
|
| 459 |
-
|
| 460 |
download_url=res.get("packshot_native_url"),
|
| 461 |
filename_stub="packshot_native",
|
| 462 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 463 |
)
|
| 464 |
|
| 465 |
-
# Padded (backend) —
|
| 466 |
-
padded_native_url = res.get("padded_native_url")
|
| 467 |
-
padded_preview_bytes = res.get("padded_display_jpeg")
|
| 468 |
show_tile_card_display_download(
|
| 469 |
c_pad, "Padded (backend) • ara adım",
|
| 470 |
-
display_bytes=
|
| 471 |
-
download_url=padded_native_url,
|
| 472 |
filename_stub="padded_backend",
|
| 473 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 474 |
)
|
| 475 |
|
| 476 |
-
# Model üzerinde (backend) —
|
| 477 |
show_tile_card_display_download(
|
| 478 |
c_model, "Model üzerinde (backend)",
|
| 479 |
-
|
| 480 |
download_url=res.get("placed_native_url"),
|
| 481 |
filename_stub="model_native",
|
| 482 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 483 |
)
|
| 484 |
|
| 485 |
-
# Video
|
| 486 |
vurl = res.get("video_url")
|
| 487 |
c_vid.markdown('<div class="lb-card-title">Video</div>', unsafe_allow_html=True)
|
| 488 |
if vurl:
|
|
@@ -526,9 +516,8 @@ if res:
|
|
| 526 |
else:
|
| 527 |
st.info("Video URL gelmedi.")
|
| 528 |
|
| 529 |
-
# Native ZIP
|
| 530 |
named = []
|
| 531 |
-
# Not: indirmede native'leri tekrar fetch edeceğiz ki ZIP full native olsun
|
| 532 |
pn = fetch_bytes(res.get("packshot_native_url")) if res.get("packshot_native_url") else None
|
| 533 |
mn = fetch_bytes(res.get("placed_native_url")) if res.get("placed_native_url") else None
|
| 534 |
if pn: named.append((f"packshot_native{infer_ext(pn)}", pn))
|
|
|
|
| 75 |
except Exception:
|
| 76 |
return None
|
| 77 |
|
| 78 |
+
def image_bytes_to_data_url(b: bytes, mime="image/jpeg") -> str:
|
| 79 |
enc = base64.b64encode(b).decode("ascii")
|
| 80 |
return f"data:{mime};base64,{enc}"
|
| 81 |
|
|
|
|
| 142 |
filename_stub: str = "image",
|
| 143 |
key_prefix: str = "",
|
| 144 |
):
|
| 145 |
+
"""Gösterimde HAFİF JPEG (display_bytes) tercih edilir; indirmede NATIVE."""
|
| 146 |
col.markdown(f'<div class="lb-card-title">{title}</div>', unsafe_allow_html=True)
|
| 147 |
|
| 148 |
# ---- Display ----
|
|
|
|
| 160 |
# ---- Native (caption + download) ----
|
| 161 |
native = download_bytes or (fetch_bytes(download_url) if download_url else None)
|
| 162 |
if not native:
|
| 163 |
+
native = display_bytes # en azından çözünürlük gösterebilmek için
|
|
|
|
| 164 |
|
| 165 |
if native:
|
| 166 |
sz = image_size_from_bytes(native)
|
| 167 |
if sz: col.caption(f"Gerçek çözünürlük: {sz[0]}×{sz[1]} px")
|
|
|
|
| 168 |
ext = infer_ext(native)
|
| 169 |
mime = "image/jpeg" if ext.lower() in [".jpg",".jpeg"] else ("image/png" if ext.lower()==".png" else "image/webp")
|
| 170 |
col.download_button("İndir (tam kalite)", data=native,
|
|
|
|
| 175 |
|
| 176 |
return display_url or "data", native
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
@st.cache_data(ttl=300, show_spinner=False)
|
| 179 |
def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280) -> bytes:
|
| 180 |
"""Ağır PNG native'i hafif JPEG önizlemeye çevir (içeriği değiştirmez)."""
|
|
|
|
| 190 |
out = io.BytesIO(); im2.save(out, format="JPEG", quality=88, subsampling=1)
|
| 191 |
return out.getvalue()
|
| 192 |
|
| 193 |
+
def make_preview_frame(img_bytes: bytes, aspect: str, long_edge: int, ratio: float, bg_rgb=(255,255,255)) -> bytes:
|
| 194 |
+
"""UI-only: seçilen aspect’te tuval + içerik oranı (JPEG)."""
|
| 195 |
+
im = Image.open(io.BytesIO(img_bytes)).convert("RGBA")
|
| 196 |
+
aw, ah = _parse_aspect_str(aspect)
|
| 197 |
+
if aw >= ah:
|
| 198 |
+
W = max(256, min(8192, int(long_edge))); H = max(1, int(W * ah / aw))
|
| 199 |
+
else:
|
| 200 |
+
H = max(256, min(8192, int(long_edge))); W = max(1, int(H * aw / ah))
|
| 201 |
+
ratio = max(0.10, min(0.99, float(ratio)))
|
| 202 |
+
target = int(min(W, H) * ratio if aw == ah else (H * ratio if aw < ah else W * ratio))
|
| 203 |
+
w, h = im.size
|
| 204 |
+
if w >= h: new_w, new_h = target, max(1, int(h * (target / max(1, w))))
|
| 205 |
+
else: new_h, new_w = target, max(1, int(w * (target / max(1, h))))
|
| 206 |
+
im_resized = im.resize((new_w, new_h), Image.LANCZOS)
|
| 207 |
+
canvas = Image.new("RGBA", (W, H), (*bg_rgb, 255))
|
| 208 |
+
off = ((W - new_w)//2, (H - new_h)//2); canvas.paste(im_resized, off, im_resized)
|
| 209 |
+
out = io.BytesIO(); canvas.convert("RGB").save(out, format="JPEG", quality=90, subsampling=1)
|
| 210 |
+
return out.getvalue()
|
| 211 |
+
|
| 212 |
# ================= Yerel DEMO dosyaları =================
|
| 213 |
def load_local_demo():
|
| 214 |
demo = {}
|
|
|
|
| 379 |
st.session_state["job_counter"] += 1
|
| 380 |
|
| 381 |
# ---- ÇIKTILAR ----
|
| 382 |
+
# Packshot NATIVE → DISPLAY küçük JPEG
|
| 383 |
+
pack_native_url = None; pack_display_jpeg = None
|
|
|
|
| 384 |
try:
|
| 385 |
pack_native_url = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
|
| 386 |
+
if pack_native_url:
|
| 387 |
+
pn = fetch_bytes(pack_native_url)
|
| 388 |
+
if pn: pack_display_jpeg = make_small_jpeg_from_native(pn, 1280)
|
| 389 |
+
except Exception: pass
|
| 390 |
|
| 391 |
+
# Padded NATIVE → DISPLAY küçük JPEG (pad etkisi görünür)
|
| 392 |
+
padded_native_url = None; padded_display_jpeg = None
|
|
|
|
| 393 |
try:
|
| 394 |
padded_native_url = (out["packshot_step"]["result"]["padded_native"] or [{}])[0].get("url")
|
| 395 |
if padded_native_url:
|
| 396 |
nb = fetch_bytes(padded_native_url)
|
| 397 |
+
if nb: padded_display_jpeg = make_small_jpeg_from_native(nb, 1280)
|
| 398 |
+
except Exception: pass
|
|
|
|
|
|
|
| 399 |
|
| 400 |
+
# Model NATIVE → DISPLAY küçük JPEG (preview URL KULLANMA!)
|
| 401 |
+
placed_native_url = None; placed_display_jpeg = None
|
|
|
|
| 402 |
try:
|
| 403 |
placed_native_url = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
|
| 404 |
+
if placed_native_url:
|
| 405 |
+
mn = fetch_bytes(placed_native_url)
|
| 406 |
+
if mn: placed_display_jpeg = make_small_jpeg_from_native(mn, 1280)
|
| 407 |
+
except Exception: pass
|
| 408 |
|
| 409 |
# Video (zincirde üretildiyse)
|
| 410 |
vurl = None
|
|
|
|
| 422 |
"job_id": out.get("job_id"),
|
| 423 |
"schema_version": out.get("schema_version"),
|
| 424 |
"packshot_native_url": pack_native_url,
|
| 425 |
+
"packshot_display_jpeg": pack_display_jpeg,
|
| 426 |
"padded_native_url": padded_native_url,
|
| 427 |
"padded_display_jpeg": padded_display_jpeg,
|
| 428 |
"placed_native_url": placed_native_url,
|
| 429 |
+
"placed_display_jpeg": placed_display_jpeg,
|
| 430 |
"video_url": vurl,
|
| 431 |
"api_base": API_BASE,
|
| 432 |
"has_auth_header": bool(HEADERS),
|
|
|
|
| 445 |
st.subheader("Üretilen Sonuçlar")
|
| 446 |
c_pack, c_pad, c_model, c_vid = st.columns(4)
|
| 447 |
|
| 448 |
+
# Packshot (backend) — DISPLAY = küçük JPEG, DOWNLOAD = NATIVE
|
| 449 |
show_tile_card_display_download(
|
| 450 |
c_pack, "Packshot (backend)",
|
| 451 |
+
display_bytes=res.get("packshot_display_jpeg"),
|
| 452 |
download_url=res.get("packshot_native_url"),
|
| 453 |
filename_stub="packshot_native",
|
| 454 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 455 |
)
|
| 456 |
|
| 457 |
+
# Padded (backend) — DISPLAY = padded küçük JPEG, DOWNLOAD = padded NATIVE
|
|
|
|
|
|
|
| 458 |
show_tile_card_display_download(
|
| 459 |
c_pad, "Padded (backend) • ara adım",
|
| 460 |
+
display_bytes=res.get("padded_display_jpeg"),
|
| 461 |
+
download_url=res.get("padded_native_url"),
|
| 462 |
filename_stub="padded_backend",
|
| 463 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 464 |
)
|
| 465 |
|
| 466 |
+
# Model üzerinde (backend) — DISPLAY = küçük JPEG (native’den), DOWNLOAD = NATIVE
|
| 467 |
show_tile_card_display_download(
|
| 468 |
c_model, "Model üzerinde (backend)",
|
| 469 |
+
display_bytes=res.get("placed_display_jpeg"),
|
| 470 |
download_url=res.get("placed_native_url"),
|
| 471 |
filename_stub="model_native",
|
| 472 |
key_prefix=f"job{st.session_state['job_counter']}"
|
| 473 |
)
|
| 474 |
|
| 475 |
+
# Video
|
| 476 |
vurl = res.get("video_url")
|
| 477 |
c_vid.markdown('<div class="lb-card-title">Video</div>', unsafe_allow_html=True)
|
| 478 |
if vurl:
|
|
|
|
| 516 |
else:
|
| 517 |
st.info("Video URL gelmedi.")
|
| 518 |
|
| 519 |
+
# Native ZIP
|
| 520 |
named = []
|
|
|
|
| 521 |
pn = fetch_bytes(res.get("packshot_native_url")) if res.get("packshot_native_url") else None
|
| 522 |
mn = fetch_bytes(res.get("placed_native_url")) if res.get("placed_native_url") else None
|
| 523 |
if pn: named.append((f"packshot_native{infer_ext(pn)}", pn))
|