Spaces:
Build error
Build error
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +19 -5
streamlit_app.py
CHANGED
|
@@ -106,7 +106,14 @@ def file_name_or_id(file_obj):
|
|
| 106 |
return None
|
| 107 |
if isinstance(file_obj, dict):
|
| 108 |
return file_obj.get("name") or file_obj.get("id")
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
def get_effective_api_key():
|
| 112 |
return st.session_state.get("api_key") or os.getenv("GOOGLE_API_KEY")
|
|
@@ -203,7 +210,11 @@ def wait_for_processed(file_obj, timeout=180):
|
|
| 203 |
return file_obj
|
| 204 |
backoff = 1.0
|
| 205 |
while True:
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
state = getattr(obj, "state", None)
|
| 208 |
if not state or getattr(state, "name", None) != "PROCESSING":
|
| 209 |
return obj
|
|
@@ -218,7 +229,10 @@ def remove_prompt_echo(prompt: str, text: str, check_len: int = 600, ratio_thres
|
|
| 218 |
a = " ".join(prompt.strip().lower().split())
|
| 219 |
b_full = text.strip()
|
| 220 |
b = " ".join(b_full[:check_len].lower().split())
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
| 222 |
if ratio >= ratio_threshold:
|
| 223 |
cut = min(len(b_full), max(int(len(prompt) * 0.9), len(a)))
|
| 224 |
new_text = b_full[cut:].lstrip(" \n:-")
|
|
@@ -533,14 +547,14 @@ if generate_now and not st.session_state.get("busy"):
|
|
| 533 |
out = out.strip()
|
| 534 |
|
| 535 |
st.session_state["analysis_out"] = out
|
| 536 |
-
st.session_state["last_error"] = ""
|
| 537 |
st.subheader("Analysis Result")
|
| 538 |
st.markdown(out if out else "No analysis returned.")
|
| 539 |
st.caption(f"Est. max tokens: {est_tokens}")
|
| 540 |
|
| 541 |
except Exception as e:
|
| 542 |
tb = traceback.format_exc()
|
| 543 |
-
st.session_state["last_error"] = f"{e}\n\nDebug: {locals().get('debug_info',
|
| 544 |
st.error("An error occurred while generating the story. You can try Generate again; the uploaded video will be reused.")
|
| 545 |
finally:
|
| 546 |
st.session_state["busy"] = False
|
|
|
|
| 106 |
return None
|
| 107 |
if isinstance(file_obj, dict):
|
| 108 |
return file_obj.get("name") or file_obj.get("id")
|
| 109 |
+
# object-like
|
| 110 |
+
for attr in ("name", "id", "fileId", "file_id", "file_id"):
|
| 111 |
+
if hasattr(file_obj, attr):
|
| 112 |
+
val = getattr(file_obj, attr)
|
| 113 |
+
if val:
|
| 114 |
+
return val
|
| 115 |
+
# fallback to string
|
| 116 |
+
return str(file_obj)
|
| 117 |
|
| 118 |
def get_effective_api_key():
|
| 119 |
return st.session_state.get("api_key") or os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 210 |
return file_obj
|
| 211 |
backoff = 1.0
|
| 212 |
while True:
|
| 213 |
+
try:
|
| 214 |
+
obj = get_file(name)
|
| 215 |
+
except Exception:
|
| 216 |
+
# if the SDK fails, return original object
|
| 217 |
+
return file_obj
|
| 218 |
state = getattr(obj, "state", None)
|
| 219 |
if not state or getattr(state, "name", None) != "PROCESSING":
|
| 220 |
return obj
|
|
|
|
| 229 |
a = " ".join(prompt.strip().lower().split())
|
| 230 |
b_full = text.strip()
|
| 231 |
b = " ".join(b_full[:check_len].lower().split())
|
| 232 |
+
try:
|
| 233 |
+
ratio = SequenceMatcher(None, a, b).ratio()
|
| 234 |
+
except Exception:
|
| 235 |
+
ratio = 0.0
|
| 236 |
if ratio >= ratio_threshold:
|
| 237 |
cut = min(len(b_full), max(int(len(prompt) * 0.9), len(a)))
|
| 238 |
new_text = b_full[cut:].lstrip(" \n:-")
|
|
|
|
| 547 |
out = out.strip()
|
| 548 |
|
| 549 |
st.session_state["analysis_out"] = out
|
| 550 |
+
st.session_state["last_error"] = "" if out else st.session_state.get("last_error", "")
|
| 551 |
st.subheader("Analysis Result")
|
| 552 |
st.markdown(out if out else "No analysis returned.")
|
| 553 |
st.caption(f"Est. max tokens: {est_tokens}")
|
| 554 |
|
| 555 |
except Exception as e:
|
| 556 |
tb = traceback.format_exc()
|
| 557 |
+
st.session_state["last_error"] = f"{e}\n\nDebug: {locals().get('debug_info', {})}\n\nTraceback:\n{tb}"
|
| 558 |
st.error("An error occurred while generating the story. You can try Generate again; the uploaded video will be reused.")
|
| 559 |
finally:
|
| 560 |
st.session_state["busy"] = False
|