Update app.py
Browse files
app.py
CHANGED
|
@@ -1,230 +1,175 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import urllib.parse
|
| 3 |
-
from urllib.parse import quote
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
DEFAULT_DIR = "/bucket_data"
|
| 7 |
|
| 8 |
# ----------------------------
|
| 9 |
-
# SCANNER (UNCHANGED
|
| 10 |
# ----------------------------
|
| 11 |
def load_live_cctv_dashboard():
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
os.path.splitext(f)[0]
|
| 23 |
-
for f in files
|
| 24 |
-
if f.lower().endswith(video_exts)
|
| 25 |
-
}
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
lower = filename.lower()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
continue
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"name": filename,
|
| 48 |
-
"video_url": encoded
|
| 49 |
})
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
"name": filename,
|
| 54 |
-
"image_url": encoded
|
| 55 |
-
})
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
# ----------------------------
|
| 62 |
-
#
|
| 63 |
# ----------------------------
|
| 64 |
custom_js = """
|
| 65 |
-
function
|
| 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 |
-
// CONTENT LAYER (icons/text BELOW thumbnail)
|
| 95 |
-
placeholder.querySelectorAll("span").forEach(el => {
|
| 96 |
-
el.style.position = "relative";
|
| 97 |
-
el.style.zIndex = "2";
|
| 98 |
-
});
|
| 99 |
|
| 100 |
-
|
| 101 |
-
let frame = 1;
|
| 102 |
-
function setFrame(i){
|
| 103 |
-
img.src = `${base}/thumbnail${i}.webp`;
|
| 104 |
-
}
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
setFrame(frame);
|
| 111 |
-
}, 2000);
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if (el.dataset.interval) clearInterval(el.dataset.interval);
|
| 124 |
-
el.dataset.thumbInit = "0";
|
| 125 |
-
}
|
| 126 |
-
});
|
| 127 |
-
}, { rootMargin: "200px", threshold: 0.1 });
|
| 128 |
|
| 129 |
-
|
| 130 |
-
document.querySelectorAll(".video-placeholder").forEach(el => {
|
| 131 |
-
observer.observe(el);
|
| 132 |
-
});
|
| 133 |
-
}
|
| 134 |
|
| 135 |
-
if (document.readyState === "loading")
|
| 136 |
document.addEventListener("DOMContentLoaded", scan);
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
"""
|
| 141 |
|
| 142 |
|
| 143 |
# ----------------------------
|
| 144 |
-
#
|
| 145 |
# ----------------------------
|
| 146 |
-
with gr.Blocks(title="CCTV
|
| 147 |
|
| 148 |
-
|
| 149 |
|
| 150 |
-
@gr.render(inputs=
|
| 151 |
-
def
|
|
|
|
| 152 |
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
html = []
|
| 156 |
|
| 157 |
-
for folder, media in
|
| 158 |
|
| 159 |
-
html.append(f"<h2
|
| 160 |
-
|
| 161 |
-
# ================= VIDEO GRID (3 PER ROW FIX) =================
|
| 162 |
-
if media["videos"]:
|
| 163 |
-
html.append("""
|
| 164 |
-
<div style="
|
| 165 |
-
display:grid;
|
| 166 |
-
grid-template-columns:repeat(3, 1fr);
|
| 167 |
-
gap:16px;
|
| 168 |
-
margin-bottom:30px;
|
| 169 |
-
">
|
| 170 |
-
""")
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
overflow:hidden;
|
| 184 |
-
height:260px;
|
| 185 |
position:relative;
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
width:100%;
|
| 194 |
-
height:100%;
|
| 195 |
-
position:relative;
|
| 196 |
-
cursor:pointer;
|
| 197 |
-
background:#111827;
|
| 198 |
-
display:flex;
|
| 199 |
-
align-items:center;
|
| 200 |
-
justify-content:center;
|
| 201 |
-
color:white;
|
| 202 |
-
">
|
| 203 |
-
|
| 204 |
-
<!-- BACKGROUND ICON (BEHIND THUMB) -->
|
| 205 |
-
<span style="font-size:2rem; opacity:0.5;">▶</span>
|
| 206 |
-
<span style="font-size:12px; margin-top:30px;">Hover preview</span>
|
| 207 |
-
|
| 208 |
-
</div>
|
| 209 |
-
</div>
|
| 210 |
-
""")
|
| 211 |
-
|
| 212 |
-
html.append("</div>")
|
| 213 |
-
|
| 214 |
-
# ================= IMAGES =================
|
| 215 |
-
if media["images"]:
|
| 216 |
-
html.append("<div style='display:grid;grid-template-columns:repeat(auto-fill,120px);gap:10px;'>")
|
| 217 |
-
|
| 218 |
-
for img in media["images"]:
|
| 219 |
-
url = f"https://kphfrepo-webdav.hf.space/{img['image_url']}"
|
| 220 |
-
html.append(f"""
|
| 221 |
-
<img src="{url}" style="width:120px;height:100px;object-fit:cover;border-radius:6px;" />
|
| 222 |
-
""")
|
| 223 |
-
|
| 224 |
-
html.append("</div>")
|
| 225 |
|
| 226 |
gr.HTML("".join(html))
|
| 227 |
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
-
demo.launch(
|
|
|
|
| 1 |
import os
|
| 2 |
+
import glob
|
| 3 |
import urllib.parse
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
DEFAULT_DIR = "/bucket_data"
|
| 7 |
|
| 8 |
# ----------------------------
|
| 9 |
+
# BACKEND SCANNER (UNCHANGED)
|
| 10 |
# ----------------------------
|
| 11 |
def load_live_cctv_dashboard():
|
| 12 |
+
try:
|
| 13 |
+
print("\n⚡ Rocket-Loading local filesystem assets in a single chunk...")
|
| 14 |
+
search_path = os.path.join(DEFAULT_DIR, "**", "*")
|
| 15 |
+
files = glob.glob(search_path, recursive=True)
|
| 16 |
|
| 17 |
+
image_exts = ('.png', '.jpg', '.jpeg', '.webp', '.gif')
|
| 18 |
+
video_exts = ('.mp4', '.mkv', '.avi', '.mov', '.dav', '.ts')
|
| 19 |
|
| 20 |
+
grouped_assets = {}
|
| 21 |
+
video_count = 0
|
| 22 |
+
image_count = 0
|
| 23 |
|
| 24 |
+
for f in files:
|
| 25 |
+
if os.path.isdir(f) or ".gitattributes" in f or f.lower().endswith('.7z'):
|
| 26 |
+
continue
|
| 27 |
|
| 28 |
+
relative_path = f.replace(f"{DEFAULT_DIR}/", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
folder_name = relative_path.split("/")[0] if "/" in relative_path else "Root Directory"
|
| 31 |
|
| 32 |
+
grouped_assets.setdefault(folder_name, {"images": [], "videos": []})
|
|
|
|
| 33 |
|
| 34 |
+
encoded_path = urllib.parse.quote(os.path.abspath(f))
|
|
|
|
| 35 |
|
| 36 |
+
if f.lower().endswith(video_exts):
|
| 37 |
+
video_count += 1
|
| 38 |
+
print("added video:", encoded_path)
|
| 39 |
|
| 40 |
+
grouped_assets[folder_name]["videos"].append({
|
| 41 |
+
"name": os.path.basename(f),
|
| 42 |
+
"video_url": encoded_path
|
| 43 |
+
})
|
| 44 |
|
| 45 |
+
elif f.lower().endswith(image_exts):
|
| 46 |
+
image_count += 1
|
| 47 |
|
| 48 |
+
grouped_assets[folder_name]["images"].append({
|
| 49 |
+
"name": os.path.basename(f),
|
| 50 |
+
"image_url": encoded_path
|
|
|
|
|
|
|
| 51 |
})
|
| 52 |
|
| 53 |
+
print(f"✅ Finished: {video_count} videos, {image_count} images")
|
| 54 |
+
return grouped_assets
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print("❌ Scan error:", e)
|
| 58 |
+
return {}
|
| 59 |
|
| 60 |
|
| 61 |
# ----------------------------
|
| 62 |
+
# ONLY FIXED THUMB HOVER JS
|
| 63 |
# ----------------------------
|
| 64 |
custom_js = """
|
| 65 |
+
(function () {
|
| 66 |
+
|
| 67 |
+
function initThumb(el) {
|
| 68 |
+
if (!el || el.dataset.thumbInit === "1") return;
|
| 69 |
+
|
| 70 |
+
const base = el.dataset.thumbBase;
|
| 71 |
+
if (!base) return;
|
| 72 |
+
|
| 73 |
+
el.dataset.thumbInit = "1";
|
| 74 |
+
|
| 75 |
+
let img = el.querySelector("img.thumb");
|
| 76 |
+
if (!img) {
|
| 77 |
+
img = document.createElement("img");
|
| 78 |
+
img.className = "thumb";
|
| 79 |
+
img.style.cssText = `
|
| 80 |
+
position:absolute;
|
| 81 |
+
inset:0;
|
| 82 |
+
width:100%;
|
| 83 |
+
height:100%;
|
| 84 |
+
object-fit:cover;
|
| 85 |
+
opacity:0;
|
| 86 |
+
transition:opacity 0.2s ease;
|
| 87 |
+
pointer-events:none;
|
| 88 |
+
`;
|
| 89 |
+
el.style.position = "relative";
|
| 90 |
+
el.appendChild(img);
|
| 91 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
img.src = `${base}/thumbnail1.webp`;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
let i = 1;
|
| 96 |
+
let interval = null;
|
| 97 |
|
| 98 |
+
el.addEventListener("mouseenter", () => {
|
| 99 |
+
if (interval) return;
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
interval = setInterval(() => {
|
| 102 |
+
i = (i % 5) + 1;
|
| 103 |
+
img.src = `${base}/thumbnail${i}.webp`;
|
| 104 |
+
img.style.opacity = 1;
|
| 105 |
+
}, 900);
|
| 106 |
+
});
|
| 107 |
|
| 108 |
+
el.addEventListener("mouseleave", () => {
|
| 109 |
+
clearInterval(interval);
|
| 110 |
+
interval = null;
|
| 111 |
+
img.style.opacity = 0;
|
| 112 |
+
});
|
| 113 |
+
}
|
| 114 |
|
| 115 |
+
function scan() {
|
| 116 |
+
document.querySelectorAll(".video-placeholder").forEach(initThumb);
|
| 117 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
const observer = new MutationObserver(scan);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
| 121 |
document.addEventListener("DOMContentLoaded", scan);
|
| 122 |
+
observer.observe(document.body, { childList: true, subtree: true });
|
| 123 |
|
| 124 |
+
})();
|
| 125 |
"""
|
| 126 |
|
| 127 |
|
| 128 |
# ----------------------------
|
| 129 |
+
# UI (UNCHANGED EXCEPT DATA ATTR ADDED)
|
| 130 |
# ----------------------------
|
| 131 |
+
with gr.Blocks(title="CCTV Explorer", js=custom_js) as demo:
|
| 132 |
|
| 133 |
+
refresh_btn = gr.Button("🔄 Load")
|
| 134 |
|
| 135 |
+
@gr.render(inputs=refresh_btn, triggers=[demo.load, refresh_btn.click])
|
| 136 |
+
def render_ui():
|
| 137 |
+
assets = load_live_cctv_dashboard()
|
| 138 |
|
| 139 |
+
if not assets:
|
| 140 |
+
gr.Markdown("No data")
|
| 141 |
+
return
|
| 142 |
|
| 143 |
html = []
|
| 144 |
|
| 145 |
+
for folder, media in assets.items():
|
| 146 |
|
| 147 |
+
html.append(f"<h2>📁 {folder}</h2>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
for v in media["videos"]:
|
| 150 |
+
base = os.path.splitext(v["video_url"])[0]
|
| 151 |
+
base_url = f"https://kphfrepo-webdav.hf.space/{base.lstrip('/')}"
|
| 152 |
|
| 153 |
+
html.append(f"""
|
| 154 |
+
<div class="video-placeholder"
|
| 155 |
+
data-thumb-base="{base_url}"
|
| 156 |
+
style="
|
| 157 |
+
width:320px;
|
| 158 |
+
height:180px;
|
| 159 |
+
background:#111;
|
| 160 |
+
margin:10px;
|
| 161 |
+
display:inline-block;
|
|
|
|
|
|
|
| 162 |
position:relative;
|
| 163 |
+
overflow:hidden;
|
| 164 |
+
border-radius:8px;
|
| 165 |
+
cursor:pointer;
|
| 166 |
+
">
|
| 167 |
+
<span style="color:white;position:absolute;top:45%;left:45%;">▶</span>
|
| 168 |
+
</div>
|
| 169 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
gr.HTML("".join(html))
|
| 172 |
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
+
demo.launch()
|