Spaces:
Sleeping
Sleeping
Upload streamlit_app.py
Browse files- streamlit_app.py +16 -41
streamlit_app.py
CHANGED
|
@@ -2,28 +2,9 @@
|
|
| 2 |
import os, io, zipfile, requests, streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
# ================= Env & API
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Desteklenen env isimleri (öncelik sırası):
|
| 10 |
-
# AI_BEAUTYBOX_API / AI_LIGHTBOX_API / BeautyBoxAI_API
|
| 11 |
-
API_BASE = (
|
| 12 |
-
_env("AI_BEAUTYBOX_API") or
|
| 13 |
-
_env("AI_LIGHTBOX_API") or
|
| 14 |
-
_env("BeautyBoxAI_API")
|
| 15 |
-
).rstrip("/")
|
| 16 |
-
|
| 17 |
-
# Fallback'ı kaldırıyoruz; yanlışlıkla 127.0.0.1'e gitmesin.
|
| 18 |
-
if not API_BASE:
|
| 19 |
-
st.error("API_BASE yok. Settings > Variables: AI_BEAUTYBOX_API veya AI_LIGHTBOX_API tanımla ve Space'i yeniden başlat.")
|
| 20 |
-
st.stop()
|
| 21 |
-
|
| 22 |
-
HF_TOKEN = (
|
| 23 |
-
_env("AI_BEAUTYBOX_TOKEN") or
|
| 24 |
-
_env("AI_LIGHTBOX_TOKEN") or
|
| 25 |
-
_env("BeautyBoxAI_TOKEN")
|
| 26 |
-
)
|
| 27 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 28 |
PREVIEW_SIZE = int(os.getenv("PREVIEW_SIZE", "512"))
|
| 29 |
|
|
@@ -35,14 +16,13 @@ if "job_counter" not in st.session_state:
|
|
| 35 |
if "duration" not in st.session_state:
|
| 36 |
st.session_state["duration"] = "6" # widget oluşturulmadan önce default
|
| 37 |
|
| 38 |
-
# ================= Yardımcılar =================
|
| 39 |
def _needs_auth(url: str) -> bool:
|
| 40 |
return url.startswith(API_BASE) or url.startswith("outputs/")
|
| 41 |
|
| 42 |
def backend_ok() -> bool:
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
r = requests.get(f"{API_BASE}/health", timeout=10)
|
| 46 |
r.raise_for_status()
|
| 47 |
return True
|
| 48 |
except Exception:
|
|
@@ -51,7 +31,7 @@ def backend_ok() -> bool:
|
|
| 51 |
def post_chain(data: dict, files_payload):
|
| 52 |
data = {**data, "to_video": "false"} # videoyu ayrı tetikleriz
|
| 53 |
r = requests.post(f"{API_BASE}/v1/tryon/chain", data=data, files=files_payload or None,
|
| 54 |
-
headers=HEADERS
|
| 55 |
r.raise_for_status()
|
| 56 |
return r.json()
|
| 57 |
|
|
@@ -62,8 +42,7 @@ def post_video(image_url: str, duration="6", resolution="768P", prompt_optimizer
|
|
| 62 |
"resolution": resolution,
|
| 63 |
"prompt_optimizer": "true" if prompt_optimizer else "false",
|
| 64 |
}
|
| 65 |
-
r = requests.post(f"{API_BASE}/v1/video/from-image", data=payload,
|
| 66 |
-
headers=HEADERS if HF_TOKEN else None, timeout=600)
|
| 67 |
r.raise_for_status()
|
| 68 |
return r.json()
|
| 69 |
|
|
@@ -73,7 +52,7 @@ def fetch_bytes(url_or_path: str):
|
|
| 73 |
return None
|
| 74 |
try:
|
| 75 |
url = f"{API_BASE}/{url_or_path}" if url_or_path.startswith("outputs/") else url_or_path
|
| 76 |
-
headers = HEADERS if
|
| 77 |
r = requests.get(url, headers=headers, timeout=180)
|
| 78 |
r.raise_for_status()
|
| 79 |
return r.content
|
|
@@ -429,18 +408,14 @@ if res:
|
|
| 429 |
"video_url": res.get("video_url"),
|
| 430 |
})
|
| 431 |
|
| 432 |
-
# ================= Sidebar
|
| 433 |
with st.sidebar:
|
| 434 |
-
st.caption(f"Resolved API_BASE: {API_BASE}")
|
| 435 |
-
st.caption("Auth header: " + ("ON" if HF_TOKEN else "OFF"))
|
| 436 |
-
if st.button("Ping /health"):
|
| 437 |
-
try:
|
| 438 |
-
r = requests.get(f"{API_BASE}/health", timeout=10) # headers yok
|
| 439 |
-
st.write(r.status_code)
|
| 440 |
-
st.json(r.json())
|
| 441 |
-
except Exception as e:
|
| 442 |
-
st.error(f"Health error: {e}")
|
| 443 |
if st.button("Clear cache"):
|
| 444 |
-
st.cache_data.clear()
|
|
|
|
| 445 |
if st.button("Clear results"):
|
| 446 |
-
st.session_state["results"] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os, io, zipfile, requests, streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# ================= Env & API =================
|
| 6 |
+
API_BASE = (os.getenv("AI_LIGHTBOX_API", "http://127.0.0.1:8000") or "http://127.0.0.1:8000").strip().strip("'\"").rstrip("/")
|
| 7 |
+
HF_TOKEN = (os.getenv("AI_LIGHTBOX_TOKEN", "") or "").strip().strip("'\"")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 9 |
PREVIEW_SIZE = int(os.getenv("PREVIEW_SIZE", "512"))
|
| 10 |
|
|
|
|
| 16 |
if "duration" not in st.session_state:
|
| 17 |
st.session_state["duration"] = "6" # widget oluşturulmadan önce default
|
| 18 |
|
| 19 |
+
# ================= Yardımcılar =================
|
| 20 |
def _needs_auth(url: str) -> bool:
|
| 21 |
return url.startswith(API_BASE) or url.startswith("outputs/")
|
| 22 |
|
| 23 |
def backend_ok() -> bool:
|
| 24 |
try:
|
| 25 |
+
r = requests.get(f"{API_BASE}/health", headers=HEADERS, timeout=10)
|
|
|
|
| 26 |
r.raise_for_status()
|
| 27 |
return True
|
| 28 |
except Exception:
|
|
|
|
| 31 |
def post_chain(data: dict, files_payload):
|
| 32 |
data = {**data, "to_video": "false"} # videoyu ayrı tetikleriz
|
| 33 |
r = requests.post(f"{API_BASE}/v1/tryon/chain", data=data, files=files_payload or None,
|
| 34 |
+
headers=HEADERS, timeout=600)
|
| 35 |
r.raise_for_status()
|
| 36 |
return r.json()
|
| 37 |
|
|
|
|
| 42 |
"resolution": resolution,
|
| 43 |
"prompt_optimizer": "true" if prompt_optimizer else "false",
|
| 44 |
}
|
| 45 |
+
r = requests.post(f"{API_BASE}/v1/video/from-image", data=payload, headers=HEADERS, timeout=600)
|
|
|
|
| 46 |
r.raise_for_status()
|
| 47 |
return r.json()
|
| 48 |
|
|
|
|
| 52 |
return None
|
| 53 |
try:
|
| 54 |
url = f"{API_BASE}/{url_or_path}" if url_or_path.startswith("outputs/") else url_or_path
|
| 55 |
+
headers = HEADERS if _needs_auth(url) else None
|
| 56 |
r = requests.get(url, headers=headers, timeout=180)
|
| 57 |
r.raise_for_status()
|
| 58 |
return r.content
|
|
|
|
| 408 |
"video_url": res.get("video_url"),
|
| 409 |
})
|
| 410 |
|
| 411 |
+
# ================= Sidebar =================
|
| 412 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
if st.button("Clear cache"):
|
| 414 |
+
st.cache_data.clear()
|
| 415 |
+
st.success("Cache cleared.")
|
| 416 |
if st.button("Clear results"):
|
| 417 |
+
st.session_state["results"] = None
|
| 418 |
+
st.success("Results cleared.")
|
| 419 |
+
st.caption(f"API_BASE: {API_BASE}")
|
| 420 |
+
if HF_TOKEN:
|
| 421 |
+
st.caption("Auth: Bearer (active)")
|