Download app/api_routes.py from hHoai/api: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/hHoai/api/resolve/main/app/api_routes.py
- Command line
-
hf download hf://spaces/hHoai/api/app/api_routes.py
-
curl -L -o api_routes.py https://huggingface.co/spaces/hHoai/api/resolve/main/app/api_routes.py
1.53 kB
| from fastapi import APIRouter, HTTPException | |
| from typing import List | |
| import logging | |
| from app.schemas import TextRequest, CorrectionResponse | |
| from app.model_handler import correct_spelling | |
| # Thiết lập logging | |
| logger = logging.getLogger(__name__) | |
| # Tạo router | |
| router = APIRouter() | |
| async def root(): | |
| return {"message": "Chào mừng đến với API sửa lỗi chính tả BARTpho"} | |
| async def health_check(): | |
| from app.model_handler import corrector | |
| if corrector is None: | |
| raise HTTPException(status_code=503, detail="Mô hình chưa được tải") | |
| return {"status": "healthy", "model_loaded": corrector is not None} | |
| async def correct_text_api(request: TextRequest): | |
| try: | |
| result = correct_spelling(request.text, request.options) | |
| return result | |
| except Exception as e: | |
| logger.error(f"Lỗi khi xử lý yêu cầu: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def batch_correct(requests: List[TextRequest]): | |
| try: | |
| results = [] | |
| for request in requests: | |
| result = correct_spelling(request.text, request.options) | |
| results.append(result) | |
| return results | |
| except Exception as e: | |
| logger.error(f"Lỗi khi xử lý yêu cầu hàng loạt: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) |