Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +44 -34
streamlit_app.py
CHANGED
|
@@ -285,22 +285,24 @@ def main() -> None:
|
|
| 285 |
key="compress_mb",
|
| 286 |
)
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
| 301 |
|
| 302 |
# ---------- Preview & clear ----------
|
| 303 |
-
if st.session_state
|
| 304 |
try:
|
| 305 |
mp4 = _convert_to_mp4(Path(st.session_state["video_path"]))
|
| 306 |
st.sidebar.video(str(mp4))
|
|
@@ -308,13 +310,13 @@ def main() -> None:
|
|
| 308 |
st.sidebar.write("Preview unavailable")
|
| 309 |
|
| 310 |
if st.sidebar.button("Clear Video"):
|
| 311 |
-
# delete
|
| 312 |
for f in DATA_DIR.iterdir():
|
| 313 |
try:
|
| 314 |
f.unlink()
|
| 315 |
except Exception:
|
| 316 |
pass
|
| 317 |
-
# reset state, including URL field
|
| 318 |
st.session_state.update(
|
| 319 |
{
|
| 320 |
"url": "",
|
|
@@ -322,23 +324,27 @@ def main() -> None:
|
|
| 322 |
"analysis_out": "",
|
| 323 |
"last_error": "",
|
| 324 |
"busy": False,
|
|
|
|
| 325 |
}
|
| 326 |
)
|
| 327 |
st.success("Session cleared.")
|
| 328 |
|
|
|
|
| 329 |
# ---------- Generation ----------
|
| 330 |
col1, col2 = st.columns([1, 3])
|
|
|
|
| 331 |
with col1:
|
| 332 |
generate_now = st.button(
|
| 333 |
-
"Generate analysis", type="primary", disabled=st.session_state
|
| 334 |
)
|
|
|
|
| 335 |
with col2:
|
| 336 |
-
if not st.session_state
|
| 337 |
st.info("Load a video first.", icon="ℹ️")
|
| 338 |
|
| 339 |
-
if generate_now and not st.session_state
|
| 340 |
-
api_key = st.session_state
|
| 341 |
-
if not st.session_state
|
| 342 |
st.error("No video loaded.")
|
| 343 |
elif not api_key:
|
| 344 |
st.error("Google API key missing.")
|
|
@@ -376,31 +382,35 @@ if generate_now and not st.session_state["busy"]:
|
|
| 376 |
st.session_state["analysis_out"] = out
|
| 377 |
st.success("Analysis generated.")
|
| 378 |
st.markdown(out or "*(no output)*")
|
|
|
|
| 379 |
except Exception as exc:
|
| 380 |
tb = traceback.format_exc()
|
| 381 |
# keep both traceback and whatever raw output we might have
|
| 382 |
-
st.session_state["last_error_detail"] =
|
|
|
|
|
|
|
| 383 |
st.session_state["last_error"] = f"Generation error: {exc}"
|
| 384 |
# indicate that raw output should be shown in the main area
|
| 385 |
st.session_state["show_raw_on_error"] = True
|
| 386 |
st.error("An error occurred during generation.")
|
|
|
|
| 387 |
finally:
|
| 388 |
st.session_state["busy"] = False
|
| 389 |
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
|
| 405 |
|
| 406 |
if __name__ == "__main__":
|
|
|
|
| 285 |
key="compress_mb",
|
| 286 |
)
|
| 287 |
|
| 288 |
+
# ---------- Load video ----------
|
| 289 |
+
if st.sidebar.button("Load Video"):
|
| 290 |
+
try:
|
| 291 |
+
with st.spinner("Downloading video…"):
|
| 292 |
+
raw_path = download_video(
|
| 293 |
+
st.session_state["url"], DATA_DIR, st.session_state["video_password"]
|
| 294 |
+
)
|
| 295 |
+
# ALWAYS convert to MP4 before storing
|
| 296 |
+
mp4_path = _convert_to_mp4(Path(raw_path))
|
| 297 |
+
st.session_state["video_path"] = str(mp4_path) # guaranteed MP4
|
| 298 |
+
st.session_state["last_error"] = ""
|
| 299 |
+
st.success("Video loaded successfully.")
|
| 300 |
+
except Exception as e:
|
| 301 |
+
st.session_state["last_error"] = f"Download failed: {e}"
|
| 302 |
+
st.sidebar.error(st.session_state["last_error"])
|
| 303 |
|
| 304 |
# ---------- Preview & clear ----------
|
| 305 |
+
if st.session_state.get("video_path"):
|
| 306 |
try:
|
| 307 |
mp4 = _convert_to_mp4(Path(st.session_state["video_path"]))
|
| 308 |
st.sidebar.video(str(mp4))
|
|
|
|
| 310 |
st.sidebar.write("Preview unavailable")
|
| 311 |
|
| 312 |
if st.sidebar.button("Clear Video"):
|
| 313 |
+
# delete every file in DATA_DIR (both raw and converted)
|
| 314 |
for f in DATA_DIR.iterdir():
|
| 315 |
try:
|
| 316 |
f.unlink()
|
| 317 |
except Exception:
|
| 318 |
pass
|
| 319 |
+
# reset session state, including the URL field
|
| 320 |
st.session_state.update(
|
| 321 |
{
|
| 322 |
"url": "",
|
|
|
|
| 324 |
"analysis_out": "",
|
| 325 |
"last_error": "",
|
| 326 |
"busy": False,
|
| 327 |
+
"show_raw_on_error": False,
|
| 328 |
}
|
| 329 |
)
|
| 330 |
st.success("Session cleared.")
|
| 331 |
|
| 332 |
+
|
| 333 |
# ---------- Generation ----------
|
| 334 |
col1, col2 = st.columns([1, 3])
|
| 335 |
+
|
| 336 |
with col1:
|
| 337 |
generate_now = st.button(
|
| 338 |
+
"Generate analysis", type="primary", disabled=st.session_state.get("busy", False)
|
| 339 |
)
|
| 340 |
+
|
| 341 |
with col2:
|
| 342 |
+
if not st.session_state.get("video_path"):
|
| 343 |
st.info("Load a video first.", icon="ℹ️")
|
| 344 |
|
| 345 |
+
if generate_now and not st.session_state.get("busy", False):
|
| 346 |
+
api_key = st.session_state.get("api_key") or os.getenv("GOOGLE_API_KEY")
|
| 347 |
+
if not st.session_state.get("video_path"):
|
| 348 |
st.error("No video loaded.")
|
| 349 |
elif not api_key:
|
| 350 |
st.error("Google API key missing.")
|
|
|
|
| 382 |
st.session_state["analysis_out"] = out
|
| 383 |
st.success("Analysis generated.")
|
| 384 |
st.markdown(out or "*(no output)*")
|
| 385 |
+
|
| 386 |
except Exception as exc:
|
| 387 |
tb = traceback.format_exc()
|
| 388 |
# keep both traceback and whatever raw output we might have
|
| 389 |
+
st.session_state["last_error_detail"] = (
|
| 390 |
+
f"{tb}\n\nRaw Gemini output:\n{st.session_state.get('raw_output', '')}"
|
| 391 |
+
)
|
| 392 |
st.session_state["last_error"] = f"Generation error: {exc}"
|
| 393 |
# indicate that raw output should be shown in the main area
|
| 394 |
st.session_state["show_raw_on_error"] = True
|
| 395 |
st.error("An error occurred during generation.")
|
| 396 |
+
|
| 397 |
finally:
|
| 398 |
st.session_state["busy"] = False
|
| 399 |
|
| 400 |
+
# ---------- Results ----------
|
| 401 |
+
if st.session_state.get("analysis_out"):
|
| 402 |
+
st.subheader("📝 Analysis")
|
| 403 |
+
st.markdown(st.session_state["analysis_out"])
|
| 404 |
|
| 405 |
+
# Show raw Gemini output only when it exists
|
| 406 |
+
if st.session_state.get("raw_output"):
|
| 407 |
+
st.subheader("🔎 Full Gemini output")
|
| 408 |
+
st.code(st.session_state["raw_output"], language="text")
|
| 409 |
|
| 410 |
+
# ---------- Errors ----------
|
| 411 |
+
if st.session_state.get("last_error"):
|
| 412 |
+
with st.expander("❗️ Error details"):
|
| 413 |
+
st.code(st.session_state["last_error_detail"], language="text")
|
| 414 |
|
| 415 |
|
| 416 |
if __name__ == "__main__":
|