CB commited on
Commit
f29c207
·
verified ·
1 Parent(s): a45b7f7

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +27 -13
streamlit_app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import time
3
  import string
@@ -60,7 +61,8 @@ st.session_state.setdefault("loop_video", False)
60
  st.session_state.setdefault("uploaded_file", None)
61
  st.session_state.setdefault("processed_file", None)
62
  st.session_state.setdefault("busy", False)
63
- st.session_state.setdefault("last_loaded_path", "")
 
64
  st.session_state.setdefault("analysis_out", "")
65
  st.session_state.setdefault("last_error", "")
66
  st.session_state.setdefault("file_hash", None)
@@ -188,7 +190,7 @@ def clear_all_video_state():
188
  st.session_state.pop("uploaded_file", None)
189
  st.session_state.pop("processed_file", None)
190
  st.session_state["videos"] = ""
191
- st.session_state["last_loaded_path"] = ""
192
  st.session_state["analysis_out"] = ""
193
  st.session_state["last_error"] = ""
194
  st.session_state["file_hash"] = None
@@ -198,11 +200,13 @@ def clear_all_video_state():
198
  except Exception:
199
  pass
200
 
201
- # Reset when URL changes
202
  current_url = st.session_state.get("url", "")
203
- if current_url != st.session_state.get("last_loaded_path"):
204
- clear_all_video_state()
205
- st.session_state["last_loaded_path"] = current_url
 
 
206
 
207
  # ---- Sidebar UI ----
208
  st.sidebar.header("Video Input")
@@ -337,16 +341,25 @@ def compress_video_if_large(local_path: str, threshold_mb: int = 200):
337
  # ---- Simple layout ----
338
  col1, col2 = st.columns([1, 3])
339
  with col1:
340
- generate_now = st.button("Generate the story", type="primary", disabled=not bool(get_effective_api_key()))
 
 
 
 
341
  with col2:
342
- st.write("")
 
 
 
 
343
 
344
  if st.sidebar.button("Load Video", use_container_width=True):
345
  try:
346
  vpw = st.session_state.get("video-password", "")
347
  path = download_video_ytdlp(st.session_state.get("url", ""), str(DATA_DIR), vpw)
348
  st.session_state["videos"] = path
349
- st.session_state["last_loaded_path"] = path
 
350
  st.session_state.pop("uploaded_file", None)
351
  st.session_state.pop("processed_file", None)
352
  try:
@@ -389,7 +402,7 @@ if st.session_state["videos"]:
389
  except Exception:
390
  pass
391
 
392
- # ---- Generation flow (minimal) ----
393
  if generate_now and not st.session_state.get("busy"):
394
  if not st.session_state.get("videos"):
395
  st.error("No video loaded. Use 'Load Video' in the sidebar.")
@@ -421,7 +434,8 @@ if generate_now and not st.session_state.get("busy"):
421
  reupload_needed = True
422
  uploaded_file = st.session_state.get("uploaded_file")
423
  uploaded_name = file_name_or_id(uploaded_file)
424
- if processed and st.session_state.get("last_loaded_path") == current_path and st.session_state.get("file_hash") == current_hash and uploaded_name:
 
425
  reupload_needed = False
426
 
427
  if reupload_needed:
@@ -455,7 +469,7 @@ if generate_now and not st.session_state.get("busy"):
455
 
456
  st.session_state["uploaded_file"] = uploaded
457
  st.session_state["processed_file"] = processed
458
- st.session_state["last_loaded_path"] = current_path
459
  st.session_state["file_hash"] = current_hash
460
 
461
  prompt_text = (analysis_prompt.strip() or DEFAULT_PROMPT).strip()
@@ -598,7 +612,7 @@ if generate_now and not st.session_state.get("busy"):
598
  st.caption(f"Est. max tokens: {est_tokens}")
599
 
600
  except Exception as e:
601
- st.session_state["last_error"] = f"{e}"
602
  st.error("An error occurred while generating the story. You can try Generate again; the uploaded video will be reused.")
603
  finally:
604
  st.session_state["busy"] = False
 
1
+ # streamlit_app.py
2
  import os
3
  import time
4
  import string
 
61
  st.session_state.setdefault("uploaded_file", None)
62
  st.session_state.setdefault("processed_file", None)
63
  st.session_state.setdefault("busy", False)
64
+ st.session_state.setdefault("last_url", "")
65
+ st.session_state.setdefault("last_local_path", "")
66
  st.session_state.setdefault("analysis_out", "")
67
  st.session_state.setdefault("last_error", "")
68
  st.session_state.setdefault("file_hash", None)
 
190
  st.session_state.pop("uploaded_file", None)
191
  st.session_state.pop("processed_file", None)
192
  st.session_state["videos"] = ""
193
+ st.session_state["last_local_path"] = ""
194
  st.session_state["analysis_out"] = ""
195
  st.session_state["last_error"] = ""
196
  st.session_state["file_hash"] = None
 
200
  except Exception:
201
  pass
202
 
203
+ # Reset when URL changes (compare against last_url only)
204
  current_url = st.session_state.get("url", "")
205
+ if current_url != st.session_state.get("last_url"):
206
+ # avoid clearing on first load when last_url is empty
207
+ if st.session_state.get("last_url"):
208
+ clear_all_video_state()
209
+ st.session_state["last_url"] = current_url
210
 
211
  # ---- Sidebar UI ----
212
  st.sidebar.header("Video Input")
 
341
  # ---- Simple layout ----
342
  col1, col2 = st.columns([1, 3])
343
  with col1:
344
+ generate_now = st.button(
345
+ "Generate the story",
346
+ type="primary",
347
+ disabled=not (bool(get_effective_api_key()) and bool(st.session_state.get("videos")) and not st.session_state.get("busy"))
348
+ )
349
  with col2:
350
+ # small UX note column
351
+ if not st.session_state.get("videos"):
352
+ st.info("Load a video first (sidebar) to enable generation.", icon="ℹ️")
353
+ else:
354
+ st.write("")
355
 
356
  if st.sidebar.button("Load Video", use_container_width=True):
357
  try:
358
  vpw = st.session_state.get("video-password", "")
359
  path = download_video_ytdlp(st.session_state.get("url", ""), str(DATA_DIR), vpw)
360
  st.session_state["videos"] = path
361
+ st.session_state["last_local_path"] = path
362
+ # keep last_url intact — it tracks the input URL
363
  st.session_state.pop("uploaded_file", None)
364
  st.session_state.pop("processed_file", None)
365
  try:
 
402
  except Exception:
403
  pass
404
 
405
+ # ---- Generation flow (minimal, robust) ----
406
  if generate_now and not st.session_state.get("busy"):
407
  if not st.session_state.get("videos"):
408
  st.error("No video loaded. Use 'Load Video' in the sidebar.")
 
434
  reupload_needed = True
435
  uploaded_file = st.session_state.get("uploaded_file")
436
  uploaded_name = file_name_or_id(uploaded_file)
437
+ # Use last_local_path to determine if file changed
438
+ if processed and st.session_state.get("last_local_path") == current_path and st.session_state.get("file_hash") == current_hash and uploaded_name:
439
  reupload_needed = False
440
 
441
  if reupload_needed:
 
469
 
470
  st.session_state["uploaded_file"] = uploaded
471
  st.session_state["processed_file"] = processed
472
+ st.session_state["last_local_path"] = current_path
473
  st.session_state["file_hash"] = current_hash
474
 
475
  prompt_text = (analysis_prompt.strip() or DEFAULT_PROMPT).strip()
 
612
  st.caption(f"Est. max tokens: {est_tokens}")
613
 
614
  except Exception as e:
615
+ st.session_state["last_error"] = f"{e}\n{traceback.format_exc()}"
616
  st.error("An error occurred while generating the story. You can try Generate again; the uploaded video will be reused.")
617
  finally:
618
  st.session_state["busy"] = False