Spaces:
Sleeping
Sleeping
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +48 -41
streamlit_app.py
CHANGED
|
@@ -265,68 +265,75 @@ safety_settings = [
|
|
| 265 |
]
|
| 266 |
|
| 267 |
# ---- Upload & processing helpers ----
|
| 268 |
-
def
|
| 269 |
"""
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
Return a dict-like wrapper with at least 'name' key or original obj.
|
| 275 |
"""
|
| 276 |
-
if obj is None:
|
| 277 |
-
return None
|
| 278 |
-
if isinstance(obj, dict):
|
| 279 |
-
# common keys
|
| 280 |
-
name = obj.get("name") or obj.get("id") or obj.get("fileId") or (obj.get("file") and obj["file"].get("name"))
|
| 281 |
-
if name:
|
| 282 |
-
return {"name": name, "_raw": obj}
|
| 283 |
-
return obj
|
| 284 |
-
# SDK object: try attributes and to_dict
|
| 285 |
-
for attr in ("name", "id", "fileId", "file_id"):
|
| 286 |
-
v = getattr(obj, attr, None)
|
| 287 |
-
if v:
|
| 288 |
-
return {"name": v, "_raw": obj}
|
| 289 |
try:
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
|
| 300 |
def upload_video_sdk(filepath: str):
|
|
|
|
| 301 |
key = get_effective_api_key()
|
| 302 |
if not key:
|
| 303 |
raise RuntimeError("No API key provided")
|
| 304 |
if not HAS_GENAI or genai is None:
|
| 305 |
raise RuntimeError("google.generativeai SDK not available; cannot upload")
|
| 306 |
-
|
| 307 |
genai.configure(api_key=key)
|
| 308 |
-
|
| 309 |
-
#
|
| 310 |
candidate_calls = []
|
|
|
|
|
|
|
| 311 |
if upload_file:
|
| 312 |
candidate_calls.append(("upload_file", upload_file))
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
candidate_calls.append(
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
if not candidate_calls:
|
| 320 |
raise RuntimeError("No upload function available in google.generativeai SDK")
|
|
|
|
|
|
|
| 321 |
for name, fn in candidate_calls:
|
| 322 |
try:
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
#
|
| 326 |
-
return
|
| 327 |
except Exception as e:
|
| 328 |
last_exc = e
|
|
|
|
|
|
|
| 329 |
continue
|
|
|
|
|
|
|
| 330 |
raise RuntimeError(f"All upload methods failed. Last error: {last_exc}")
|
| 331 |
|
| 332 |
def wait_for_processed(file_obj, timeout: int = None):
|
|
|
|
| 265 |
]
|
| 266 |
|
| 267 |
# ---- Upload & processing helpers ----
|
| 268 |
+
def _upload_with_kwargs(fn, filepath):
|
| 269 |
"""
|
| 270 |
+
Call an upload function `fn` with the required ragStoreName argument.
|
| 271 |
+
Most recent google.generativeai SDK versions expect:
|
| 272 |
+
upload_file(path, ragStoreName="default")
|
| 273 |
+
Older versions accept just the path, so we try both signatures.
|
|
|
|
| 274 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
try:
|
| 276 |
+
# Preferred modern signature
|
| 277 |
+
return fn(filepath, ragStoreName="default")
|
| 278 |
+
except TypeError:
|
| 279 |
+
# Fallback to older signature (no ragStoreName)
|
| 280 |
+
return fn(filepath)
|
| 281 |
+
except Exception as e:
|
| 282 |
+
# Propagate any other error for higher‑level handling
|
| 283 |
+
raise e
|
| 284 |
+
|
| 285 |
|
| 286 |
def upload_video_sdk(filepath: str):
|
| 287 |
+
"""Upload a video file using whichever upload function the SDK provides."""
|
| 288 |
key = get_effective_api_key()
|
| 289 |
if not key:
|
| 290 |
raise RuntimeError("No API key provided")
|
| 291 |
if not HAS_GENAI or genai is None:
|
| 292 |
raise RuntimeError("google.generativeai SDK not available; cannot upload")
|
| 293 |
+
|
| 294 |
genai.configure(api_key=key)
|
| 295 |
+
|
| 296 |
+
# Build a list of possible upload callables (newest first)
|
| 297 |
candidate_calls = []
|
| 298 |
+
|
| 299 |
+
# Newer SDK exposes upload_file directly
|
| 300 |
if upload_file:
|
| 301 |
candidate_calls.append(("upload_file", upload_file))
|
| 302 |
+
|
| 303 |
+
# Some installations expose it under ragstore or files namespaces
|
| 304 |
+
candidate_calls.append(
|
| 305 |
+
("genai.ragstore.upload_file",
|
| 306 |
+
getattr(genai, "ragstore", None) and getattr(genai.ragstore, "upload_file", None))
|
| 307 |
+
)
|
| 308 |
+
candidate_calls.append(
|
| 309 |
+
("genai.files.upload",
|
| 310 |
+
getattr(genai, "files", None) and getattr(genai.files, "upload", None))
|
| 311 |
+
)
|
| 312 |
+
candidate_calls.append(
|
| 313 |
+
("genai.upload",
|
| 314 |
+
getattr(genai, "upload", None))
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
# Filter out any None entries
|
| 318 |
+
candidate_calls = [(name, fn) for name, fn in candidate_calls if fn]
|
| 319 |
+
|
| 320 |
if not candidate_calls:
|
| 321 |
raise RuntimeError("No upload function available in google.generativeai SDK")
|
| 322 |
+
|
| 323 |
+
last_exc = None
|
| 324 |
for name, fn in candidate_calls:
|
| 325 |
try:
|
| 326 |
+
# Use the wrapper that injects ragStoreName when needed
|
| 327 |
+
res = _upload_with_kwargs(fn, filepath)
|
| 328 |
+
# Normalise the return value so the rest of the code works
|
| 329 |
+
return _normalize_uploaded_obj(res)
|
| 330 |
except Exception as e:
|
| 331 |
last_exc = e
|
| 332 |
+
# Log the attempted method for debugging (optional)
|
| 333 |
+
st.session_state["last_error"] = f"Upload attempt '{name}' failed: {e}"
|
| 334 |
continue
|
| 335 |
+
|
| 336 |
+
# If we get here every method failed
|
| 337 |
raise RuntimeError(f"All upload methods failed. Last error: {last_exc}")
|
| 338 |
|
| 339 |
def wait_for_processed(file_obj, timeout: int = None):
|