File size: 8,189 Bytes
ce9d458
ffe7caa
 
3a65be4
15b177f
ffe7caa
 
 
 
 
 
 
 
7cfa4ef
baeb067
ffe7caa
09581d8
ffe7caa
 
 
 
09581d8
ffe7caa
 
09581d8
ffe7caa
 
38052bd
ffe7caa
 
 
 
 
 
7cfa4ef
ffe7caa
 
 
 
 
7cfa4ef
ffe7caa
 
977e5ae
ffe7caa
 
 
 
 
 
7cfa4ef
ffe7caa
3a65be4
7cfa4ef
ffe7caa
3a65be4
ffe7caa
 
 
 
 
 
2d21be1
 
ffe7caa
 
 
 
 
baeb067
ffe7caa
7cfa4ef
 
ffe7caa
 
 
 
977e5ae
ffe7caa
 
977e5ae
ffe7caa
 
 
 
7cfa4ef
 
ffe7caa
 
 
 
fb48d8d
ffe7caa
 
 
 
 
fb48d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe7caa
fb48d8d
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d21be1
7cfa4ef
ffe7caa
 
7cfa4ef
 
ffe7caa
 
 
7cfa4ef
 
ffe7caa
 
 
 
 
 
7cfa4ef
1fd6d89
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
977e5ae
ffe7caa
 
7cfa4ef
ffe7caa
977e5ae
ffe7caa
 
 
 
 
 
 
 
ce9d458
7cfa4ef
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
 
38052bd
ffe7caa
d08ffde
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b177f
 
 
ffe7caa
15b177f
ffe7caa
 
 
 
 
15b177f
 
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cfa4ef
 
ffe7caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
baeb067
ffe7caa
 
 
 
 
 
 
 
 
2d21be1
ffe7caa
 
 
 
 
 
 
 
15b177f
 
2d21be1
 
ffe7caa
 
2d21be1
 
 
 
ffe7caa
 
15b177f
 
ffe7caa
5028211
9fed8d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
import math
import random
import tempfile
import gradio as gr
from huggingface_hub import HfApi, hf_hub_download

# ======================================================
# CONFIG
# ======================================================
DEFAULT_REPO = "rahul7star/WAN22-Mar-2026"
FREE_REPO = "rahul7star/ohamlab"
FREE_IMAGE_DIR = "showcase/image"

HF_TOKEN = os.getenv("HF_TOKEN")
APP_PASSWORD = os.getenv("APP_PASSWORD")

if not HF_TOKEN:
    raise RuntimeError("HF_TOKEN not set")
if not APP_PASSWORD:
    raise RuntimeError("APP_PASSWORD not set")

api = HfApi()
PAGE_SIZE = 6

tmp_dir = tempfile.mkdtemp()
images_cache = {}   # (repo, date) -> images

# ======================================================
# FREE PREVIEW
# ======================================================
def load_free_images():
    tmp = tempfile.mkdtemp()
    files = api.list_repo_files(FREE_REPO, token=HF_TOKEN)

    imgs = [
        f for f in files
        if f.startswith(FREE_IMAGE_DIR)
        and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
    ]

    random.shuffle(imgs)
    imgs = imgs[:5]

    out = []
    for f in imgs:
        try:
            out.append(hf_hub_download(FREE_REPO, f, token=HF_TOKEN, local_dir=tmp))
        except Exception as e:
            print("[FREE IMG ERROR]", e)

    return out


FREE_IMAGES = load_free_images()

# ======================================================
# HELPERS
# ======================================================
def clear_cache():
    images_cache.clear()
    print("[CACHE] cleared all")


def list_date_folders(repo, last_n=20):
    files = api.list_repo_files(repo, token=HF_TOKEN)
    dates = sorted(
        {f.split("/")[0] for f in files if "/" in f},
        reverse=True
    )
    return dates[:last_n]


def download_images(repo, date):
    key = (repo, date)
    if key in images_cache:
        return images_cache[key]

    files = api.list_repo_files(repo, token=HF_TOKEN)
    local = []

    for f in files:
        if not f.startswith(date + "/"):
            continue
        if not f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
            continue

        dst = os.path.join(tmp_dir, repo.replace("/", "_"), f)
        os.makedirs(os.path.dirname(dst), exist_ok=True)

        try:
            img_path = hf_hub_download(
                repo,
                f,
                token=HF_TOKEN,
                local_dir=os.path.dirname(dst),
            )

            # ------------------------------------------
            # NEW: load summary.txt from same folder
            # ------------------------------------------
            summary_text = ""
            summary_file = os.path.join(os.path.dirname(f), "summary.txt")

            if summary_file in files:
                try:
                    summary_path = hf_hub_download(
                        repo,
                        summary_file,
                        token=HF_TOKEN,
                        local_dir=os.path.dirname(dst),
                    )
                    with open(summary_path, "r", encoding="utf-8") as sf:
                        summary_text = sf.read().strip()
                except Exception as e:
                    print("[SUMMARY ERROR]", summary_file, e)

            # Gallery item = (image, caption)
            local.append((img_path, summary_text))

            print("[DOWNLOAD]", repo, f)

        except Exception as e:
            print("[DOWNLOAD ERROR]", f, e)

    images_cache[key] = local
    print(f"[INFO] {repo} {date}: {len(local)} images")
    return local


# ======================================================
# PAGINATION
# ======================================================
def render_page(repo, date, page):
    images = download_images(repo, date)
    total_pages = max(1, math.ceil(len(images) / PAGE_SIZE))

    page = max(0, min(int(page), total_pages - 1))
    start = page * PAGE_SIZE
    end = start + PAGE_SIZE

    return (
        images[start:end],
        page,
        f"Page {page+1} / {total_pages}",
    )


def nav(delta, repo, date, page):
    return render_page(repo, date, page + delta)


def change_date(repo, date):
    clear_cache()
    return render_page(repo, date, 0)


def change_repo(repo):
    clear_cache()
    dates = list_date_folders(repo)
    first = dates[0] if dates else ""
    imgs, page, info = render_page(repo, first, 0)
    return dates, imgs, page, first, info


# ======================================================
# AUTH
# ======================================================
def check_password(pwd):
    if pwd != APP_PASSWORD:
        return (
            gr.update(visible=True),
            gr.update(visible=False),
            [],
            0,
            "",
            "❌ Wrong password",
        )

    dates = list_date_folders(DEFAULT_REPO)
    latest = dates[0]

    imgs, page, info = render_page(DEFAULT_REPO, latest, 0)

    return (
        gr.update(visible=False),
        gr.update(visible=True),
        imgs,
        page,
        latest,
        info,
    )


# ======================================================
# CSS
# ======================================================
css = """
footer, .gradio-footer { display:none!important; }
#ohamlab-footer {
  position:fixed;
  bottom:0;
  width:100%;
  background:#f8f9fb;
  font-size:12px;
  padding:8px;
  border-top:1px solid #e5e7eb;
  text-align:center;
}
"""

# ======================================================
# UI
# ======================================================
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
    gr.Markdown("# 🔐 OhamLab Image Showcase (PRO)")

    # ---------- LOGIN ----------
    with gr.Column(visible=True) as login_box:
        pwd = gr.Textbox(type="password", label="Password")
        login_btn = gr.Button("Unlock")

        gr.Markdown("### 🎁 Free Preview")
        with gr.Row():
            for img in FREE_IMAGES:
                gr.Image(img, show_label=False)

    # ---------- APP ----------
    with gr.Column(visible=False) as app:
        repo_state = gr.State(DEFAULT_REPO)
        date_state = gr.State("")
        page_state = gr.State(0)

        repo_input = gr.Textbox(
            value=DEFAULT_REPO,
            label="Base HuggingFace Repo",
        )

        date_dropdown = gr.Dropdown(label="Select Date")

        manual_date = gr.Textbox(
            label="Or enter date manually (YYYY-MM-DD)"
        )

        gallery = gr.Gallery(columns=3, height="auto")
        page_info = gr.Markdown()

        with gr.Row():
            prev_btn = gr.Button("⬅ Previous")
            next_btn = gr.Button("Next ➡")

    # ---------- EVENTS ----------
    login_btn.click(
        check_password,
        inputs=pwd,
        outputs=[
            login_box,
            app,
            gallery,
            page_state,
            date_state,
            page_info,
        ],
    )

    repo_input.change(
        change_repo,
        inputs=repo_input,
        outputs=[
            date_dropdown,
            gallery,
            page_state,
            date_state,
            page_info,
        ],
    ).then(
        lambda r: r,
        inputs=repo_input,
        outputs=repo_state,
    )

    date_dropdown.change(
        lambda r, d: change_date(r, d),
        inputs=[repo_state, date_dropdown],
        outputs=[gallery, page_state, page_info],
    ).then(
        lambda d: d,
        inputs=date_dropdown,
        outputs=date_state,
    )

    manual_date.submit(
        lambda r, d: change_date(r, d),
        inputs=[repo_state, manual_date],
        outputs=[gallery, page_state, page_info],
    ).then(
        lambda d: d,
        inputs=manual_date,
        outputs=date_state,
    )

    prev_btn.click(
        lambda r, d, p: nav(-1, r, d, p),
        inputs=[repo_state, date_state, page_state],
        outputs=[gallery, page_state, page_info],
    )

    next_btn.click(
        lambda r, d, p: nav(1, r, d, p),
        inputs=[repo_state, date_state, page_state],
        outputs=[gallery, page_state, page_info],
    )

    gr.HTML("<div id='ohamlab-footer'>© OhamLab Copyright</div>")

demo.launch()