Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -250,25 +250,14 @@ 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 upload
|
| 254 |
try:
|
| 255 |
if hasattr(client, "files") and hasattr(client.files, "upload"):
|
| 256 |
with open(path, "rb") as fh:
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 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"]
|
|
@@ -281,7 +270,7 @@ def upload_file_to_mistral(client, path: str, filename: str | None = None, purpo
|
|
| 281 |
except Exception as e:
|
| 282 |
last_sdk_error = e
|
| 283 |
|
| 284 |
-
# REST fallback:
|
| 285 |
api_key = getattr(client, "api_key", "") or DEFAULT_KEY
|
| 286 |
if not api_key:
|
| 287 |
raise RuntimeError("MISTRAL_API_KEY missing or empty")
|
|
|
|
| 250 |
fname = filename or os.path.basename(path)
|
| 251 |
last_sdk_error = None
|
| 252 |
|
| 253 |
+
# Try SDK upload: pass exactly one positional file-like argument
|
| 254 |
try:
|
| 255 |
if hasattr(client, "files") and hasattr(client.files, "upload"):
|
| 256 |
with open(path, "rb") as fh:
|
| 257 |
+
# many SDKs expect a single positional file arg
|
| 258 |
+
res = client.files.upload(fh)
|
| 259 |
+
# normalize response
|
| 260 |
+
fid = getattr(res, "id", None) or (res.get("id") if isinstance(res, dict) else None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
if not fid:
|
| 262 |
try:
|
| 263 |
fid = res["data"][0]["id"]
|
|
|
|
| 270 |
except Exception as e:
|
| 271 |
last_sdk_error = e
|
| 272 |
|
| 273 |
+
# REST fallback: multipart/form-data with field name "file" and purpose in form data
|
| 274 |
api_key = getattr(client, "api_key", "") or DEFAULT_KEY
|
| 275 |
if not api_key:
|
| 276 |
raise RuntimeError("MISTRAL_API_KEY missing or empty")
|