Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -250,31 +250,38 @@ def upload_file_to_mistral(client, path: str, filename: str | None = None, purpo
|
|
| 250 |
fname = filename or os.path.basename(path)
|
| 251 |
last_sdk_error = None
|
| 252 |
|
| 253 |
-
# Try SDK
|
| 254 |
try:
|
| 255 |
if hasattr(client, "files") and hasattr(client.files, "upload"):
|
| 256 |
with open(path, "rb") as fh:
|
| 257 |
-
# Many SDKs accept the file-like directly; adapt if your SDK needs different arg names
|
| 258 |
try:
|
| 259 |
-
|
|
|
|
| 260 |
except TypeError:
|
| 261 |
-
#
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
if not fid:
|
| 265 |
-
# Try common SDK shape
|
| 266 |
try:
|
| 267 |
fid = res["data"][0]["id"]
|
| 268 |
except Exception:
|
| 269 |
-
|
| 270 |
if fid:
|
| 271 |
if progress is not None:
|
| 272 |
-
progress(0.6, desc="Upload complete")
|
| 273 |
return fid
|
| 274 |
except Exception as e:
|
| 275 |
last_sdk_error = e
|
| 276 |
|
| 277 |
-
# REST fallback
|
| 278 |
api_key = getattr(client, "api_key", "") or DEFAULT_KEY
|
| 279 |
if not api_key:
|
| 280 |
raise RuntimeError("MISTRAL_API_KEY missing or empty")
|
|
@@ -291,7 +298,6 @@ def upload_file_to_mistral(client, path: str, filename: str | None = None, purpo
|
|
| 291 |
progress(0.65, desc="Upload complete (REST)")
|
| 292 |
return jr.get("id") or jr.get("data", [{}])[0].get("id")
|
| 293 |
except requests.exceptions.RequestException as re:
|
| 294 |
-
# Surface both SDK and REST errors for debugging
|
| 295 |
err_msg = f"File upload failed. REST error: {re}"
|
| 296 |
if last_sdk_error:
|
| 297 |
err_msg += f" | SDK error: {last_sdk_error}"
|
|
|
|
| 250 |
fname = filename or os.path.basename(path)
|
| 251 |
last_sdk_error = None
|
| 252 |
|
| 253 |
+
# Try SDK upload (pass file-like only; avoid unsupported kwargs)
|
| 254 |
try:
|
| 255 |
if hasattr(client, "files") and hasattr(client.files, "upload"):
|
| 256 |
with open(path, "rb") as fh:
|
|
|
|
| 257 |
try:
|
| 258 |
+
# Many SDKs accept just the file-like object; adjust if your SDK docs differ
|
| 259 |
+
res = client.files.upload(fh)
|
| 260 |
except TypeError:
|
| 261 |
+
# Try alternative common shape: single dict arg
|
| 262 |
+
try:
|
| 263 |
+
res = client.files.upload({"file": fh})
|
| 264 |
+
except Exception as e:
|
| 265 |
+
raise e
|
| 266 |
+
# Normalize SDK response to find id
|
| 267 |
+
fid = None
|
| 268 |
+
try:
|
| 269 |
+
fid = getattr(res, "id", None) or (res.get("id") if isinstance(res, dict) else None)
|
| 270 |
+
except Exception:
|
| 271 |
+
fid = None
|
| 272 |
if not fid:
|
|
|
|
| 273 |
try:
|
| 274 |
fid = res["data"][0]["id"]
|
| 275 |
except Exception:
|
| 276 |
+
fid = None
|
| 277 |
if fid:
|
| 278 |
if progress is not None:
|
| 279 |
+
progress(0.6, desc="Upload complete (SDK)")
|
| 280 |
return fid
|
| 281 |
except Exception as e:
|
| 282 |
last_sdk_error = e
|
| 283 |
|
| 284 |
+
# REST fallback: send multipart to /v1/files
|
| 285 |
api_key = getattr(client, "api_key", "") or DEFAULT_KEY
|
| 286 |
if not api_key:
|
| 287 |
raise RuntimeError("MISTRAL_API_KEY missing or empty")
|
|
|
|
| 298 |
progress(0.65, desc="Upload complete (REST)")
|
| 299 |
return jr.get("id") or jr.get("data", [{}])[0].get("id")
|
| 300 |
except requests.exceptions.RequestException as re:
|
|
|
|
| 301 |
err_msg = f"File upload failed. REST error: {re}"
|
| 302 |
if last_sdk_error:
|
| 303 |
err_msg += f" | SDK error: {last_sdk_error}"
|