Spaces:
Running
Running
File size: 8,431 Bytes
6c24b50 2b6ef22 5c8183d 6c24b50 2b6ef22 6c24b50 2b6ef22 6c24b50 62aa98f 2b6ef22 6c24b50 b7dddbe 6c24b50 2b6ef22 bd469c1 6c24b50 5afbc54 6c24b50 2b6ef22 6c24b50 d02a73b 62aa98f d02a73b 62aa98f d02a73b 6c24b50 b7dddbe 6c24b50 b7dddbe 6c24b50 b7dddbe 6c24b50 2b6ef22 bd469c1 2b6ef22 b7dddbe 2b6ef22 bd469c1 2b6ef22 bd469c1 2b6ef22 d02a73b b7dddbe d02a73b 62aa98f d02a73b b7dddbe d02a73b 62aa98f d02a73b b7dddbe 62aa98f d02a73b 62aa98f d02a73b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | from __future__ import annotations
import io
import os
import platform
import tarfile
import time
from datetime import datetime, timezone
from pathlib import Path
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
from fastapi.responses import StreamingResponse
from app.api.deps import get_extraction_service
from app.config import get_settings
from app.core.constants import (
ARCHIVE_EXTENSIONS,
AUDIO_EXTENSIONS,
DOCUMENT_EXTENSIONS,
IMAGE_EXTENSIONS,
OFFICE_EXTENSIONS,
SUPPORTED_EXTENSIONS,
TEXT_EXTENSIONS,
WEB_EXTENSIONS,
)
from app.core.logger import get_logger
from app.core.thread_pool import run_in_executor
from app.models.schemas import (
HealthResponse,
InfoResponse,
SpacyLabelsResponse,
SupportedFormatsResponse,
)
from app.services.extraction_service import ExtractionService
router = APIRouter() # system routes
_settings = get_settings()
_START_TIME = time.time()
_logger = get_logger(__name__)
_MAINTENANCE_MODE: bool = False
def _maintenance_lock_path() -> Path:
return _get_data_dir() / ".maintenance.lock"
def is_maintenance() -> bool:
if _MAINTENANCE_MODE:
return True
if os.environ.get("APP_UNDER_MAINTENANCE", "").lower() in ("1", "true", "yes"):
return True
lock_path = _maintenance_lock_path()
return lock_path.is_file()
@router.get("/health", response_model=HealthResponse, summary="Health check")
async def health():
return HealthResponse(
success=True,
status="ok",
version=_settings.app_version,
uptime_seconds=round(time.time() - _START_TIME, 2),
timestamp=datetime.now(timezone.utc).isoformat(),
)
@router.get("/info", response_model=InfoResponse, summary="Server and environment information")
async def info():
return InfoResponse(
success=True,
app=_settings.app_name,
version=_settings.app_version,
python_version=platform.python_version(),
platform=platform.system(),
uptime_seconds=round(time.time() - _START_TIME, 2),
max_upload_mb=_settings.max_upload_mb,
supported_extensions=len(SUPPORTED_EXTENSIONS),
timestamp=datetime.now(timezone.utc).isoformat(),
)
@router.get("/formats", response_model=SupportedFormatsResponse, summary="List supported file formats")
async def list_formats():
by_category = {
"documents": [e for e in SUPPORTED_EXTENSIONS if e in DOCUMENT_EXTENSIONS],
"office": [e for e in SUPPORTED_EXTENSIONS if e in OFFICE_EXTENSIONS],
"data": [e for e in SUPPORTED_EXTENSIONS if e in {".csv", ".json", ".xml"}],
"web": [e for e in SUPPORTED_EXTENSIONS if e in WEB_EXTENSIONS],
"text": [e for e in SUPPORTED_EXTENSIONS if e in TEXT_EXTENSIONS],
"images": [e for e in SUPPORTED_EXTENSIONS if e in IMAGE_EXTENSIONS],
"audio": [e for e in SUPPORTED_EXTENSIONS if e in AUDIO_EXTENSIONS],
"archives": [e for e in SUPPORTED_EXTENSIONS if e in ARCHIVE_EXTENSIONS],
}
return SupportedFormatsResponse(
success=True,
total_count=len(SUPPORTED_EXTENSIONS),
all_extensions=sorted(SUPPORTED_EXTENSIONS),
by_category={k: sorted(v) for k, v in by_category.items()},
)
@router.get("/spacy-labels", response_model=SpacyLabelsResponse, summary="List available spaCy NER labels")
async def list_spacy_labels(
extraction_service: ExtractionService = Depends(get_extraction_service),
):
return SpacyLabelsResponse(
success=True,
spacy_labels=extraction_service.get_spacy_labels(),
source_types={
"entity": "Extract using spaCy NER labels (ORG, PERSON, DATE, etc.)",
"regex": "Extract using custom regular expressions",
"token_attr": "Extract using token attributes (text, pos_, tag_, etc.)",
},
example_mappings={
"company": {"source_type": "entity", "label": "ORG"},
"person": {"source_type": "entity", "label": "PERSON"},
"date": {"source_type": "entity", "label": "DATE"},
"money": {"source_type": "entity", "label": "MONEY"},
"email": {
"source_type": "regex",
"pattern": r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b",
},
"phone": {"source_type": "regex", "pattern": r"\b\d{3}-\d{3}-\d{4}\b"},
},
)
def _build_archive(data_dir: Path) -> io.BytesIO:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
for path in sorted(data_dir.rglob("*")):
if path.is_file():
arcname = path.relative_to(data_dir.parent)
tar.add(str(path), arcname=str(arcname))
buf.seek(0)
return buf
def _extract_archive(content: bytes, data_dir: Path) -> int:
restored = 0
with tarfile.open(fileobj=io.BytesIO(content), mode="r:gz") as tar:
for member in tar.getmembers():
if member.isfile():
tar.extract(member, path=data_dir.parent)
restored += 1
return restored
def _get_data_dir() -> Path:
raw = _settings.data_dir
p = Path(raw)
if not p.is_absolute():
p = Path.cwd() / p
return p.resolve()
@router.get("/backup", summary="Download a full data backup archive")
async def download_backup():
data_dir = _get_data_dir()
if not data_dir.is_dir():
raise HTTPException(status_code=404, detail="Data directory not found")
try:
archive = await run_in_executor(_build_archive, data_dir)
except Exception as exc:
_logger.error("Backup creation failed: %s", exc)
raise HTTPException(status_code=500, detail=f"Backup failed: {exc}")
return StreamingResponse(
archive,
media_type="application/gzip",
headers={"Content-Disposition": "attachment; filename=backup.tar.gz"},
)
@router.post("/backup/restore", summary="Upload and restore a data backup archive")
async def upload_and_restore(
file: UploadFile = File(...),
):
data_dir = _get_data_dir()
data_dir.mkdir(parents=True, exist_ok=True)
try:
content = await file.read()
except Exception as exc:
raise HTTPException(status_code=400, detail=f"Failed to read upload: {exc}")
if not content:
raise HTTPException(status_code=400, detail="Empty file")
restored = 0
try:
restored = await run_in_executor(_extract_archive, content, data_dir)
except tarfile.TarError as exc:
raise HTTPException(status_code=400, detail=f"Invalid archive: {exc}")
return {
"success": True,
"message": f"Restored {restored} files to {data_dir}",
"files_restored": restored,
"data_dir": str(data_dir),
}
@router.post("/maintenance/enable", summary="Enable maintenance mode (blocks write operations)")
async def enable_maintenance():
global _MAINTENANCE_MODE
_MAINTENANCE_MODE = True
lock_path = _maintenance_lock_path()
try:
lock_path.parent.mkdir(parents=True, exist_ok=True)
lock_path.write_text(str(time.time()), encoding="utf-8")
except Exception as exc:
_logger.warning("Could not write maintenance lock file: %s", exc)
_logger.warning("Maintenance mode ENABLED — all write operations blocked")
return {"success": True, "message": "Maintenance mode enabled", "maintenance": True}
@router.post("/maintenance/disable", summary="Disable maintenance mode")
async def disable_maintenance():
global _MAINTENANCE_MODE
_MAINTENANCE_MODE = False
lock_path = _maintenance_lock_path()
try:
lock_path.unlink(missing_ok=True)
except Exception as exc:
_logger.warning("Could not remove maintenance lock file: %s", exc)
_logger.warning("Maintenance mode DISABLED — write operations resumed")
return {"success": True, "message": "Maintenance mode disabled", "maintenance": False}
@router.get("/maintenance", summary="Check maintenance mode status")
async def maintenance_status():
lock_path = _maintenance_lock_path()
source = "off"
if _MAINTENANCE_MODE:
source = "api_flag"
elif os.environ.get("APP_UNDER_MAINTENANCE"):
source = "env_var"
elif lock_path.is_file():
source = "lock_file"
return {
"success": True,
"maintenance": is_maintenance(),
"source": source,
}
|