Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +123 -125
streamlit_app.py
CHANGED
|
@@ -257,12 +257,59 @@ def main() -> None:
|
|
| 257 |
st.sidebar.header("Video Input")
|
| 258 |
st.sidebar.text_input("Video URL", key="url", placeholder="https://")
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
with st.sidebar.expander("Settings", expanded=False):
|
| 261 |
model = st.selectbox(
|
| 262 |
"Model", MODEL_OPTIONS, index=MODEL_OPTIONS.index(DEFAULT_MODEL)
|
| 263 |
)
|
| 264 |
if model == "custom":
|
| 265 |
-
model = st.text_input(
|
|
|
|
|
|
|
| 266 |
st.session_state["model_input"] = model
|
| 267 |
|
| 268 |
# API key handling
|
|
@@ -280,137 +327,88 @@ def main() -> None:
|
|
| 280 |
"Compress if > (MB)",
|
| 281 |
min_value=10,
|
| 282 |
max_value=2000,
|
| 283 |
-
value=st.session_state
|
| 284 |
step=10,
|
| 285 |
key="compress_mb",
|
| 286 |
)
|
| 287 |
|
| 288 |
-
# ----------
|
| 289 |
-
|
| 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 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
st.sidebar.video(str(mp4))
|
| 309 |
-
except Exception:
|
| 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": "",
|
| 323 |
-
"video_path": "",
|
| 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.")
|
| 351 |
-
else:
|
| 352 |
-
try:
|
| 353 |
-
st.session_state["busy"] = True
|
| 354 |
-
genai.configure(api_key=api_key)
|
| 355 |
-
|
| 356 |
-
# ---- optional compression ----
|
| 357 |
-
with st.spinner("Checking video size…"):
|
| 358 |
-
video_path, was_compressed = _maybe_compress(
|
| 359 |
-
Path(st.session_state["video_path"]),
|
| 360 |
-
st.session_state["compress_mb"],
|
| 361 |
-
)
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
)
|
| 371 |
-
|
| 372 |
-
st.session_state["
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
st.
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 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__":
|
|
|
|
| 257 |
st.sidebar.header("Video Input")
|
| 258 |
st.sidebar.text_input("Video URL", key="url", placeholder="https://")
|
| 259 |
|
| 260 |
+
# **Load Video button – now directly under the URL field**
|
| 261 |
+
if st.sidebar.button("Load Video"):
|
| 262 |
+
try:
|
| 263 |
+
with st.spinner("Downloading video…"):
|
| 264 |
+
raw_path = download_video(
|
| 265 |
+
st.session_state["url"], DATA_DIR, st.session_state["video_password"]
|
| 266 |
+
)
|
| 267 |
+
# ALWAYS convert to MP4 before storing
|
| 268 |
+
mp4_path = _convert_to_mp4(Path(raw_path))
|
| 269 |
+
st.session_state["video_path"] = str(mp4_path) # guaranteed MP4
|
| 270 |
+
st.session_state["last_error"] = ""
|
| 271 |
+
st.success("Video loaded successfully.")
|
| 272 |
+
except Exception as e:
|
| 273 |
+
st.session_state["last_error"] = f"Download failed: {e}"
|
| 274 |
+
st.sidebar.error(st.session_state["last_error"])
|
| 275 |
+
|
| 276 |
+
# ---------- Preview & clear ----------
|
| 277 |
+
if st.session_state.get("video_path"):
|
| 278 |
+
try:
|
| 279 |
+
mp4 = _convert_to_mp4(Path(st.session_state["video_path"]))
|
| 280 |
+
st.sidebar.video(str(mp4))
|
| 281 |
+
except Exception:
|
| 282 |
+
st.sidebar.write("Preview unavailable")
|
| 283 |
+
|
| 284 |
+
if st.sidebar.button("Clear Video"):
|
| 285 |
+
# delete every file in DATA_DIR (both raw and converted)
|
| 286 |
+
for f in DATA_DIR.iterdir():
|
| 287 |
+
try:
|
| 288 |
+
f.unlink()
|
| 289 |
+
except Exception:
|
| 290 |
+
pass
|
| 291 |
+
# reset session state, including the URL field
|
| 292 |
+
st.session_state.update(
|
| 293 |
+
{
|
| 294 |
+
"url": "",
|
| 295 |
+
"video_path": "",
|
| 296 |
+
"analysis_out": "",
|
| 297 |
+
"last_error": "",
|
| 298 |
+
"busy": False,
|
| 299 |
+
"show_raw_on_error": False,
|
| 300 |
+
}
|
| 301 |
+
)
|
| 302 |
+
st.success("Session cleared.")
|
| 303 |
+
|
| 304 |
+
# ---------- Settings ----------
|
| 305 |
with st.sidebar.expander("Settings", expanded=False):
|
| 306 |
model = st.selectbox(
|
| 307 |
"Model", MODEL_OPTIONS, index=MODEL_OPTIONS.index(DEFAULT_MODEL)
|
| 308 |
)
|
| 309 |
if model == "custom":
|
| 310 |
+
model = st.text_input(
|
| 311 |
+
"Custom model ID", value=DEFAULT_MODEL, key="custom_model"
|
| 312 |
+
)
|
| 313 |
st.session_state["model_input"] = model
|
| 314 |
|
| 315 |
# API key handling
|
|
|
|
| 327 |
"Compress if > (MB)",
|
| 328 |
min_value=10,
|
| 329 |
max_value=2000,
|
| 330 |
+
value=st.session_state.get("compress_mb", 100),
|
| 331 |
step=10,
|
| 332 |
key="compress_mb",
|
| 333 |
)
|
| 334 |
|
| 335 |
+
# ---------- Generation ----------
|
| 336 |
+
col1, col2 = st.columns([1, 3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
+
with col1:
|
| 339 |
+
generate_now = st.button(
|
| 340 |
+
"Generate analysis", type="primary", disabled=st.session_state.get("busy", False)
|
| 341 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
with col2:
|
| 344 |
+
if not st.session_state.get("video_path"):
|
| 345 |
+
st.info("Load a video first.", icon="ℹ️")
|
| 346 |
+
|
| 347 |
+
if generate_now and not st.session_state.get("busy", False):
|
| 348 |
+
api_key = st.session_state.get("api_key") or os.getenv("GOOGLE_API_KEY")
|
| 349 |
+
if not st.session_state.get("video_path"):
|
| 350 |
+
st.error("No video loaded.")
|
| 351 |
+
elif not api_key:
|
| 352 |
+
st.error("Google API key missing.")
|
| 353 |
+
else:
|
| 354 |
+
try:
|
| 355 |
+
st.session_state["busy"] = True
|
| 356 |
+
genai.configure(api_key=api_key)
|
| 357 |
+
|
| 358 |
+
# ---- optional compression ----
|
| 359 |
+
with st.spinner("Checking video size…"):
|
| 360 |
+
video_path, was_compressed = _maybe_compress(
|
| 361 |
+
Path(st.session_state["video_path"]),
|
| 362 |
+
st.session_state["compress_mb"],
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
# ---- generation ----
|
| 366 |
+
with st.spinner("Generating analysis…"):
|
| 367 |
+
raw_out = generate_report(
|
| 368 |
+
video_path,
|
| 369 |
+
st.session_state["prompt"],
|
| 370 |
+
st.session_state["model_input"],
|
| 371 |
+
st.session_state.get("generation_timeout", 300),
|
| 372 |
+
)
|
| 373 |
+
st.session_state["raw_output"] = raw_out
|
| 374 |
+
|
| 375 |
+
# clean up temporary compressed file
|
| 376 |
+
if was_compressed:
|
| 377 |
+
try:
|
| 378 |
+
video_path.unlink()
|
| 379 |
+
except OSError:
|
| 380 |
+
pass
|
| 381 |
+
|
| 382 |
+
out = _strip_prompt_echo(st.session_state["prompt"], raw_out)
|
| 383 |
+
st.session_state["analysis_out"] = out
|
| 384 |
+
st.success("Analysis generated.")
|
| 385 |
+
st.markdown(out or "*(no output)*")
|
| 386 |
+
|
| 387 |
+
except Exception as exc:
|
| 388 |
+
tb = traceback.format_exc()
|
| 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 |
+
st.session_state["show_raw_on_error"] = True
|
| 394 |
+
st.error("An error occurred during generation.")
|
| 395 |
+
finally:
|
| 396 |
+
st.session_state["busy"] = False
|
| 397 |
+
|
| 398 |
+
# ---------- Results ----------
|
| 399 |
+
if st.session_state.get("analysis_out"):
|
| 400 |
+
st.subheader("📝 Analysis")
|
| 401 |
+
st.markdown(st.session_state["analysis_out"])
|
| 402 |
+
|
| 403 |
+
# Show raw Gemini output only when it exists
|
| 404 |
+
if st.session_state.get("raw_output"):
|
| 405 |
+
st.subheader("🔎 Full Gemini output")
|
| 406 |
+
st.code(st.session_state["raw_output"], language="text")
|
| 407 |
+
|
| 408 |
+
# ---------- Errors ----------
|
| 409 |
+
if st.session_state.get("last_error"):
|
| 410 |
+
with st.expander("❗️ Error details"):
|
| 411 |
+
st.code(st.session_state["last_error_detail"], language="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
|
| 414 |
if __name__ == "__main__":
|