CB commited on
Commit
7e44dbd
·
verified ·
1 Parent(s): b208b42

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +48 -41
streamlit_app.py CHANGED
@@ -265,68 +265,75 @@ safety_settings = [
265
  ]
266
 
267
  # ---- Upload & processing helpers ----
268
- def _normalize_uploaded_obj(obj):
269
  """
270
- Normalize possible upload_file return shapes:
271
- - SDK object with .name or .id
272
- - dict with 'name' or 'id'
273
- - legacy or ragstore shapes: {'file': {...}} etc.
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
- if hasattr(obj, "to_dict"):
291
- d = obj.to_dict()
292
- if isinstance(d, dict):
293
- name = d.get("name") or d.get("id") or d.get("fileId") or (d.get("file") and d["file"].get("name"))
294
- if name:
295
- return {"name": name, "_raw": obj}
296
- except Exception:
297
- pass
298
- return {"name": None, "_raw": obj}
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
- # prefer upload_file if present, otherwise try genai.ragstore or genai.files.upload style
307
  genai.configure(api_key=key)
308
- last_exc = None
309
- # try a few known entrypoints
310
  candidate_calls = []
 
 
311
  if upload_file:
312
  candidate_calls.append(("upload_file", upload_file))
313
- # sometimes SDK exposed as genai.ragstore.upload_file or genai.files.upload
314
- candidate_calls.append(("genai.ragstore.upload_file", getattr(genai, "ragstore", None) and getattr(genai.ragstore, "upload_file", None)))
315
- candidate_calls.append(("genai.files.upload", getattr(genai, "files", None) and getattr(genai.files, "upload", None)))
316
- candidate_calls.append(("genai.upload", getattr(genai, "upload", None)))
317
- # filter none
318
- candidate_calls = [(n, fn) for n, fn in candidate_calls if fn]
 
 
 
 
 
 
 
 
 
 
 
 
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
- res = fn(filepath)
324
- norm = _normalize_uploaded_obj(res)
325
- # ensure we return something that file_name_or_id understands
326
- return norm if isinstance(norm, dict) else res
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):