Hug0endob commited on
Commit
2adbfcc
·
verified ·
1 Parent(s): 0d53c9b

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +41 -32
streamlit_app.py CHANGED
@@ -224,11 +224,17 @@ def generate_report(video_path: Path, prompt: str, model_id: str, timeout: int =
224
  def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
225
  if not prompt or not text:
226
  return text
 
 
227
  clean_prompt = " ".join(prompt.lower().split())
228
- snippet = " ".join(text.lower().split()[:600])
229
- if SequenceMatcher(None, clean_prompt, snippet).ratio() > threshold:
230
- cut = max(len(clean_prompt), int(len(prompt) * 0.9))
231
- return text[cut:].lstrip(" \n:-")
 
 
 
 
232
  return text
233
 
234
  # ----------------------------------------------------------------------
@@ -360,34 +366,37 @@ def main() -> None:
360
  )
361
 
362
  # ---------- Main panel ----------
363
- # Run Analysis button
364
- if st.button("Run Analysis"):
365
- if not st.session_state.get("video_path"):
366
- st.error("No video loaded – load a video first.")
367
- elif not st.session_state.get("api_key"):
368
- st.error("Google API key missing – enter it in the sidebar.")
369
-
370
- st.session_state["busy"] = True
371
- st.session_state["analysis_out"] = ""
372
- st.session_state["raw_output"] = ""
373
- st.session_state["last_error"] = ""
374
- st.session_state["last_error_detail"] = ""
 
 
 
375
 
376
- try:
377
- with st.spinner("Generating report (this may take a minute)…"):
378
- raw = generate_report(
379
- Path(st.session_state["video_path"]),
380
- st.session_state["prompt"],
381
- st.session_state["model_input"],
382
- )
383
- cleaned = _strip_prompt_echo(st.session_state["prompt"], raw)
384
- st.session_state["analysis_out"] = cleaned
385
- st.session_state["raw_output"] = raw
386
- except Exception as e:
387
- st.session_state["last_error"] = f"Analysis failed: {e}"
388
- st.session_state["last_error_detail"] = traceback.format_exc()
389
- finally:
390
- st.session_state["busy"] = False
391
 
392
  # ---- Layout: analysis first, then video, then errors ----
393
  if st.session_state.get("analysis_out"):
@@ -412,4 +421,4 @@ if st.button("Run Analysis"):
412
  # ----------------------------------------------------------------------
413
  if __name__ == "__main__":
414
  # No need to call _init_state() here – it is invoked inside main()
415
- main()
 
224
  def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
225
  if not prompt or not text:
226
  return text
227
+
228
+ # Normalize the prompt and the response text
229
  clean_prompt = " ".join(prompt.lower().split())
230
+ lower_text = text.lower()
231
+
232
+ # Check if the start of the response matches the prompt
233
+ if lower_text.startswith(clean_prompt):
234
+ # If it matches, remove the prompt section from the start
235
+ return text[len(prompt):].lstrip(" \n:-")
236
+
237
+ # If there is no significant match, return the text as is
238
  return text
239
 
240
  # ----------------------------------------------------------------------
 
366
  )
367
 
368
  # ---------- Main panel ----------
369
+ # Run Analysis button (placed after settings for visual flow)
370
+ if st.button("Run Analysis"):
371
+ if not st.session_state.get("video_path"):
372
+ st.error("No video loaded – load a video first.")
373
+ elif not st.session_state.get("api_key"):
374
+ st.error("Google API key missing – enter it in the sidebar.")
375
+ else:
376
+ # configure Gemini now that we have a key
377
+ genai.configure(api_key=st.session_state["api_key"])
378
+
379
+ st.session_state["busy"] = True
380
+ st.session_state["analysis_out"] = ""
381
+ st.session_state["raw_output"] = ""
382
+ st.session_state["last_error"] = ""
383
+ st.session_state["last_error_detail"] = ""
384
 
385
+ try:
386
+ with st.spinner("Generating report (this may take a minute)…"):
387
+ raw = generate_report(
388
+ Path(st.session_state["video_path"]),
389
+ st.session_state["prompt"],
390
+ st.session_state["model_input"],
391
+ )
392
+ cleaned = _strip_prompt_echo(st.session_state["prompt"], raw)
393
+ st.session_state["analysis_out"] = cleaned
394
+ st.session_state["raw_output"] = raw
395
+ except Exception as e:
396
+ st.session_state["last_error"] = f"Analysis failed: {e}"
397
+ st.session_state["last_error_detail"] = traceback.format_exc()
398
+ finally:
399
+ st.session_state["busy"] = False
400
 
401
  # ---- Layout: analysis first, then video, then errors ----
402
  if st.session_state.get("analysis_out"):
 
421
  # ----------------------------------------------------------------------
422
  if __name__ == "__main__":
423
  # No need to call _init_state() here – it is invoked inside main()
424
+ main()