muhammadjalaludin
Implement service layers for backup, model management, file handling, tokenization, and training
1424118 Download api/backup.py from kota101/ta_ml: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/kota101/ta_ml/resolve/main/api/backup.py
- Command line
-
hf download hf://spaces/kota101/ta_ml/api/backup.py
-
curl -L -o backup.py https://huggingface.co/spaces/kota101/ta_ml/resolve/main/api/backup.py
4.98 kB
| # ta_ml/api/backup.py | |
| """ | |
| API endpoints for backup and cache management. | |
| """ | |
| import logging | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from fastapi.responses import FileResponse | |
| from models.api import CacheFileListResponse, BackupUploadResponse | |
| from services.backup_service import BackupService | |
| from api.dependencies import get_backup_service | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(prefix="/backup", tags=["Cache Backup"]) | |
| def list_cache_files( | |
| backup_service: BackupService = Depends(get_backup_service) | |
| ): | |
| try: | |
| files = backup_service.list_cache_files() | |
| return CacheFileListResponse(files=files) | |
| except FileNotFoundError as e: | |
| logger.warning(f"List cache failed: {e}") | |
| raise HTTPException(status_code=404, detail=str(e)) | |
| def download_log(backup_service: BackupService = Depends(get_backup_service)): | |
| try: | |
| log_file_path = backup_service.get_log_file_for_download() | |
| return FileResponse( | |
| path=log_file_path, | |
| filename="training_logs.pkl", | |
| media_type="application/octet-stream", | |
| ) | |
| except FileNotFoundError as e: | |
| logger.warning(f"Log file download failed: {e}") | |
| raise HTTPException(status_code=404, detail=str(e)) | |
| def download_cache(backup_service: BackupService = Depends(get_backup_service)): | |
| try: | |
| archive_path = backup_service.get_cache_archive_for_download() | |
| return FileResponse( | |
| path=archive_path, | |
| filename="hf_cache_backup.zip", | |
| media_type="application/zip", | |
| ) | |
| except FileNotFoundError as e: | |
| raise HTTPException(status_code=404, detail=str(e)) | |
| except Exception as e: | |
| logger.error(f"Cache archiving failed: {e}", exc_info=True) | |
| raise HTTPException( | |
| status_code=500, detail="Failed to create cache archive.") | |
| def upload_cache_to_hub( | |
| backup_service: BackupService = Depends(get_backup_service) | |
| ): | |
| try: | |
| repo_id = backup_service.upload_cache_to_hub() | |
| return BackupUploadResponse( | |
| status="Cache uploaded successfully", | |
| repo=repo_id | |
| ) | |
| except ValueError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| except Exception as e: | |
| logger.error(f"Cache upload to Hub failed: {e}", exc_info=True) | |
| raise HTTPException(status_code=500, detail=f"Upload failed: {e}") | |