ptom
Browse filespromot suport
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 8 |
# ======================================================
|
| 9 |
# CONFIG
|
| 10 |
# ======================================================
|
| 11 |
-
DEFAULT_REPO = "rahul7star/WAN22-
|
| 12 |
FREE_REPO = "rahul7star/ohamlab"
|
| 13 |
FREE_IMAGE_DIR = "showcase/image"
|
| 14 |
|
|
@@ -24,7 +24,42 @@ api = HfApi()
|
|
| 24 |
PAGE_SIZE = 6
|
| 25 |
|
| 26 |
tmp_dir = tempfile.mkdtemp()
|
| 27 |
-
images_cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# ======================================================
|
| 30 |
# FREE PREVIEW
|
|
@@ -46,8 +81,8 @@ def load_free_images():
|
|
| 46 |
for f in imgs:
|
| 47 |
try:
|
| 48 |
out.append(hf_hub_download(FREE_REPO, f, token=HF_TOKEN, local_dir=tmp))
|
| 49 |
-
except Exception
|
| 50 |
-
|
| 51 |
|
| 52 |
return out
|
| 53 |
|
|
@@ -59,7 +94,8 @@ FREE_IMAGES = load_free_images()
|
|
| 59 |
# ======================================================
|
| 60 |
def clear_cache():
|
| 61 |
images_cache.clear()
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def list_date_folders(repo, last_n=20):
|
|
@@ -77,7 +113,7 @@ def download_images(repo, date):
|
|
| 77 |
return images_cache[key]
|
| 78 |
|
| 79 |
files = api.list_repo_files(repo, token=HF_TOKEN)
|
| 80 |
-
|
| 81 |
|
| 82 |
for f in files:
|
| 83 |
if not f.startswith(date + "/"):
|
|
@@ -85,39 +121,41 @@ def download_images(repo, date):
|
|
| 85 |
if not f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
|
| 86 |
continue
|
| 87 |
|
|
|
|
| 88 |
dst = os.path.join(tmp_dir, repo.replace("/", "_"), f)
|
| 89 |
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
| 90 |
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
repo,
|
| 94 |
f,
|
| 95 |
token=HF_TOKEN,
|
| 96 |
local_dir=os.path.dirname(dst),
|
| 97 |
)
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
print("[DOWNLOAD ERROR]", f, e)
|
| 102 |
|
| 103 |
-
images_cache[key] =
|
| 104 |
-
print(f"[INFO] {repo} {date}: {len(
|
| 105 |
-
return
|
| 106 |
|
| 107 |
|
| 108 |
# ======================================================
|
| 109 |
# PAGINATION
|
| 110 |
# ======================================================
|
| 111 |
def render_page(repo, date, page):
|
| 112 |
-
|
| 113 |
-
total_pages = max(1, math.ceil(len(
|
| 114 |
|
| 115 |
page = max(0, min(int(page), total_pages - 1))
|
| 116 |
start = page * PAGE_SIZE
|
| 117 |
end = start + PAGE_SIZE
|
| 118 |
|
| 119 |
return (
|
| 120 |
-
|
| 121 |
page,
|
| 122 |
f"Page {page+1} / {total_pages}",
|
| 123 |
)
|
|
@@ -170,7 +208,7 @@ def check_password(pwd):
|
|
| 170 |
|
| 171 |
|
| 172 |
# ======================================================
|
| 173 |
-
# CSS
|
| 174 |
# ======================================================
|
| 175 |
css = """
|
| 176 |
footer, .gradio-footer { display:none!important; }
|
|
@@ -192,7 +230,6 @@ footer, .gradio-footer { display:none!important; }
|
|
| 192 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 193 |
gr.Markdown("# 🔐 OhamLab Image Showcase (PRO)")
|
| 194 |
|
| 195 |
-
# ---------- LOGIN ----------
|
| 196 |
with gr.Column(visible=True) as login_box:
|
| 197 |
pwd = gr.Textbox(type="password", label="Password")
|
| 198 |
login_btn = gr.Button("Unlock")
|
|
@@ -202,79 +239,45 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 202 |
for img in FREE_IMAGES:
|
| 203 |
gr.Image(img, show_label=False)
|
| 204 |
|
| 205 |
-
# ---------- APP ----------
|
| 206 |
with gr.Column(visible=False) as app:
|
| 207 |
repo_state = gr.State(DEFAULT_REPO)
|
| 208 |
date_state = gr.State("")
|
| 209 |
page_state = gr.State(0)
|
| 210 |
|
| 211 |
-
repo_input = gr.Textbox(
|
| 212 |
-
value=DEFAULT_REPO,
|
| 213 |
-
label="Base HuggingFace Repo",
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
date_dropdown = gr.Dropdown(label="Select Date")
|
|
|
|
| 217 |
|
| 218 |
-
|
| 219 |
-
label="Or enter date manually (YYYY-MM-DD)"
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
gallery = gr.Gallery(columns=3, height="auto")
|
| 223 |
page_info = gr.Markdown()
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
prev_btn = gr.Button("⬅ Previous")
|
| 227 |
next_btn = gr.Button("Next ➡")
|
| 228 |
|
| 229 |
-
# ---------- EVENTS ----------
|
| 230 |
login_btn.click(
|
| 231 |
check_password,
|
| 232 |
inputs=pwd,
|
| 233 |
-
outputs=[
|
| 234 |
-
login_box,
|
| 235 |
-
app,
|
| 236 |
-
gallery,
|
| 237 |
-
page_state,
|
| 238 |
-
date_state,
|
| 239 |
-
page_info,
|
| 240 |
-
],
|
| 241 |
)
|
| 242 |
|
| 243 |
repo_input.change(
|
| 244 |
change_repo,
|
| 245 |
inputs=repo_input,
|
| 246 |
-
outputs=[
|
| 247 |
-
|
| 248 |
-
gallery,
|
| 249 |
-
page_state,
|
| 250 |
-
date_state,
|
| 251 |
-
page_info,
|
| 252 |
-
],
|
| 253 |
-
).then(
|
| 254 |
-
lambda r: r,
|
| 255 |
-
inputs=repo_input,
|
| 256 |
-
outputs=repo_state,
|
| 257 |
-
)
|
| 258 |
|
| 259 |
date_dropdown.change(
|
| 260 |
lambda r, d: change_date(r, d),
|
| 261 |
inputs=[repo_state, date_dropdown],
|
| 262 |
outputs=[gallery, page_state, page_info],
|
| 263 |
-
).then(
|
| 264 |
-
lambda d: d,
|
| 265 |
-
inputs=date_dropdown,
|
| 266 |
-
outputs=date_state,
|
| 267 |
-
)
|
| 268 |
|
| 269 |
manual_date.submit(
|
| 270 |
lambda r, d: change_date(r, d),
|
| 271 |
inputs=[repo_state, manual_date],
|
| 272 |
outputs=[gallery, page_state, page_info],
|
| 273 |
-
).then(
|
| 274 |
-
lambda d: d,
|
| 275 |
-
inputs=manual_date,
|
| 276 |
-
outputs=date_state,
|
| 277 |
-
)
|
| 278 |
|
| 279 |
prev_btn.click(
|
| 280 |
lambda r, d, p: nav(-1, r, d, p),
|
|
|
|
| 8 |
# ======================================================
|
| 9 |
# CONFIG
|
| 10 |
# ======================================================
|
| 11 |
+
DEFAULT_REPO = "rahul7star/WAN22-Feb-2026"
|
| 12 |
FREE_REPO = "rahul7star/ohamlab"
|
| 13 |
FREE_IMAGE_DIR = "showcase/image"
|
| 14 |
|
|
|
|
| 24 |
PAGE_SIZE = 6
|
| 25 |
|
| 26 |
tmp_dir = tempfile.mkdtemp()
|
| 27 |
+
images_cache = {} # (repo, date) -> list[(img, prompt)]
|
| 28 |
+
prompt_cache = {} # folder_path -> prompt text
|
| 29 |
+
|
| 30 |
+
# ======================================================
|
| 31 |
+
# PROMPT LOADER (NEW)
|
| 32 |
+
# ======================================================
|
| 33 |
+
def load_prompt_from_folder(repo, folder):
|
| 34 |
+
"""
|
| 35 |
+
Tries to find prompt text inside the same upload folder.
|
| 36 |
+
Cached for performance.
|
| 37 |
+
"""
|
| 38 |
+
key = (repo, folder)
|
| 39 |
+
if key in prompt_cache:
|
| 40 |
+
return prompt_cache[key]
|
| 41 |
+
|
| 42 |
+
files = api.list_repo_files(repo, token=HF_TOKEN)
|
| 43 |
+
prompt = ""
|
| 44 |
+
|
| 45 |
+
for f in files:
|
| 46 |
+
if not f.startswith(folder + "/"):
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
name = os.path.basename(f).lower()
|
| 50 |
+
|
| 51 |
+
if name in ("prompt.txt", "caption.txt"):
|
| 52 |
+
try:
|
| 53 |
+
path = hf_hub_download(repo, f, token=HF_TOKEN)
|
| 54 |
+
with open(path, "r", encoding="utf-8", errors="ignore") as fp:
|
| 55 |
+
prompt = fp.read().strip()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print("[PROMPT ERROR]", e)
|
| 58 |
+
break
|
| 59 |
+
|
| 60 |
+
prompt_cache[key] = prompt
|
| 61 |
+
return prompt
|
| 62 |
+
|
| 63 |
|
| 64 |
# ======================================================
|
| 65 |
# FREE PREVIEW
|
|
|
|
| 81 |
for f in imgs:
|
| 82 |
try:
|
| 83 |
out.append(hf_hub_download(FREE_REPO, f, token=HF_TOKEN, local_dir=tmp))
|
| 84 |
+
except Exception:
|
| 85 |
+
pass
|
| 86 |
|
| 87 |
return out
|
| 88 |
|
|
|
|
| 94 |
# ======================================================
|
| 95 |
def clear_cache():
|
| 96 |
images_cache.clear()
|
| 97 |
+
prompt_cache.clear()
|
| 98 |
+
print("[CACHE] cleared")
|
| 99 |
|
| 100 |
|
| 101 |
def list_date_folders(repo, last_n=20):
|
|
|
|
| 113 |
return images_cache[key]
|
| 114 |
|
| 115 |
files = api.list_repo_files(repo, token=HF_TOKEN)
|
| 116 |
+
results = []
|
| 117 |
|
| 118 |
for f in files:
|
| 119 |
if not f.startswith(date + "/"):
|
|
|
|
| 121 |
if not f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
|
| 122 |
continue
|
| 123 |
|
| 124 |
+
folder = os.path.dirname(f)
|
| 125 |
dst = os.path.join(tmp_dir, repo.replace("/", "_"), f)
|
| 126 |
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
| 127 |
|
| 128 |
try:
|
| 129 |
+
img_path = hf_hub_download(
|
| 130 |
repo,
|
| 131 |
f,
|
| 132 |
token=HF_TOKEN,
|
| 133 |
local_dir=os.path.dirname(dst),
|
| 134 |
)
|
| 135 |
+
prompt = load_prompt_from_folder(repo, folder)
|
| 136 |
+
results.append((img_path, prompt))
|
| 137 |
+
print("[DOWNLOAD]", f)
|
| 138 |
except Exception as e:
|
| 139 |
print("[DOWNLOAD ERROR]", f, e)
|
| 140 |
|
| 141 |
+
images_cache[key] = results
|
| 142 |
+
print(f"[INFO] {repo} {date}: {len(results)} images")
|
| 143 |
+
return results
|
| 144 |
|
| 145 |
|
| 146 |
# ======================================================
|
| 147 |
# PAGINATION
|
| 148 |
# ======================================================
|
| 149 |
def render_page(repo, date, page):
|
| 150 |
+
items = download_images(repo, date)
|
| 151 |
+
total_pages = max(1, math.ceil(len(items) / PAGE_SIZE))
|
| 152 |
|
| 153 |
page = max(0, min(int(page), total_pages - 1))
|
| 154 |
start = page * PAGE_SIZE
|
| 155 |
end = start + PAGE_SIZE
|
| 156 |
|
| 157 |
return (
|
| 158 |
+
items[start:end],
|
| 159 |
page,
|
| 160 |
f"Page {page+1} / {total_pages}",
|
| 161 |
)
|
|
|
|
| 208 |
|
| 209 |
|
| 210 |
# ======================================================
|
| 211 |
+
# CSS (UNCHANGED)
|
| 212 |
# ======================================================
|
| 213 |
css = """
|
| 214 |
footer, .gradio-footer { display:none!important; }
|
|
|
|
| 230 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 231 |
gr.Markdown("# 🔐 OhamLab Image Showcase (PRO)")
|
| 232 |
|
|
|
|
| 233 |
with gr.Column(visible=True) as login_box:
|
| 234 |
pwd = gr.Textbox(type="password", label="Password")
|
| 235 |
login_btn = gr.Button("Unlock")
|
|
|
|
| 239 |
for img in FREE_IMAGES:
|
| 240 |
gr.Image(img, show_label=False)
|
| 241 |
|
|
|
|
| 242 |
with gr.Column(visible=False) as app:
|
| 243 |
repo_state = gr.State(DEFAULT_REPO)
|
| 244 |
date_state = gr.State("")
|
| 245 |
page_state = gr.State(0)
|
| 246 |
|
| 247 |
+
repo_input = gr.Textbox(value=DEFAULT_REPO, label="Base HuggingFace Repo")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
date_dropdown = gr.Dropdown(label="Select Date")
|
| 249 |
+
manual_date = gr.Textbox(label="Or enter date (YYYY-MM-DD)")
|
| 250 |
|
| 251 |
+
gallery = gr.Gallery(columns=3, height="auto", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
page_info = gr.Markdown()
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
prev_btn = gr.Button("⬅ Previous")
|
| 256 |
next_btn = gr.Button("Next ➡")
|
| 257 |
|
|
|
|
| 258 |
login_btn.click(
|
| 259 |
check_password,
|
| 260 |
inputs=pwd,
|
| 261 |
+
outputs=[login_box, app, gallery, page_state, date_state, page_info],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
)
|
| 263 |
|
| 264 |
repo_input.change(
|
| 265 |
change_repo,
|
| 266 |
inputs=repo_input,
|
| 267 |
+
outputs=[date_dropdown, gallery, page_state, date_state, page_info],
|
| 268 |
+
).then(lambda r: r, inputs=repo_input, outputs=repo_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
date_dropdown.change(
|
| 271 |
lambda r, d: change_date(r, d),
|
| 272 |
inputs=[repo_state, date_dropdown],
|
| 273 |
outputs=[gallery, page_state, page_info],
|
| 274 |
+
).then(lambda d: d, inputs=date_dropdown, outputs=date_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
manual_date.submit(
|
| 277 |
lambda r, d: change_date(r, d),
|
| 278 |
inputs=[repo_state, manual_date],
|
| 279 |
outputs=[gallery, page_state, page_info],
|
| 280 |
+
).then(lambda d: d, inputs=manual_date, outputs=date_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
prev_btn.click(
|
| 283 |
lambda r, d, p: nav(-1, r, d, p),
|