Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +59 -49
streamlit_app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
|
| 4 |
"""
|
| 5 |
-
Video‑analysis Streamlit app (refactored
|
| 6 |
"""
|
| 7 |
|
| 8 |
# ----------------------------------------------------------------------
|
|
@@ -210,10 +210,11 @@ def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
|
|
| 210 |
# UI helpers
|
| 211 |
# ----------------------------------------------------------------------
|
| 212 |
def _expand_sidebar(width: int = 380) -> None:
|
|
|
|
| 213 |
st.markdown(
|
| 214 |
f"""
|
| 215 |
<style>
|
| 216 |
-
.css-1d391kg {{ /* may
|
| 217 |
width: {width}px !important;
|
| 218 |
min-width: {width}px !important;
|
| 219 |
}}
|
|
@@ -234,6 +235,19 @@ def main() -> None:
|
|
| 234 |
st.sidebar.header("Video Input")
|
| 235 |
st.sidebar.text_input("Video URL", key="url", placeholder="https://")
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
if st.sidebar.button("Load Video"):
|
| 238 |
try:
|
| 239 |
with st.spinner("Downloading video…"):
|
|
@@ -241,12 +255,11 @@ def main() -> None:
|
|
| 241 |
st.session_state["url"], DATA_DIR, st.session_state["video_password"]
|
| 242 |
)
|
| 243 |
mp4_path = _convert_to_mp4(Path(raw_path))
|
| 244 |
-
# Optional compression
|
| 245 |
mp4_path, _ = _maybe_compress(mp4_path, st.session_state["compress_mb"])
|
| 246 |
st.session_state["video_path"] = str(mp4_path)
|
| 247 |
st.session_state["last_error"] = ""
|
| 248 |
st.toast("Video ready")
|
| 249 |
-
st.experimental_rerun()
|
| 250 |
except Exception as e:
|
| 251 |
st.session_state["last_error"] = f"Download failed: {e}"
|
| 252 |
st.sidebar.error(st.session_state["last_error"])
|
|
@@ -260,11 +273,16 @@ def main() -> None:
|
|
| 260 |
model = st.text_input("Custom model ID", value=DEFAULT_MODEL, key="custom_model")
|
| 261 |
st.session_state["model_input"] = model
|
| 262 |
|
| 263 |
-
# API key handling
|
| 264 |
secret_key = os.getenv("GOOGLE_API_KEY", "")
|
| 265 |
if secret_key:
|
| 266 |
st.session_state["api_key"] = secret_key
|
| 267 |
-
st.text_input(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
st.text_area(
|
| 270 |
"Analysis prompt",
|
|
@@ -286,17 +304,29 @@ def main() -> None:
|
|
| 286 |
key="compress_mb",
|
| 287 |
)
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
try:
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
# ---------- Main panel ----------
|
| 302 |
if st.session_state["last_error"]:
|
|
@@ -305,39 +335,16 @@ def main() -> None:
|
|
| 305 |
if st.session_state.get("video_path"):
|
| 306 |
st.video(st.session_state["video_path"])
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
)
|
| 319 |
-
cleaned = _strip_prompt_echo(st.session_state["prompt"], raw)
|
| 320 |
-
st.session_state["analysis_out"] = cleaned
|
| 321 |
-
st.session_state["raw_output"] = raw
|
| 322 |
-
except Exception as e:
|
| 323 |
-
st.session_state["last_error"] = f"Analysis failed: {e}"
|
| 324 |
-
st.session_state["last_error_detail"] = traceback.format_exc()
|
| 325 |
-
finally:
|
| 326 |
-
st.session_state["busy"] = False
|
| 327 |
-
st.experimental_rerun()
|
| 328 |
-
|
| 329 |
-
# Show results
|
| 330 |
-
if st.session_state.get("analysis_out"):
|
| 331 |
-
st.subheader("📝 Analysis")
|
| 332 |
-
st.write(st.session_state["analysis_out"])
|
| 333 |
-
|
| 334 |
-
with st.expander("Show raw model output"):
|
| 335 |
-
st.code(st.session_state["raw_output"], language="text")
|
| 336 |
-
|
| 337 |
-
# Optional error details
|
| 338 |
-
if st.session_state.get("last_error_detail"):
|
| 339 |
-
with st.expander("Show error details"):
|
| 340 |
-
st.code(st.session_state["last_error_detail"], language="text")
|
| 341 |
|
| 342 |
|
| 343 |
# ----------------------------------------------------------------------
|
|
@@ -367,6 +374,9 @@ def _init_state() -> None:
|
|
| 367 |
# ----------------------------------------------------------------------
|
| 368 |
if __name__ == "__main__":
|
| 369 |
_init_state()
|
|
|
|
|
|
|
| 370 |
if st.session_state["api_key"]:
|
| 371 |
genai.configure(api_key=st.session_state["api_key"])
|
|
|
|
| 372 |
main()
|
|
|
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
|
| 4 |
"""
|
| 5 |
+
Video‑analysis Streamlit app (refactored, sidebar‑based controls, no experimental_rerun).
|
| 6 |
"""
|
| 7 |
|
| 8 |
# ----------------------------------------------------------------------
|
|
|
|
| 210 |
# UI helpers
|
| 211 |
# ----------------------------------------------------------------------
|
| 212 |
def _expand_sidebar(width: int = 380) -> None:
|
| 213 |
+
"""Make the Streamlit sidebar a bit wider."""
|
| 214 |
st.markdown(
|
| 215 |
f"""
|
| 216 |
<style>
|
| 217 |
+
.css-1d391kg {{ /* class name may change with Streamlit updates */
|
| 218 |
width: {width}px !important;
|
| 219 |
min-width: {width}px !important;
|
| 220 |
}}
|
|
|
|
| 235 |
st.sidebar.header("Video Input")
|
| 236 |
st.sidebar.text_input("Video URL", key="url", placeholder="https://")
|
| 237 |
|
| 238 |
+
# Clear cached videos
|
| 239 |
+
if st.sidebar.button("Clear Video"):
|
| 240 |
+
for f in DATA_DIR.iterdir():
|
| 241 |
+
try:
|
| 242 |
+
f.unlink()
|
| 243 |
+
except Exception:
|
| 244 |
+
pass
|
| 245 |
+
st.session_state["video_path"] = ""
|
| 246 |
+
st.session_state["analysis_out"] = ""
|
| 247 |
+
st.session_state["raw_output"] = ""
|
| 248 |
+
st.toast("All cached videos cleared")
|
| 249 |
+
|
| 250 |
+
# Load video button
|
| 251 |
if st.sidebar.button("Load Video"):
|
| 252 |
try:
|
| 253 |
with st.spinner("Downloading video…"):
|
|
|
|
| 255 |
st.session_state["url"], DATA_DIR, st.session_state["video_password"]
|
| 256 |
)
|
| 257 |
mp4_path = _convert_to_mp4(Path(raw_path))
|
| 258 |
+
# Optional compression based on user‑defined limit
|
| 259 |
mp4_path, _ = _maybe_compress(mp4_path, st.session_state["compress_mb"])
|
| 260 |
st.session_state["video_path"] = str(mp4_path)
|
| 261 |
st.session_state["last_error"] = ""
|
| 262 |
st.toast("Video ready")
|
|
|
|
| 263 |
except Exception as e:
|
| 264 |
st.session_state["last_error"] = f"Download failed: {e}"
|
| 265 |
st.sidebar.error(st.session_state["last_error"])
|
|
|
|
| 273 |
model = st.text_input("Custom model ID", value=DEFAULT_MODEL, key="custom_model")
|
| 274 |
st.session_state["model_input"] = model
|
| 275 |
|
| 276 |
+
# API key handling – can be set via env var or entered here
|
| 277 |
secret_key = os.getenv("GOOGLE_API_KEY", "")
|
| 278 |
if secret_key:
|
| 279 |
st.session_state["api_key"] = secret_key
|
| 280 |
+
st.text_input(
|
| 281 |
+
"Google API Key",
|
| 282 |
+
key="api_key",
|
| 283 |
+
type="password",
|
| 284 |
+
help="Enter your Gemini API key (or set GOOGLE_API_KEY env var).",
|
| 285 |
+
)
|
| 286 |
|
| 287 |
st.text_area(
|
| 288 |
"Analysis prompt",
|
|
|
|
| 304 |
key="compress_mb",
|
| 305 |
)
|
| 306 |
|
| 307 |
+
# Run Analysis button – now in the sidebar, under Clear Video
|
| 308 |
+
if st.sidebar.button("Run Analysis"):
|
| 309 |
+
if not st.session_state.get("video_path"):
|
| 310 |
+
st.sidebar.error("No video loaded – load a video first.")
|
| 311 |
+
else:
|
| 312 |
+
st.session_state["busy"] = True
|
| 313 |
+
st.session_state["analysis_out"] = ""
|
| 314 |
+
st.session_state["raw_output"] = ""
|
| 315 |
try:
|
| 316 |
+
with st.spinner("Generating report…"):
|
| 317 |
+
raw = generate_report(
|
| 318 |
+
Path(st.session_state["video_path"]),
|
| 319 |
+
st.session_state["prompt"],
|
| 320 |
+
st.session_state["model_input"],
|
| 321 |
+
)
|
| 322 |
+
cleaned = _strip_prompt_echo(st.session_state["prompt"], raw)
|
| 323 |
+
st.session_state["analysis_out"] = cleaned
|
| 324 |
+
st.session_state["raw_output"] = raw
|
| 325 |
+
except Exception as e:
|
| 326 |
+
st.session_state["last_error"] = f"Analysis failed: {e}"
|
| 327 |
+
st.session_state["last_error_detail"] = traceback.format_exc()
|
| 328 |
+
finally:
|
| 329 |
+
st.session_state["busy"] = False
|
| 330 |
|
| 331 |
# ---------- Main panel ----------
|
| 332 |
if st.session_state["last_error"]:
|
|
|
|
| 335 |
if st.session_state.get("video_path"):
|
| 336 |
st.video(st.session_state["video_path"])
|
| 337 |
|
| 338 |
+
if st.session_state.get("analysis_out"):
|
| 339 |
+
st.subheader("📝 Analysis")
|
| 340 |
+
st.write(st.session_state["analysis_out"])
|
| 341 |
+
|
| 342 |
+
with st.expander("Show raw model output"):
|
| 343 |
+
st.code(st.session_state["raw_output"], language="text")
|
| 344 |
+
|
| 345 |
+
if st.session_state.get("last_error_detail"):
|
| 346 |
+
with st.expander("Show error details"):
|
| 347 |
+
st.code(st.session_state["last_error_detail"], language="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
|
| 350 |
# ----------------------------------------------------------------------
|
|
|
|
| 374 |
# ----------------------------------------------------------------------
|
| 375 |
if __name__ == "__main__":
|
| 376 |
_init_state()
|
| 377 |
+
|
| 378 |
+
# Configure Gemini if an API key is present (env var or sidebar entry)
|
| 379 |
if st.session_state["api_key"]:
|
| 380 |
genai.configure(api_key=st.session_state["api_key"])
|
| 381 |
+
|
| 382 |
main()
|