suport for repo
Browse filesadd repo , previous veriosn works well
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import math
|
| 3 |
-
import time
|
| 4 |
import random
|
| 5 |
import tempfile
|
| 6 |
import gradio as gr
|
|
@@ -9,7 +8,7 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 9 |
# ======================================================
|
| 10 |
# CONFIG
|
| 11 |
# ======================================================
|
| 12 |
-
|
| 13 |
FREE_REPO = "rahul7star/ohamlab"
|
| 14 |
FREE_IMAGE_DIR = "showcase/image"
|
| 15 |
|
|
@@ -22,10 +21,10 @@ if not APP_PASSWORD:
|
|
| 22 |
raise RuntimeError("APP_PASSWORD not set")
|
| 23 |
|
| 24 |
api = HfApi()
|
| 25 |
-
|
| 26 |
PAGE_SIZE = 6
|
|
|
|
| 27 |
tmp_dir = tempfile.mkdtemp()
|
| 28 |
-
images_cache = {}
|
| 29 |
|
| 30 |
# ======================================================
|
| 31 |
# FREE PREVIEW
|
|
@@ -56,10 +55,15 @@ def load_free_images():
|
|
| 56 |
FREE_IMAGES = load_free_images()
|
| 57 |
|
| 58 |
# ======================================================
|
| 59 |
-
#
|
| 60 |
# ======================================================
|
| 61 |
-
def
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
dates = sorted(
|
| 64 |
{f.split("/")[0] for f in files if "/" in f},
|
| 65 |
reverse=True
|
|
@@ -67,17 +71,12 @@ def list_date_folders(last_n=20):
|
|
| 67 |
return dates[:last_n]
|
| 68 |
|
| 69 |
|
| 70 |
-
def
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def download_images_for_date(date):
|
| 77 |
-
if date in images_cache:
|
| 78 |
-
return images_cache[date]
|
| 79 |
|
| 80 |
-
files = api.list_repo_files(
|
| 81 |
local = []
|
| 82 |
|
| 83 |
for f in files:
|
|
@@ -86,51 +85,59 @@ def download_images_for_date(date):
|
|
| 86 |
if not f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
|
| 87 |
continue
|
| 88 |
|
| 89 |
-
|
| 90 |
-
os.makedirs(
|
| 91 |
|
| 92 |
try:
|
| 93 |
path = hf_hub_download(
|
| 94 |
-
|
| 95 |
f,
|
| 96 |
token=HF_TOKEN,
|
| 97 |
-
local_dir=
|
| 98 |
)
|
| 99 |
local.append(path)
|
| 100 |
-
print("[DOWNLOAD]",
|
| 101 |
except Exception as e:
|
| 102 |
print("[DOWNLOAD ERROR]", f, e)
|
| 103 |
|
| 104 |
-
images_cache[
|
| 105 |
-
print(f"[INFO] {
|
| 106 |
return local
|
| 107 |
|
| 108 |
|
| 109 |
# ======================================================
|
| 110 |
# PAGINATION
|
| 111 |
# ======================================================
|
| 112 |
-
def render_page(date, page):
|
| 113 |
-
images =
|
| 114 |
-
|
| 115 |
total_pages = max(1, math.ceil(len(images) / PAGE_SIZE))
|
| 116 |
-
page = max(0, min(int(page), total_pages - 1))
|
| 117 |
|
|
|
|
| 118 |
start = page * PAGE_SIZE
|
| 119 |
end = start + PAGE_SIZE
|
| 120 |
-
subset = images[start:end]
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
-
def
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
-
def
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
# ======================================================
|
|
@@ -141,27 +148,24 @@ def check_password(pwd):
|
|
| 141 |
return (
|
| 142 |
gr.update(visible=True),
|
| 143 |
gr.update(visible=False),
|
| 144 |
-
gr.update(visible=True),
|
| 145 |
[],
|
| 146 |
0,
|
| 147 |
"",
|
| 148 |
"❌ Wrong password",
|
| 149 |
)
|
| 150 |
|
| 151 |
-
dates = list_date_folders()
|
| 152 |
latest = dates[0]
|
| 153 |
|
| 154 |
-
imgs, page, info = render_page(latest, 0)
|
| 155 |
|
| 156 |
return (
|
| 157 |
gr.update(visible=False),
|
| 158 |
gr.update(visible=True),
|
| 159 |
-
gr.update(visible=False),
|
| 160 |
imgs,
|
| 161 |
page,
|
| 162 |
latest,
|
| 163 |
info,
|
| 164 |
-
"✅ Access granted",
|
| 165 |
)
|
| 166 |
|
| 167 |
|
|
@@ -170,7 +174,6 @@ def check_password(pwd):
|
|
| 170 |
# ======================================================
|
| 171 |
css = """
|
| 172 |
footer, .gradio-footer { display:none!important; }
|
| 173 |
-
|
| 174 |
#ohamlab-footer {
|
| 175 |
position:fixed;
|
| 176 |
bottom:0;
|
|
@@ -189,89 +192,100 @@ footer, .gradio-footer { display:none!important; }
|
|
| 189 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 190 |
gr.Markdown("# 🔐 OhamLab Image Showcase (PRO)")
|
| 191 |
|
| 192 |
-
# -------- LOGIN --------
|
| 193 |
with gr.Column(visible=True) as login_box:
|
| 194 |
pwd = gr.Textbox(type="password", label="Password")
|
| 195 |
login_btn = gr.Button("Unlock")
|
| 196 |
-
status = gr.Markdown()
|
| 197 |
|
| 198 |
gr.Markdown("### 🎁 Free Preview")
|
| 199 |
with gr.Row():
|
| 200 |
for img in FREE_IMAGES:
|
| 201 |
gr.Image(img, show_label=False)
|
| 202 |
|
| 203 |
-
# -------- APP --------
|
| 204 |
with gr.Column(visible=False) as app:
|
| 205 |
-
|
|
|
|
| 206 |
page_state = gr.State(0)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
label="
|
| 211 |
)
|
| 212 |
|
|
|
|
|
|
|
| 213 |
manual_date = gr.Textbox(
|
| 214 |
label="Or enter date manually (YYYY-MM-DD)"
|
| 215 |
)
|
| 216 |
|
| 217 |
-
gallery = gr.Gallery(
|
| 218 |
-
label="Images",
|
| 219 |
-
columns=3,
|
| 220 |
-
height="auto"
|
| 221 |
-
)
|
| 222 |
-
|
| 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 |
-
login_box,
|
| 237 |
gallery,
|
| 238 |
page_state,
|
| 239 |
-
|
| 240 |
page_info,
|
| 241 |
-
status,
|
| 242 |
],
|
| 243 |
)
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
inputs=
|
| 248 |
-
outputs=[
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
)
|
| 256 |
|
| 257 |
date_dropdown.change(
|
| 258 |
-
change_date,
|
| 259 |
-
inputs=date_dropdown,
|
| 260 |
outputs=[gallery, page_state, page_info],
|
| 261 |
).then(
|
| 262 |
lambda d: d,
|
| 263 |
inputs=date_dropdown,
|
| 264 |
-
outputs=
|
| 265 |
)
|
| 266 |
|
| 267 |
manual_date.submit(
|
| 268 |
-
change_date,
|
| 269 |
-
inputs=manual_date,
|
| 270 |
outputs=[gallery, page_state, page_info],
|
| 271 |
).then(
|
| 272 |
lambda d: d,
|
| 273 |
inputs=manual_date,
|
| 274 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
)
|
| 276 |
|
| 277 |
gr.HTML("<div id='ohamlab-footer'>© OhamLab Copyright</div>")
|
|
|
|
| 1 |
import os
|
| 2 |
import math
|
|
|
|
| 3 |
import random
|
| 4 |
import tempfile
|
| 5 |
import gradio as gr
|
|
|
|
| 8 |
# ======================================================
|
| 9 |
# CONFIG
|
| 10 |
# ======================================================
|
| 11 |
+
DEFAULT_REPO = "rahul7star/WAN22-Mar-2026"
|
| 12 |
FREE_REPO = "rahul7star/ohamlab"
|
| 13 |
FREE_IMAGE_DIR = "showcase/image"
|
| 14 |
|
|
|
|
| 21 |
raise RuntimeError("APP_PASSWORD not set")
|
| 22 |
|
| 23 |
api = HfApi()
|
|
|
|
| 24 |
PAGE_SIZE = 6
|
| 25 |
+
|
| 26 |
tmp_dir = tempfile.mkdtemp()
|
| 27 |
+
images_cache = {} # (repo, date) -> images
|
| 28 |
|
| 29 |
# ======================================================
|
| 30 |
# FREE PREVIEW
|
|
|
|
| 55 |
FREE_IMAGES = load_free_images()
|
| 56 |
|
| 57 |
# ======================================================
|
| 58 |
+
# HELPERS
|
| 59 |
# ======================================================
|
| 60 |
+
def clear_cache():
|
| 61 |
+
images_cache.clear()
|
| 62 |
+
print("[CACHE] cleared all")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def list_date_folders(repo, last_n=20):
|
| 66 |
+
files = api.list_repo_files(repo, token=HF_TOKEN)
|
| 67 |
dates = sorted(
|
| 68 |
{f.split("/")[0] for f in files if "/" in f},
|
| 69 |
reverse=True
|
|
|
|
| 71 |
return dates[:last_n]
|
| 72 |
|
| 73 |
|
| 74 |
+
def download_images(repo, date):
|
| 75 |
+
key = (repo, date)
|
| 76 |
+
if key in images_cache:
|
| 77 |
+
return images_cache[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
files = api.list_repo_files(repo, token=HF_TOKEN)
|
| 80 |
local = []
|
| 81 |
|
| 82 |
for f in files:
|
|
|
|
| 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 |
path = hf_hub_download(
|
| 93 |
+
repo,
|
| 94 |
f,
|
| 95 |
token=HF_TOKEN,
|
| 96 |
+
local_dir=os.path.dirname(dst),
|
| 97 |
)
|
| 98 |
local.append(path)
|
| 99 |
+
print("[DOWNLOAD]", repo, f)
|
| 100 |
except Exception as e:
|
| 101 |
print("[DOWNLOAD ERROR]", f, e)
|
| 102 |
|
| 103 |
+
images_cache[key] = local
|
| 104 |
+
print(f"[INFO] {repo} {date}: {len(local)} images")
|
| 105 |
return local
|
| 106 |
|
| 107 |
|
| 108 |
# ======================================================
|
| 109 |
# PAGINATION
|
| 110 |
# ======================================================
|
| 111 |
+
def render_page(repo, date, page):
|
| 112 |
+
images = download_images(repo, date)
|
|
|
|
| 113 |
total_pages = max(1, math.ceil(len(images) / PAGE_SIZE))
|
|
|
|
| 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 |
+
images[start:end],
|
| 121 |
+
page,
|
| 122 |
+
f"Page {page+1} / {total_pages}",
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
|
| 126 |
+
def nav(delta, repo, date, page):
|
| 127 |
+
return render_page(repo, date, page + delta)
|
| 128 |
|
| 129 |
|
| 130 |
+
def change_date(repo, date):
|
| 131 |
+
clear_cache()
|
| 132 |
+
return render_page(repo, date, 0)
|
| 133 |
|
| 134 |
|
| 135 |
+
def change_repo(repo):
|
| 136 |
+
clear_cache()
|
| 137 |
+
dates = list_date_folders(repo)
|
| 138 |
+
first = dates[0] if dates else ""
|
| 139 |
+
imgs, page, info = render_page(repo, first, 0)
|
| 140 |
+
return dates, imgs, page, first, info
|
| 141 |
|
| 142 |
|
| 143 |
# ======================================================
|
|
|
|
| 148 |
return (
|
| 149 |
gr.update(visible=True),
|
| 150 |
gr.update(visible=False),
|
|
|
|
| 151 |
[],
|
| 152 |
0,
|
| 153 |
"",
|
| 154 |
"❌ Wrong password",
|
| 155 |
)
|
| 156 |
|
| 157 |
+
dates = list_date_folders(DEFAULT_REPO)
|
| 158 |
latest = dates[0]
|
| 159 |
|
| 160 |
+
imgs, page, info = render_page(DEFAULT_REPO, latest, 0)
|
| 161 |
|
| 162 |
return (
|
| 163 |
gr.update(visible=False),
|
| 164 |
gr.update(visible=True),
|
|
|
|
| 165 |
imgs,
|
| 166 |
page,
|
| 167 |
latest,
|
| 168 |
info,
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
|
|
|
|
| 174 |
# ======================================================
|
| 175 |
css = """
|
| 176 |
footer, .gradio-footer { display:none!important; }
|
|
|
|
| 177 |
#ohamlab-footer {
|
| 178 |
position:fixed;
|
| 179 |
bottom:0;
|
|
|
|
| 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")
|
|
|
|
| 199 |
|
| 200 |
gr.Markdown("### 🎁 Free Preview")
|
| 201 |
with gr.Row():
|
| 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 |
manual_date = gr.Textbox(
|
| 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 |
+
date_dropdown,
|
| 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),
|
| 281 |
+
inputs=[repo_state, date_state, page_state],
|
| 282 |
+
outputs=[gallery, page_state, page_info],
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
next_btn.click(
|
| 286 |
+
lambda r, d, p: nav(1, r, d, p),
|
| 287 |
+
inputs=[repo_state, date_state, page_state],
|
| 288 |
+
outputs=[gallery, page_state, page_info],
|
| 289 |
)
|
| 290 |
|
| 291 |
gr.HTML("<div id='ohamlab-footer'>© OhamLab Copyright</div>")
|