Spaces:
Running
Running
add free play video
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import math
|
| 3 |
import time
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
from datasets import load_dataset
|
|
@@ -15,13 +16,11 @@ usage = {} # session_hash -> (count, start_time)
|
|
| 15 |
|
| 16 |
def quota_guard(fn):
|
| 17 |
def wrapper(*args, request: gr.Request = None, **kwargs):
|
| 18 |
-
# ---- session id ----
|
| 19 |
session = request.session_hash if request else "global"
|
| 20 |
now = time.time()
|
| 21 |
|
| 22 |
count, start = usage.get(session, (0, now))
|
| 23 |
|
| 24 |
-
# Reset after window
|
| 25 |
if now - start > WINDOW:
|
| 26 |
count = 0
|
| 27 |
start = now
|
|
@@ -34,8 +33,6 @@ def quota_guard(fn):
|
|
| 34 |
)
|
| 35 |
|
| 36 |
usage[session] = (count + 1, start)
|
| 37 |
-
|
| 38 |
-
# IMPORTANT: forward request explicitly
|
| 39 |
return fn(*args, request=request, **kwargs)
|
| 40 |
|
| 41 |
return wrapper
|
|
@@ -55,6 +52,42 @@ if not APP_PASSWORD:
|
|
| 55 |
raise RuntimeError("APP_PASSWORD environment variable not set")
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ======================================================
|
| 59 |
# LOAD DATASET
|
| 60 |
# ======================================================
|
|
@@ -64,7 +97,7 @@ df = df[[VIDEO_COL, TEXT_COL, DATE_COL]].dropna().reset_index(drop=True)
|
|
| 64 |
|
| 65 |
|
| 66 |
# ======================================================
|
| 67 |
-
# PAGINATION
|
| 68 |
# ======================================================
|
| 69 |
def render_grid(page, page_size):
|
| 70 |
page = int(page)
|
|
@@ -74,9 +107,7 @@ def render_grid(page, page_size):
|
|
| 74 |
total_pages = max(1, math.ceil(total_items / page_size))
|
| 75 |
page = max(0, min(page, total_pages - 1))
|
| 76 |
|
| 77 |
-
|
| 78 |
-
end = start + page_size
|
| 79 |
-
batch = df.iloc[start:end]
|
| 80 |
|
| 81 |
cards = []
|
| 82 |
for _, row in batch.iterrows():
|
|
@@ -91,12 +122,8 @@ def render_grid(page, page_size):
|
|
| 91 |
""")
|
| 92 |
|
| 93 |
html = f"""
|
| 94 |
-
<div class="grid">
|
| 95 |
-
|
| 96 |
-
</div>
|
| 97 |
-
<div class="page-info">
|
| 98 |
-
Page {page + 1} / {total_pages}
|
| 99 |
-
</div>
|
| 100 |
"""
|
| 101 |
|
| 102 |
return html, page
|
|
@@ -115,21 +142,23 @@ def reset_page(page_size):
|
|
| 115 |
|
| 116 |
|
| 117 |
# ======================================================
|
| 118 |
-
# AUTH
|
| 119 |
# ======================================================
|
| 120 |
@quota_guard
|
| 121 |
def check_password(user_password, request: gr.Request):
|
| 122 |
if user_password == APP_PASSWORD:
|
| 123 |
html, page = render_grid(0, 5)
|
| 124 |
return (
|
| 125 |
-
gr.update(visible=False),
|
| 126 |
-
gr.update(visible=
|
|
|
|
| 127 |
html,
|
| 128 |
page,
|
| 129 |
"✅ Access granted"
|
| 130 |
)
|
| 131 |
else:
|
| 132 |
return (
|
|
|
|
| 133 |
gr.update(visible=True),
|
| 134 |
gr.update(visible=False),
|
| 135 |
"",
|
|
@@ -159,24 +188,32 @@ css = """
|
|
| 159 |
border-radius: 12px;
|
| 160 |
}
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
display: none !important;
|
| 166 |
}
|
| 167 |
|
| 168 |
-
/* ---- Custom OhamLab footer ---- */
|
| 169 |
#ohamlab-footer {
|
| 170 |
position: fixed;
|
| 171 |
bottom: 0;
|
| 172 |
-
left: 0;
|
| 173 |
width: 100%;
|
| 174 |
text-align: center;
|
| 175 |
-
padding: 8px
|
| 176 |
font-size: 12px;
|
| 177 |
background: #f8f9fb;
|
| 178 |
-
color: #555;
|
| 179 |
-
z-index: 9999;
|
| 180 |
border-top: 1px solid #e5e7eb;
|
| 181 |
}
|
| 182 |
"""
|
|
@@ -186,13 +223,18 @@ footer,
|
|
| 186 |
# UI
|
| 187 |
# ======================================================
|
| 188 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 189 |
-
gr.Markdown("# 🔐 OhamLab Video Showcase
|
| 190 |
|
|
|
|
| 191 |
with gr.Column(visible=True) as login_box:
|
| 192 |
password_input = gr.Textbox(type="password", label="Password")
|
| 193 |
login_btn = gr.Button("Unlock")
|
| 194 |
login_status = gr.Markdown()
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
with gr.Column(visible=False) as app_content:
|
| 197 |
with gr.Row():
|
| 198 |
page_size = gr.Dropdown([1, 5, 10], value=5)
|
|
@@ -208,14 +250,21 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 208 |
next_btn.click(next_page, [page_state, page_size], [gallery, page_state])
|
| 209 |
page_size.change(reset_page, [page_size], [gallery, page_state])
|
| 210 |
|
|
|
|
| 211 |
login_btn.click(
|
| 212 |
check_password,
|
| 213 |
inputs=password_input,
|
| 214 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
)
|
| 216 |
|
| 217 |
-
gr.HTML("<div id='ohamlab-footer'>© OhamLab
|
| 218 |
-
|
| 219 |
|
| 220 |
|
| 221 |
demo.launch()
|
|
|
|
| 1 |
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
|
|
|
|
| 16 |
|
| 17 |
def quota_guard(fn):
|
| 18 |
def wrapper(*args, request: gr.Request = None, **kwargs):
|
|
|
|
| 19 |
session = request.session_hash if request else "global"
|
| 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
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
usage[session] = (count + 1, start)
|
|
|
|
|
|
|
| 36 |
return fn(*args, request=request, **kwargs)
|
| 37 |
|
| 38 |
return wrapper
|
|
|
|
| 52 |
raise RuntimeError("APP_PASSWORD environment variable not set")
|
| 53 |
|
| 54 |
|
| 55 |
+
# ======================================================
|
| 56 |
+
# YOUTUBE FREE PREVIEW
|
| 57 |
+
# ======================================================
|
| 58 |
+
YOUTUBE_LINKS = [
|
| 59 |
+
"https://www.youtube.com/watch?v=g30KR9VNXS0",
|
| 60 |
+
"https://www.youtube.com/watch?v=g30KR9VNXS0",
|
| 61 |
+
"https://www.youtube.com/watch?v=g30KR9VNXS0",
|
| 62 |
+
"https://www.youtube.com/watch?v=g30KR9VNXS0",
|
| 63 |
+
"https://www.youtube.com/watch?v=g30KR9VNXS0",
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def render_free_videos():
|
| 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 |
+
return f"""
|
| 84 |
+
<h3>🎬 Free Preview</h3>
|
| 85 |
+
<div class="free-grid">
|
| 86 |
+
{''.join(cards)}
|
| 87 |
+
</div>
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
|
| 91 |
# ======================================================
|
| 92 |
# LOAD DATASET
|
| 93 |
# ======================================================
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
# ======================================================
|
| 100 |
+
# PAGINATION
|
| 101 |
# ======================================================
|
| 102 |
def render_grid(page, page_size):
|
| 103 |
page = int(page)
|
|
|
|
| 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: (page + 1) * page_size]
|
|
|
|
|
|
|
| 111 |
|
| 112 |
cards = []
|
| 113 |
for _, row in batch.iterrows():
|
|
|
|
| 122 |
""")
|
| 123 |
|
| 124 |
html = f"""
|
| 125 |
+
<div class="grid">{''.join(cards)}</div>
|
| 126 |
+
<div class="page-info">Page {page + 1} / {total_pages}</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
"""
|
| 128 |
|
| 129 |
return html, page
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
# ======================================================
|
| 145 |
+
# AUTH
|
| 146 |
# ======================================================
|
| 147 |
@quota_guard
|
| 148 |
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 box
|
| 153 |
+
gr.update(visible=False), # free preview
|
| 154 |
+
gr.update(visible=True), # app content
|
| 155 |
html,
|
| 156 |
page,
|
| 157 |
"✅ Access granted"
|
| 158 |
)
|
| 159 |
else:
|
| 160 |
return (
|
| 161 |
+
gr.update(visible=True),
|
| 162 |
gr.update(visible=True),
|
| 163 |
gr.update(visible=False),
|
| 164 |
"",
|
|
|
|
| 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 |
}
|
| 208 |
|
|
|
|
| 209 |
#ohamlab-footer {
|
| 210 |
position: fixed;
|
| 211 |
bottom: 0;
|
|
|
|
| 212 |
width: 100%;
|
| 213 |
text-align: center;
|
| 214 |
+
padding: 8px;
|
| 215 |
font-size: 12px;
|
| 216 |
background: #f8f9fb;
|
|
|
|
|
|
|
| 217 |
border-top: 1px solid #e5e7eb;
|
| 218 |
}
|
| 219 |
"""
|
|
|
|
| 223 |
# UI
|
| 224 |
# ======================================================
|
| 225 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 226 |
+
gr.Markdown("# 🔐 OhamLab Video Showcase")
|
| 227 |
|
| 228 |
+
# -------- LOGIN + FREE PREVIEW --------
|
| 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 |
+
free_html = gr.HTML(render_free_videos())
|
| 236 |
+
|
| 237 |
+
# -------- PRO CONTENT --------
|
| 238 |
with gr.Column(visible=False) as app_content:
|
| 239 |
with gr.Row():
|
| 240 |
page_size = gr.Dropdown([1, 5, 10], value=5)
|
|
|
|
| 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 |
+
# -------- EVENTS --------
|
| 254 |
login_btn.click(
|
| 255 |
check_password,
|
| 256 |
inputs=password_input,
|
| 257 |
+
outputs=[
|
| 258 |
+
login_box,
|
| 259 |
+
free_preview,
|
| 260 |
+
app_content,
|
| 261 |
+
gallery,
|
| 262 |
+
page_state,
|
| 263 |
+
login_status,
|
| 264 |
+
],
|
| 265 |
)
|
| 266 |
|
| 267 |
+
gr.HTML("<div id='ohamlab-footer'>© OhamLab</div>")
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
demo.launch()
|