Spaces:
Running
Running
Pulastya B commited on
Commit ·
ccd6d0c
1
Parent(s): feaba15
Add better error logging to HF export endpoint
Browse files- src/api/app.py +50 -13
src/api/app.py
CHANGED
|
@@ -1416,15 +1416,23 @@ async def export_to_huggingface(request: HuggingFaceExportRequest):
|
|
| 1416 |
|
| 1417 |
# Fetch user's HuggingFace token from hf_tokens table (not user_profiles)
|
| 1418 |
logger.info(f"[HF Export] Fetching HF token from hf_tokens table...")
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
| 1423 |
-
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
|
| 1427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1428 |
|
| 1429 |
if not hf_token:
|
| 1430 |
raise HTTPException(
|
|
@@ -1432,10 +1440,19 @@ async def export_to_huggingface(request: HuggingFaceExportRequest):
|
|
| 1432 |
detail="HuggingFace token not found. Please connect in Settings."
|
| 1433 |
)
|
| 1434 |
|
| 1435 |
-
|
| 1436 |
-
from src.storage.huggingface_storage import HuggingFaceStorage
|
| 1437 |
|
| 1438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1439 |
|
| 1440 |
# Collect all session assets
|
| 1441 |
uploaded_files = []
|
|
@@ -1448,6 +1465,14 @@ async def export_to_huggingface(request: HuggingFaceExportRequest):
|
|
| 1448 |
|
| 1449 |
logger.info(f"[HF Export] Looking for files in: {session_outputs_dir}, {global_outputs_dir}, {tmp_outputs_dir}")
|
| 1450 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1451 |
# Upload datasets (CSVs)
|
| 1452 |
csv_patterns = [
|
| 1453 |
session_outputs_dir / "*.csv",
|
|
@@ -1554,11 +1579,23 @@ async def export_to_huggingface(request: HuggingFaceExportRequest):
|
|
| 1554 |
errors.append(f"Image {Path(image_file).name}: {str(e)}")
|
| 1555 |
|
| 1556 |
if not uploaded_files and errors:
|
|
|
|
| 1557 |
raise HTTPException(
|
| 1558 |
status_code=500,
|
| 1559 |
detail=f"Export failed: {'; '.join(errors)}"
|
| 1560 |
)
|
| 1561 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1562 |
return JSONResponse({
|
| 1563 |
"success": True,
|
| 1564 |
"uploaded_files": uploaded_files,
|
|
@@ -1569,7 +1606,7 @@ async def export_to_huggingface(request: HuggingFaceExportRequest):
|
|
| 1569 |
except HTTPException:
|
| 1570 |
raise
|
| 1571 |
except Exception as e:
|
| 1572 |
-
logger.error(f"
|
| 1573 |
raise HTTPException(status_code=500, detail=f"Export failed: {str(e)}")
|
| 1574 |
|
| 1575 |
|
|
|
|
| 1416 |
|
| 1417 |
# Fetch user's HuggingFace token from hf_tokens table (not user_profiles)
|
| 1418 |
logger.info(f"[HF Export] Fetching HF token from hf_tokens table...")
|
| 1419 |
+
try:
|
| 1420 |
+
result = supabase.table("hf_tokens").select(
|
| 1421 |
+
"huggingface_token, huggingface_username"
|
| 1422 |
+
).eq("user_id", request.user_id).execute()
|
| 1423 |
+
|
| 1424 |
+
logger.info(f"[HF Export] Query result: {result.data}")
|
| 1425 |
+
|
| 1426 |
+
if not result.data or len(result.data) == 0:
|
| 1427 |
+
raise HTTPException(status_code=404, detail="HuggingFace not connected. Please connect in Settings first.")
|
| 1428 |
+
|
| 1429 |
+
hf_token = result.data[0].get("huggingface_token")
|
| 1430 |
+
hf_username = result.data[0].get("huggingface_username")
|
| 1431 |
+
except HTTPException:
|
| 1432 |
+
raise
|
| 1433 |
+
except Exception as db_error:
|
| 1434 |
+
logger.error(f"[HF Export] Database query error: {db_error}")
|
| 1435 |
+
raise HTTPException(status_code=500, detail=f"Database error: {str(db_error)}")
|
| 1436 |
|
| 1437 |
if not hf_token:
|
| 1438 |
raise HTTPException(
|
|
|
|
| 1440 |
detail="HuggingFace token not found. Please connect in Settings."
|
| 1441 |
)
|
| 1442 |
|
| 1443 |
+
logger.info(f"[HF Export] Got HF token for user {hf_username}, initializing HuggingFace storage...")
|
|
|
|
| 1444 |
|
| 1445 |
+
# Import HuggingFace storage service
|
| 1446 |
+
try:
|
| 1447 |
+
from src.storage.huggingface_storage import HuggingFaceStorage
|
| 1448 |
+
hf_service = HuggingFaceStorage(hf_token=hf_token)
|
| 1449 |
+
logger.info(f"[HF Export] HuggingFace storage initialized successfully")
|
| 1450 |
+
except ImportError as e:
|
| 1451 |
+
logger.error(f"[HF Export] Failed to import HuggingFaceStorage: {e}")
|
| 1452 |
+
raise HTTPException(status_code=500, detail="Server error: huggingface_hub package not installed")
|
| 1453 |
+
except Exception as e:
|
| 1454 |
+
logger.error(f"[HF Export] Failed to initialize HuggingFace storage: {e}")
|
| 1455 |
+
raise HTTPException(status_code=500, detail=f"HuggingFace initialization failed: {str(e)}")
|
| 1456 |
|
| 1457 |
# Collect all session assets
|
| 1458 |
uploaded_files = []
|
|
|
|
| 1465 |
|
| 1466 |
logger.info(f"[HF Export] Looking for files in: {session_outputs_dir}, {global_outputs_dir}, {tmp_outputs_dir}")
|
| 1467 |
|
| 1468 |
+
# Log directory contents for debugging
|
| 1469 |
+
for dir_path in [session_outputs_dir, global_outputs_dir, tmp_outputs_dir]:
|
| 1470 |
+
if dir_path.exists():
|
| 1471 |
+
files = list(dir_path.glob("*"))
|
| 1472 |
+
logger.info(f"[HF Export] Files in {dir_path}: {[f.name for f in files[:10]]}")
|
| 1473 |
+
else:
|
| 1474 |
+
logger.info(f"[HF Export] Directory does not exist: {dir_path}")
|
| 1475 |
+
|
| 1476 |
# Upload datasets (CSVs)
|
| 1477 |
csv_patterns = [
|
| 1478 |
session_outputs_dir / "*.csv",
|
|
|
|
| 1579 |
errors.append(f"Image {Path(image_file).name}: {str(e)}")
|
| 1580 |
|
| 1581 |
if not uploaded_files and errors:
|
| 1582 |
+
logger.error(f"[HF Export] All uploads failed: {errors}")
|
| 1583 |
raise HTTPException(
|
| 1584 |
status_code=500,
|
| 1585 |
detail=f"Export failed: {'; '.join(errors)}"
|
| 1586 |
)
|
| 1587 |
|
| 1588 |
+
if not uploaded_files and not errors:
|
| 1589 |
+
logger.warning(f"[HF Export] No files found to export for session {request.session_id}")
|
| 1590 |
+
return JSONResponse({
|
| 1591 |
+
"success": True,
|
| 1592 |
+
"uploaded_files": [],
|
| 1593 |
+
"errors": None,
|
| 1594 |
+
"message": "No files found to export. Run some analysis first to generate outputs."
|
| 1595 |
+
})
|
| 1596 |
+
|
| 1597 |
+
logger.info(f"[HF Export] Export complete: {len(uploaded_files)} files uploaded, {len(errors)} errors")
|
| 1598 |
+
|
| 1599 |
return JSONResponse({
|
| 1600 |
"success": True,
|
| 1601 |
"uploaded_files": uploaded_files,
|
|
|
|
| 1606 |
except HTTPException:
|
| 1607 |
raise
|
| 1608 |
except Exception as e:
|
| 1609 |
+
logger.error(f"[HF Export] Unexpected error: {str(e)}", exc_info=True)
|
| 1610 |
raise HTTPException(status_code=500, detail=f"Export failed: {str(e)}")
|
| 1611 |
|
| 1612 |
|