Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,18 @@ import os
|
|
| 2 |
import math
|
| 3 |
import time
|
| 4 |
import random
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
from datasets import load_dataset
|
|
|
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
# QUOTA CONFIG
|
| 11 |
# ======================================================
|
| 12 |
LIMIT = 3
|
| 13 |
-
WINDOW = 86400
|
| 14 |
-
usage = {}
|
| 15 |
|
| 16 |
|
| 17 |
def quota_guard(fn):
|
|
@@ -20,7 +22,6 @@ def quota_guard(fn):
|
|
| 20 |
now = time.time()
|
| 21 |
|
| 22 |
count, start = usage.get(session, (0, now))
|
| 23 |
-
|
| 24 |
if now - start > WINDOW:
|
| 25 |
count = 0
|
| 26 |
start = now
|
|
@@ -41,59 +42,58 @@ def quota_guard(fn):
|
|
| 41 |
# ======================================================
|
| 42 |
# CONFIG
|
| 43 |
# ======================================================
|
| 44 |
-
DATASET_NAME = "rahul7star/Wan-video"
|
| 45 |
-
|
| 46 |
-
VIDEO_COL = "video"
|
| 47 |
-
TEXT_COL = "text"
|
| 48 |
-
DATE_COL = "date"
|
| 49 |
-
|
| 50 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 51 |
if not APP_PASSWORD:
|
| 52 |
raise RuntimeError("APP_PASSWORD environment variable not set")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# ======================================================
|
| 56 |
-
#
|
| 57 |
# ======================================================
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
picks = random.sample(YOUTUBE_LINKS, 4)
|
| 69 |
-
cards = []
|
| 70 |
|
| 71 |
-
for link in picks:
|
| 72 |
-
vid = link.split("v=")[-1]
|
| 73 |
-
cards.append(f"""
|
| 74 |
-
<div class="free-card">
|
| 75 |
-
<iframe
|
| 76 |
-
src="https://www.youtube.com/embed/{vid}"
|
| 77 |
-
allowfullscreen
|
| 78 |
-
loading="lazy">
|
| 79 |
-
</iframe>
|
| 80 |
-
</div>
|
| 81 |
-
""")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
# ======================================================
|
| 92 |
-
# LOAD DATASET
|
| 93 |
# ======================================================
|
| 94 |
-
dataset = load_dataset(
|
| 95 |
-
df = dataset.to_pandas()
|
| 96 |
-
df = df[[VIDEO_COL, TEXT_COL, DATE_COL]].dropna().reset_index(drop=True)
|
| 97 |
|
| 98 |
|
| 99 |
# ======================================================
|
|
@@ -103,30 +103,24 @@ def render_grid(page, page_size):
|
|
| 103 |
page = int(page)
|
| 104 |
page_size = int(page_size)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
total_pages = max(1, math.ceil(total_items / page_size))
|
| 108 |
page = max(0, min(page, total_pages - 1))
|
| 109 |
|
| 110 |
-
batch = df.iloc[page * page_size:
|
| 111 |
|
| 112 |
cards = []
|
| 113 |
for _, row in batch.iterrows():
|
| 114 |
cards.append(f"""
|
| 115 |
<div class="card">
|
| 116 |
-
<video src="{row[
|
| 117 |
<div class="meta">
|
| 118 |
-
<div class="date">{row[
|
| 119 |
-
<div class="caption">{row[
|
| 120 |
</div>
|
| 121 |
</div>
|
| 122 |
""")
|
| 123 |
|
| 124 |
-
|
| 125 |
-
<div class="grid">{''.join(cards)}</div>
|
| 126 |
-
<div class="page-info">Page {page + 1} / {total_pages}</div>
|
| 127 |
-
"""
|
| 128 |
-
|
| 129 |
-
return html, page
|
| 130 |
|
| 131 |
|
| 132 |
def next_page(page, page_size):
|
|
@@ -149,22 +143,22 @@ def check_password(user_password, request: gr.Request):
|
|
| 149 |
if user_password == APP_PASSWORD:
|
| 150 |
html, page = render_grid(0, 5)
|
| 151 |
return (
|
| 152 |
-
gr.update(visible=False), # login
|
| 153 |
gr.update(visible=False), # free preview
|
| 154 |
-
gr.update(visible=True), #
|
| 155 |
html,
|
| 156 |
page,
|
| 157 |
"✅ Access granted"
|
| 158 |
)
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
|
| 170 |
# ======================================================
|
|
@@ -188,20 +182,6 @@ css = """
|
|
| 188 |
border-radius: 12px;
|
| 189 |
}
|
| 190 |
|
| 191 |
-
.free-grid {
|
| 192 |
-
display: grid;
|
| 193 |
-
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
|
| 194 |
-
gap: 16px;
|
| 195 |
-
margin-top: 12px;
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
.free-card iframe {
|
| 199 |
-
width: 100%;
|
| 200 |
-
aspect-ratio: 16 / 9;
|
| 201 |
-
border-radius: 12px;
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
/* Hide default footer */
|
| 205 |
footer, .gradio-footer {
|
| 206 |
display: none !important;
|
| 207 |
}
|
|
@@ -223,18 +203,23 @@ footer, .gradio-footer {
|
|
| 223 |
# UI
|
| 224 |
# ======================================================
|
| 225 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 226 |
-
gr.Markdown("# 🔐 OhamLab Video Showcase")
|
| 227 |
|
| 228 |
-
#
|
| 229 |
with gr.Column(visible=True) as login_box:
|
| 230 |
password_input = gr.Textbox(type="password", label="Password")
|
| 231 |
login_btn = gr.Button("Unlock")
|
| 232 |
login_status = gr.Markdown()
|
| 233 |
|
|
|
|
| 234 |
with gr.Column(visible=True) as free_preview:
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
#
|
| 238 |
with gr.Column(visible=False) as app_content:
|
| 239 |
with gr.Row():
|
| 240 |
page_size = gr.Dropdown([1, 5, 10], value=5)
|
|
@@ -250,7 +235,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 250 |
next_btn.click(next_page, [page_state, page_size], [gallery, page_state])
|
| 251 |
page_size.change(reset_page, [page_size], [gallery, page_state])
|
| 252 |
|
| 253 |
-
#
|
| 254 |
login_btn.click(
|
| 255 |
check_password,
|
| 256 |
inputs=password_input,
|
|
|
|
| 2 |
import math
|
| 3 |
import time
|
| 4 |
import random
|
| 5 |
+
import tempfile
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
from datasets import load_dataset
|
| 9 |
+
from huggingface_hub import snapshot_download
|
| 10 |
|
| 11 |
# ======================================================
|
| 12 |
# QUOTA CONFIG
|
| 13 |
# ======================================================
|
| 14 |
LIMIT = 3
|
| 15 |
+
WINDOW = 86400
|
| 16 |
+
usage = {}
|
| 17 |
|
| 18 |
|
| 19 |
def quota_guard(fn):
|
|
|
|
| 22 |
now = time.time()
|
| 23 |
|
| 24 |
count, start = usage.get(session, (0, now))
|
|
|
|
| 25 |
if now - start > WINDOW:
|
| 26 |
count = 0
|
| 27 |
start = now
|
|
|
|
| 42 |
# ======================================================
|
| 43 |
# CONFIG
|
| 44 |
# ======================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 46 |
if not APP_PASSWORD:
|
| 47 |
raise RuntimeError("APP_PASSWORD environment variable not set")
|
| 48 |
|
| 49 |
+
PRO_DATASET = "rahul7star/Wan-video"
|
| 50 |
+
FREE_VIDEO_REPO = "rahul7star/ohamlab"
|
| 51 |
+
FREE_VIDEO_FOLDER = "video"
|
| 52 |
+
|
| 53 |
|
| 54 |
# ======================================================
|
| 55 |
+
# LOAD FREE VIDEOS FROM HF REPO
|
| 56 |
# ======================================================
|
| 57 |
+
def load_free_videos():
|
| 58 |
+
local_dir = snapshot_download(
|
| 59 |
+
repo_id=FREE_VIDEO_REPO,
|
| 60 |
+
allow_patterns=[f"{FREE_VIDEO_FOLDER}/*"],
|
| 61 |
+
local_dir=tempfile.mkdtemp(),
|
| 62 |
+
repo_type="dataset",
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
video_dir = os.path.join(local_dir, FREE_VIDEO_FOLDER)
|
| 66 |
+
videos = [
|
| 67 |
+
os.path.join(video_dir, f)
|
| 68 |
+
for f in os.listdir(video_dir)
|
| 69 |
+
if f.lower().endswith((".mp4", ".webm", ".mov"))
|
| 70 |
+
]
|
| 71 |
|
| 72 |
+
return videos
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
FREE_VIDEOS = load_free_videos()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def pick_free_videos(max_count=5):
|
| 79 |
+
if not FREE_VIDEOS:
|
| 80 |
+
return []
|
| 81 |
+
|
| 82 |
+
if len(FREE_VIDEOS) >= max_count:
|
| 83 |
+
return random.sample(FREE_VIDEOS, max_count)
|
| 84 |
+
|
| 85 |
+
# repeat if less than max_count
|
| 86 |
+
result = []
|
| 87 |
+
while len(result) < max_count:
|
| 88 |
+
result.extend(FREE_VIDEOS)
|
| 89 |
+
return result[:max_count]
|
| 90 |
|
| 91 |
|
| 92 |
# ======================================================
|
| 93 |
+
# LOAD PRO DATASET
|
| 94 |
# ======================================================
|
| 95 |
+
dataset = load_dataset(PRO_DATASET, split="test")
|
| 96 |
+
df = dataset.to_pandas()[["video", "text", "date"]].dropna().reset_index(drop=True)
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
# ======================================================
|
|
|
|
| 103 |
page = int(page)
|
| 104 |
page_size = int(page_size)
|
| 105 |
|
| 106 |
+
total_pages = max(1, math.ceil(len(df) / page_size))
|
|
|
|
| 107 |
page = max(0, min(page, total_pages - 1))
|
| 108 |
|
| 109 |
+
batch = df.iloc[page * page_size:(page + 1) * page_size]
|
| 110 |
|
| 111 |
cards = []
|
| 112 |
for _, row in batch.iterrows():
|
| 113 |
cards.append(f"""
|
| 114 |
<div class="card">
|
| 115 |
+
<video src="{row['video']}" controls></video>
|
| 116 |
<div class="meta">
|
| 117 |
+
<div class="date">{row['date']}</div>
|
| 118 |
+
<div class="caption">{row['text']}</div>
|
| 119 |
</div>
|
| 120 |
</div>
|
| 121 |
""")
|
| 122 |
|
| 123 |
+
return f"<div class='grid'>{''.join(cards)}</div>", page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
def next_page(page, page_size):
|
|
|
|
| 143 |
if user_password == APP_PASSWORD:
|
| 144 |
html, page = render_grid(0, 5)
|
| 145 |
return (
|
| 146 |
+
gr.update(visible=False), # login
|
| 147 |
gr.update(visible=False), # free preview
|
| 148 |
+
gr.update(visible=True), # pro content
|
| 149 |
html,
|
| 150 |
page,
|
| 151 |
"✅ Access granted"
|
| 152 |
)
|
| 153 |
+
|
| 154 |
+
return (
|
| 155 |
+
gr.update(visible=True),
|
| 156 |
+
gr.update(visible=True),
|
| 157 |
+
gr.update(visible=False),
|
| 158 |
+
"",
|
| 159 |
+
0,
|
| 160 |
+
"❌ Incorrect password"
|
| 161 |
+
)
|
| 162 |
|
| 163 |
|
| 164 |
# ======================================================
|
|
|
|
| 182 |
border-radius: 12px;
|
| 183 |
}
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
footer, .gradio-footer {
|
| 186 |
display: none !important;
|
| 187 |
}
|
|
|
|
| 203 |
# UI
|
| 204 |
# ======================================================
|
| 205 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 206 |
+
gr.Markdown("# 🔐 OhamLab Video Showcase - For PRO Users 18+")
|
| 207 |
|
| 208 |
+
# ---------- LOGIN ----------
|
| 209 |
with gr.Column(visible=True) as login_box:
|
| 210 |
password_input = gr.Textbox(type="password", label="Password")
|
| 211 |
login_btn = gr.Button("Unlock")
|
| 212 |
login_status = gr.Markdown()
|
| 213 |
|
| 214 |
+
# ---------- FREE PREVIEW ----------
|
| 215 |
with gr.Column(visible=True) as free_preview:
|
| 216 |
+
gr.Markdown("### 🎬 Free Preview")
|
| 217 |
+
free_videos = pick_free_videos(5)
|
| 218 |
+
with gr.Row():
|
| 219 |
+
for v in free_videos:
|
| 220 |
+
gr.Video(v, autoplay=False)
|
| 221 |
|
| 222 |
+
# ---------- PRO CONTENT ----------
|
| 223 |
with gr.Column(visible=False) as app_content:
|
| 224 |
with gr.Row():
|
| 225 |
page_size = gr.Dropdown([1, 5, 10], value=5)
|
|
|
|
| 235 |
next_btn.click(next_page, [page_state, page_size], [gallery, page_state])
|
| 236 |
page_size.change(reset_page, [page_size], [gallery, page_state])
|
| 237 |
|
| 238 |
+
# ---------- EVENTS ----------
|
| 239 |
login_btn.click(
|
| 240 |
check_password,
|
| 241 |
inputs=password_input,
|