Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import math
|
|
| 3 |
import time
|
| 4 |
import random
|
| 5 |
import tempfile
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import HfApi, hf_hub_download
|
| 8 |
|
|
@@ -49,6 +51,18 @@ if not APP_PASSWORD:
|
|
| 49 |
api = HfApi()
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# ======================================================
|
| 53 |
# FREE IMAGE PREVIEW
|
| 54 |
# ======================================================
|
|
@@ -62,11 +76,8 @@ def load_free_images():
|
|
| 62 |
and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
| 63 |
]
|
| 64 |
|
| 65 |
-
if not images:
|
| 66 |
-
return []
|
| 67 |
-
|
| 68 |
random.shuffle(images)
|
| 69 |
-
picked =
|
| 70 |
|
| 71 |
local = []
|
| 72 |
for img in picked:
|
|
@@ -86,19 +97,25 @@ FREE_IMAGES = load_free_images()
|
|
| 86 |
# ======================================================
|
| 87 |
def list_date_folders():
|
| 88 |
files = api.list_repo_files(REPO_ID)
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
def load_images_for_date(date_folder):
|
| 93 |
files = api.list_repo_files(REPO_ID)
|
| 94 |
-
|
| 95 |
|
|
|
|
| 96 |
for f in files:
|
| 97 |
-
if f.startswith(
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
return
|
| 102 |
|
| 103 |
|
| 104 |
# ======================================================
|
|
@@ -112,7 +129,7 @@ def render_grid(date_folder, page, page_size):
|
|
| 112 |
start, end = page * page_size, (page + 1) * page_size
|
| 113 |
|
| 114 |
cards = [
|
| 115 |
-
f"<div class='card'><img src='{img}'></div>"
|
| 116 |
for img in data[start:end]
|
| 117 |
]
|
| 118 |
|
|
@@ -125,12 +142,10 @@ def render_grid(date_folder, page, page_size):
|
|
| 125 |
def next_page(date, page, size):
|
| 126 |
return render_grid(date, page + 1, size)
|
| 127 |
|
|
|
|
| 128 |
def prev_page(date, page, size):
|
| 129 |
return render_grid(date, page - 1, size)
|
| 130 |
|
| 131 |
-
def reset_page(date, size):
|
| 132 |
-
return render_grid(date, 0, size)
|
| 133 |
-
|
| 134 |
|
| 135 |
# ======================================================
|
| 136 |
# AUTH
|
|
@@ -145,11 +160,12 @@ def check_password(pwd):
|
|
| 145 |
"",
|
| 146 |
0,
|
| 147 |
"❌ Wrong password",
|
| 148 |
-
|
| 149 |
)
|
| 150 |
|
| 151 |
dates = list_date_folders()
|
| 152 |
-
latest = dates[
|
|
|
|
| 153 |
html, page = render_grid(latest, 0, 6)
|
| 154 |
|
| 155 |
return (
|
|
@@ -181,9 +197,7 @@ css = """
|
|
| 181 |
width:100%;
|
| 182 |
border-radius:10px;
|
| 183 |
}
|
| 184 |
-
|
| 185 |
footer, .gradio-footer { display:none!important; }
|
| 186 |
-
|
| 187 |
#ohamlab-footer {
|
| 188 |
position:fixed;
|
| 189 |
bottom:0;
|
|
@@ -200,10 +214,9 @@ footer, .gradio-footer { display:none!important; }
|
|
| 200 |
# ======================================================
|
| 201 |
# UI
|
| 202 |
# ======================================================
|
| 203 |
-
with gr.Blocks(
|
| 204 |
-
gr.Markdown("# 🔐 OhamLab Image Showcase
|
| 205 |
|
| 206 |
-
# ---------- LOGIN ----------
|
| 207 |
with gr.Column(visible=True) as login_box:
|
| 208 |
pwd = gr.Textbox(type="password", label="Password")
|
| 209 |
login_btn = gr.Button("Unlock")
|
|
@@ -215,7 +228,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 215 |
for img in FREE_IMAGES:
|
| 216 |
gr.Image(img, show_label=False)
|
| 217 |
|
| 218 |
-
# ---------- PRO ----------
|
| 219 |
with gr.Column(visible=False) as app:
|
| 220 |
current_date = gr.State("")
|
| 221 |
page_state = gr.State(0)
|
|
@@ -236,7 +248,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 236 |
outputs=[login_box, app, free_preview, gallery, page_state, status, current_date]
|
| 237 |
)
|
| 238 |
|
| 239 |
-
gr.HTML("<div id='ohamlab-footer'>© OhamLab
|
| 240 |
|
| 241 |
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
import random
|
| 5 |
import tempfile
|
| 6 |
+
from datetime import datetime, date
|
| 7 |
+
|
| 8 |
import gradio as gr
|
| 9 |
from huggingface_hub import HfApi, hf_hub_download
|
| 10 |
|
|
|
|
| 51 |
api = HfApi()
|
| 52 |
|
| 53 |
|
| 54 |
+
# ======================================================
|
| 55 |
+
# DATE PARSING
|
| 56 |
+
# ======================================================
|
| 57 |
+
def parse_date_safe(name: str) -> datetime:
|
| 58 |
+
for fmt in ("%Y-%m-%d", "%Y_%m_%d", "%Y%m%d"):
|
| 59 |
+
try:
|
| 60 |
+
return datetime.strptime(name, fmt)
|
| 61 |
+
except ValueError:
|
| 62 |
+
pass
|
| 63 |
+
return datetime.combine(date.today(), datetime.min.time())
|
| 64 |
+
|
| 65 |
+
|
| 66 |
# ======================================================
|
| 67 |
# FREE IMAGE PREVIEW
|
| 68 |
# ======================================================
|
|
|
|
| 76 |
and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
| 77 |
]
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
random.shuffle(images)
|
| 80 |
+
picked = images[:5]
|
| 81 |
|
| 82 |
local = []
|
| 83 |
for img in picked:
|
|
|
|
| 97 |
# ======================================================
|
| 98 |
def list_date_folders():
|
| 99 |
files = api.list_repo_files(REPO_ID)
|
| 100 |
+
folders = {f.split("/")[0] for f in files if "/" in f}
|
| 101 |
+
|
| 102 |
+
return sorted(folders, key=parse_date_safe, reverse=True)
|
| 103 |
|
| 104 |
|
| 105 |
def load_images_for_date(date_folder):
|
| 106 |
files = api.list_repo_files(REPO_ID)
|
| 107 |
+
prefix = f"{date_folder}/"
|
| 108 |
|
| 109 |
+
images = []
|
| 110 |
for f in files:
|
| 111 |
+
if f.startswith(prefix) and f.lower().endswith(
|
| 112 |
+
(".png", ".jpg", ".jpeg", ".webp")
|
| 113 |
+
):
|
| 114 |
+
images.append(
|
| 115 |
+
f"https://huggingface.co/{REPO_ID}/resolve/main/{f}"
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
return images
|
| 119 |
|
| 120 |
|
| 121 |
# ======================================================
|
|
|
|
| 129 |
start, end = page * page_size, (page + 1) * page_size
|
| 130 |
|
| 131 |
cards = [
|
| 132 |
+
f"<div class='card'><img src='{img}' loading='lazy'></div>"
|
| 133 |
for img in data[start:end]
|
| 134 |
]
|
| 135 |
|
|
|
|
| 142 |
def next_page(date, page, size):
|
| 143 |
return render_grid(date, page + 1, size)
|
| 144 |
|
| 145 |
+
|
| 146 |
def prev_page(date, page, size):
|
| 147 |
return render_grid(date, page - 1, size)
|
| 148 |
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# ======================================================
|
| 151 |
# AUTH
|
|
|
|
| 160 |
"",
|
| 161 |
0,
|
| 162 |
"❌ Wrong password",
|
| 163 |
+
""
|
| 164 |
)
|
| 165 |
|
| 166 |
dates = list_date_folders()
|
| 167 |
+
latest = dates[0] # newest date folder
|
| 168 |
+
|
| 169 |
html, page = render_grid(latest, 0, 6)
|
| 170 |
|
| 171 |
return (
|
|
|
|
| 197 |
width:100%;
|
| 198 |
border-radius:10px;
|
| 199 |
}
|
|
|
|
| 200 |
footer, .gradio-footer { display:none!important; }
|
|
|
|
| 201 |
#ohamlab-footer {
|
| 202 |
position:fixed;
|
| 203 |
bottom:0;
|
|
|
|
| 214 |
# ======================================================
|
| 215 |
# UI
|
| 216 |
# ======================================================
|
| 217 |
+
with gr.Blocks() as demo:
|
| 218 |
+
gr.Markdown("# 🔐 OhamLab Image Showcase – PRO Users Only")
|
| 219 |
|
|
|
|
| 220 |
with gr.Column(visible=True) as login_box:
|
| 221 |
pwd = gr.Textbox(type="password", label="Password")
|
| 222 |
login_btn = gr.Button("Unlock")
|
|
|
|
| 228 |
for img in FREE_IMAGES:
|
| 229 |
gr.Image(img, show_label=False)
|
| 230 |
|
|
|
|
| 231 |
with gr.Column(visible=False) as app:
|
| 232 |
current_date = gr.State("")
|
| 233 |
page_state = gr.State(0)
|
|
|
|
| 248 |
outputs=[login_box, app, free_preview, gallery, page_state, status, current_date]
|
| 249 |
)
|
| 250 |
|
| 251 |
+
gr.HTML("<div id='ohamlab-footer'>© OhamLab</div>")
|
| 252 |
|
| 253 |
|
| 254 |
+
if __name__ == "__main__":
|
| 255 |
+
demo.launch(
|
| 256 |
+
theme=gr.themes.Soft(),
|
| 257 |
+
css=css,
|
| 258 |
+
prevent_thread_lock=True,
|
| 259 |
+
ssr_mode=False,
|
| 260 |
+
show_error=True
|
| 261 |
+
)
|