Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ from huggingface_hub import snapshot_download
|
|
| 13 |
# QUOTA CONFIG
|
| 14 |
# ======================================================
|
| 15 |
LIMIT = 3
|
| 16 |
-
WINDOW = 86400
|
| 17 |
usage = {}
|
| 18 |
|
| 19 |
|
|
@@ -23,7 +23,6 @@ def quota_guard(fn):
|
|
| 23 |
now = time.time()
|
| 24 |
|
| 25 |
count, start = usage.get(session, (0, now))
|
| 26 |
-
|
| 27 |
if now - start > WINDOW:
|
| 28 |
count = 0
|
| 29 |
start = now
|
|
@@ -50,23 +49,21 @@ if not APP_PASSWORD:
|
|
| 50 |
|
| 51 |
# PRO CONTENT
|
| 52 |
PRO_DATASET = "rahul7star/Wan-video"
|
| 53 |
-
VIDEO_COL = "video"
|
| 54 |
-
TEXT_COL = "text"
|
| 55 |
-
DATE_COL = "date"
|
| 56 |
|
| 57 |
# FREE PREVIEW (MODEL REPO)
|
| 58 |
FREE_VIDEO_REPO = "rahul7star/ohamlab"
|
| 59 |
-
FREE_VIDEO_FOLDER = "video"
|
| 60 |
MAX_FREE_VIDEOS = 5
|
| 61 |
|
| 62 |
|
| 63 |
# ======================================================
|
| 64 |
-
# LOAD FREE VIDEOS (MODEL REPO –
|
| 65 |
# ======================================================
|
| 66 |
def load_free_videos():
|
| 67 |
local_dir = snapshot_download(
|
| 68 |
repo_id=FREE_VIDEO_REPO,
|
| 69 |
-
repo_type="model",
|
|
|
|
| 70 |
local_dir=tempfile.mkdtemp(),
|
| 71 |
local_dir_use_symlinks=False,
|
| 72 |
)
|
|
@@ -83,7 +80,7 @@ def load_free_videos():
|
|
| 83 |
if f.lower().endswith((".mp4", ".webm", ".mov"))
|
| 84 |
]
|
| 85 |
|
| 86 |
-
print(f"[INFO] Loaded
|
| 87 |
return videos
|
| 88 |
|
| 89 |
|
|
@@ -97,7 +94,7 @@ def pick_free_videos(max_count=MAX_FREE_VIDEOS):
|
|
| 97 |
if len(FREE_VIDEOS) >= max_count:
|
| 98 |
return random.sample(FREE_VIDEOS, max_count)
|
| 99 |
|
| 100 |
-
# repeat
|
| 101 |
result = []
|
| 102 |
while len(result) < max_count:
|
| 103 |
result.extend(FREE_VIDEOS)
|
|
@@ -109,47 +106,34 @@ def pick_free_videos(max_count=MAX_FREE_VIDEOS):
|
|
| 109 |
# LOAD PRO DATASET
|
| 110 |
# ======================================================
|
| 111 |
dataset = load_dataset(PRO_DATASET, split="test")
|
| 112 |
-
df = dataset.to_pandas()
|
| 113 |
-
df = df[[VIDEO_COL, TEXT_COL, DATE_COL]].dropna().reset_index(drop=True)
|
| 114 |
|
| 115 |
|
| 116 |
# ======================================================
|
| 117 |
-
# PAGINATION
|
| 118 |
# ======================================================
|
| 119 |
def render_grid(page, page_size):
|
| 120 |
page = int(page)
|
| 121 |
page_size = int(page_size)
|
| 122 |
|
| 123 |
-
|
| 124 |
-
total_pages = max(1, math.ceil(total_items / page_size))
|
| 125 |
page = max(0, min(page, total_pages - 1))
|
| 126 |
|
| 127 |
-
|
| 128 |
-
end = start + page_size
|
| 129 |
-
batch = df.iloc[start:end]
|
| 130 |
|
| 131 |
cards = []
|
| 132 |
for _, row in batch.iterrows():
|
| 133 |
cards.append(f"""
|
| 134 |
<div class="card">
|
| 135 |
-
<video src="{row[
|
| 136 |
<div class="meta">
|
| 137 |
-
<div class="date">{row[
|
| 138 |
-
<div class="caption">{row[
|
| 139 |
</div>
|
| 140 |
</div>
|
| 141 |
""")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
<div class="grid">
|
| 145 |
-
{''.join(cards)}
|
| 146 |
-
</div>
|
| 147 |
-
<div class="page-info">
|
| 148 |
-
Page {page + 1} / {total_pages}
|
| 149 |
-
</div>
|
| 150 |
-
"""
|
| 151 |
-
|
| 152 |
-
return html, page
|
| 153 |
|
| 154 |
|
| 155 |
def next_page(page, page_size):
|
|
@@ -165,16 +149,16 @@ def reset_page(page_size):
|
|
| 165 |
|
| 166 |
|
| 167 |
# ======================================================
|
| 168 |
-
# AUTH
|
| 169 |
# ======================================================
|
| 170 |
@quota_guard
|
| 171 |
def check_password(user_password, request: gr.Request):
|
| 172 |
if user_password == APP_PASSWORD:
|
| 173 |
html, page = render_grid(0, 5)
|
| 174 |
return (
|
| 175 |
-
gr.update(visible=False),
|
| 176 |
-
gr.update(visible=False),
|
| 177 |
-
gr.update(visible=True),
|
| 178 |
html,
|
| 179 |
page,
|
| 180 |
"✅ Access granted"
|
|
@@ -190,57 +174,19 @@ def check_password(user_password, request: gr.Request):
|
|
| 190 |
)
|
| 191 |
|
| 192 |
|
| 193 |
-
# ======================================================
|
| 194 |
-
# CSS
|
| 195 |
-
# ======================================================
|
| 196 |
-
css = """
|
| 197 |
-
.grid {
|
| 198 |
-
display: grid;
|
| 199 |
-
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
| 200 |
-
gap: 18px;
|
| 201 |
-
}
|
| 202 |
-
|
| 203 |
-
.card {
|
| 204 |
-
background: #0b1220;
|
| 205 |
-
border-radius: 14px;
|
| 206 |
-
padding: 12px;
|
| 207 |
-
}
|
| 208 |
-
|
| 209 |
-
.card video {
|
| 210 |
-
width: 100%;
|
| 211 |
-
border-radius: 12px;
|
| 212 |
-
}
|
| 213 |
-
|
| 214 |
-
footer, .gradio-footer {
|
| 215 |
-
display: none !important;
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
#ohamlab-footer {
|
| 219 |
-
position: fixed;
|
| 220 |
-
bottom: 0;
|
| 221 |
-
width: 100%;
|
| 222 |
-
text-align: center;
|
| 223 |
-
padding: 8px;
|
| 224 |
-
font-size: 12px;
|
| 225 |
-
background: #f8f9fb;
|
| 226 |
-
border-top: 1px solid #e5e7eb;
|
| 227 |
-
}
|
| 228 |
-
"""
|
| 229 |
-
|
| 230 |
-
|
| 231 |
# ======================================================
|
| 232 |
# UI
|
| 233 |
# ======================================================
|
| 234 |
-
with gr.Blocks(theme=gr.themes.Soft()
|
| 235 |
gr.Markdown("# 🔐 OhamLab Video Showcase")
|
| 236 |
|
| 237 |
-
#
|
| 238 |
with gr.Column(visible=True) as login_box:
|
| 239 |
password_input = gr.Textbox(type="password", label="Password")
|
| 240 |
login_btn = gr.Button("Unlock")
|
| 241 |
login_status = gr.Markdown()
|
| 242 |
|
| 243 |
-
#
|
| 244 |
with gr.Column(visible=True) as free_preview:
|
| 245 |
gr.Markdown("### 🎬 Free Preview")
|
| 246 |
|
|
@@ -248,30 +194,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 248 |
|
| 249 |
with gr.Row():
|
| 250 |
for v in free_videos:
|
| 251 |
-
gr.Video(
|
| 252 |
-
value=v,
|
| 253 |
-
autoplay=False,
|
| 254 |
-
loop=False,
|
| 255 |
-
show_download_button=False
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
# -------- PRO CONTENT --------
|
| 259 |
-
with gr.Column(visible=False) as app_content:
|
| 260 |
-
with gr.Row():
|
| 261 |
-
page_size = gr.Dropdown([1, 5, 10], value=5)
|
| 262 |
-
page_state = gr.State(0)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
gallery = gr.HTML()
|
| 265 |
|
| 266 |
-
with gr.Row():
|
| 267 |
-
prev_btn = gr.Button("⬅ Previous")
|
| 268 |
-
next_btn = gr.Button("Next ➡")
|
| 269 |
-
|
| 270 |
-
prev_btn.click(prev_page, [page_state, page_size], [gallery, page_state])
|
| 271 |
-
next_btn.click(next_page, [page_state, page_size], [gallery, page_state])
|
| 272 |
-
page_size.change(reset_page, [page_size], [gallery, page_state])
|
| 273 |
-
|
| 274 |
-
# -------- EVENTS --------
|
| 275 |
login_btn.click(
|
| 276 |
check_password,
|
| 277 |
inputs=password_input,
|
|
@@ -285,7 +215,4 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 285 |
],
|
| 286 |
)
|
| 287 |
|
| 288 |
-
gr.HTML("<div id='ohamlab-footer'>© OhamLab</div>")
|
| 289 |
-
|
| 290 |
-
|
| 291 |
demo.launch()
|
|
|
|
| 13 |
# QUOTA CONFIG
|
| 14 |
# ======================================================
|
| 15 |
LIMIT = 3
|
| 16 |
+
WINDOW = 86400
|
| 17 |
usage = {}
|
| 18 |
|
| 19 |
|
|
|
|
| 23 |
now = time.time()
|
| 24 |
|
| 25 |
count, start = usage.get(session, (0, now))
|
|
|
|
| 26 |
if now - start > WINDOW:
|
| 27 |
count = 0
|
| 28 |
start = now
|
|
|
|
| 49 |
|
| 50 |
# PRO CONTENT
|
| 51 |
PRO_DATASET = "rahul7star/Wan-video"
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# FREE PREVIEW (MODEL REPO)
|
| 54 |
FREE_VIDEO_REPO = "rahul7star/ohamlab"
|
| 55 |
+
FREE_VIDEO_FOLDER = "showcase/video" # ✅ FIXED PATH
|
| 56 |
MAX_FREE_VIDEOS = 5
|
| 57 |
|
| 58 |
|
| 59 |
# ======================================================
|
| 60 |
+
# LOAD FREE VIDEOS (MODEL REPO – CORRECT PATH)
|
| 61 |
# ======================================================
|
| 62 |
def load_free_videos():
|
| 63 |
local_dir = snapshot_download(
|
| 64 |
repo_id=FREE_VIDEO_REPO,
|
| 65 |
+
repo_type="model", # ✅ correct
|
| 66 |
+
allow_patterns=["showcase/video/*"], # ✅ optional but good
|
| 67 |
local_dir=tempfile.mkdtemp(),
|
| 68 |
local_dir_use_symlinks=False,
|
| 69 |
)
|
|
|
|
| 80 |
if f.lower().endswith((".mp4", ".webm", ".mov"))
|
| 81 |
]
|
| 82 |
|
| 83 |
+
print(f"[INFO] Loaded free videos: {videos}")
|
| 84 |
return videos
|
| 85 |
|
| 86 |
|
|
|
|
| 94 |
if len(FREE_VIDEOS) >= max_count:
|
| 95 |
return random.sample(FREE_VIDEOS, max_count)
|
| 96 |
|
| 97 |
+
# repeat if fewer than max_count
|
| 98 |
result = []
|
| 99 |
while len(result) < max_count:
|
| 100 |
result.extend(FREE_VIDEOS)
|
|
|
|
| 106 |
# LOAD PRO DATASET
|
| 107 |
# ======================================================
|
| 108 |
dataset = load_dataset(PRO_DATASET, split="test")
|
| 109 |
+
df = dataset.to_pandas()[["video", "text", "date"]].dropna().reset_index(drop=True)
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
# ======================================================
|
| 113 |
+
# PAGINATION
|
| 114 |
# ======================================================
|
| 115 |
def render_grid(page, page_size):
|
| 116 |
page = int(page)
|
| 117 |
page_size = int(page_size)
|
| 118 |
|
| 119 |
+
total_pages = max(1, math.ceil(len(df) / page_size))
|
|
|
|
| 120 |
page = max(0, min(page, total_pages - 1))
|
| 121 |
|
| 122 |
+
batch = df.iloc[page * page_size:(page + 1) * page_size]
|
|
|
|
|
|
|
| 123 |
|
| 124 |
cards = []
|
| 125 |
for _, row in batch.iterrows():
|
| 126 |
cards.append(f"""
|
| 127 |
<div class="card">
|
| 128 |
+
<video src="{row['video']}" controls></video>
|
| 129 |
<div class="meta">
|
| 130 |
+
<div class="date">{row['date']}</div>
|
| 131 |
+
<div class="caption">{row['text']}</div>
|
| 132 |
</div>
|
| 133 |
</div>
|
| 134 |
""")
|
| 135 |
|
| 136 |
+
return f"<div class='grid'>{''.join(cards)}</div>", page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
def next_page(page, page_size):
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
# ======================================================
|
| 152 |
+
# AUTH
|
| 153 |
# ======================================================
|
| 154 |
@quota_guard
|
| 155 |
def check_password(user_password, request: gr.Request):
|
| 156 |
if user_password == APP_PASSWORD:
|
| 157 |
html, page = render_grid(0, 5)
|
| 158 |
return (
|
| 159 |
+
gr.update(visible=False),
|
| 160 |
+
gr.update(visible=False),
|
| 161 |
+
gr.update(visible=True),
|
| 162 |
html,
|
| 163 |
page,
|
| 164 |
"✅ Access granted"
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
# ======================================================
|
| 178 |
# UI
|
| 179 |
# ======================================================
|
| 180 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 181 |
gr.Markdown("# 🔐 OhamLab Video Showcase")
|
| 182 |
|
| 183 |
+
# LOGIN
|
| 184 |
with gr.Column(visible=True) as login_box:
|
| 185 |
password_input = gr.Textbox(type="password", label="Password")
|
| 186 |
login_btn = gr.Button("Unlock")
|
| 187 |
login_status = gr.Markdown()
|
| 188 |
|
| 189 |
+
# FREE PREVIEW
|
| 190 |
with gr.Column(visible=True) as free_preview:
|
| 191 |
gr.Markdown("### 🎬 Free Preview")
|
| 192 |
|
|
|
|
| 194 |
|
| 195 |
with gr.Row():
|
| 196 |
for v in free_videos:
|
| 197 |
+
gr.Video(v)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
# PRO CONTENT
|
| 200 |
+
with gr.Column(visible=False) as app_content:
|
| 201 |
+
page_size = gr.Dropdown([1, 5, 10], value=5)
|
| 202 |
+
page_state = gr.State(0)
|
| 203 |
gallery = gr.HTML()
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
login_btn.click(
|
| 206 |
check_password,
|
| 207 |
inputs=password_input,
|
|
|
|
| 215 |
],
|
| 216 |
)
|
| 217 |
|
|
|
|
|
|
|
|
|
|
| 218 |
demo.launch()
|