muhammadjalaludin
Implement service layers for backup, model management, file handling, tokenization, and training
1424118 Download api/tokenize.py from kota101/ta_ml: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/spaces/kota101/ta_ml/resolve/main/api/tokenize.py
- Command line
-
hf download hf://spaces/kota101/ta_ml/api/tokenize.py
-
curl -L -o tokenize.py https://huggingface.co/spaces/kota101/ta_ml/resolve/main/api/tokenize.py
4.53 kB
| # ta_ml/api/tokenize.py | |
| """ | |
| API endpoints for text tokenization. | |
| """ | |
| import logging | |
| from typing import Optional | |
| import pandas as pd | |
| from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile | |
| from fastapi.responses import StreamingResponse | |
| from api.dependencies import get_tokenization_service | |
| from data.file_handler import FileHandler, FileHandlerError | |
| from models.api import (TokenizationDatasetResponse, | |
| TokenizationResult) | |
| from services.tokenization_service import TokenizationService | |
| logger = logging.getLogger(__name__) | |
| router = APIRouter(prefix="/tokenize", tags=["Tokenization"]) | |
| def tokenize_word( | |
| word: str = Query(..., min_length=1, | |
| description="The word or phrase to tokenize."), | |
| tokenization_service: TokenizationService = Depends( | |
| get_tokenization_service), | |
| ): | |
| return tokenization_service.tokenize_word(word) | |
| async def tokenize_file( | |
| file: UploadFile = File(..., description="A CSV or Excel file."), | |
| text_column: str = Query( | |
| "text", description="The name of the column containing text to tokenize."), | |
| limit: int = Query(100, ge=1, le=10000, | |
| description="Maximum number of rows to process."), | |
| offset: int = Query( | |
| 0, ge=0, description="Number of rows to skip from the beginning."), | |
| export_format: Optional[str] = Query(None, enum=[ | |
| "csv", "xlsx"], description="If provided, returns the tokenized data as a downloadable file."), | |
| tokenization_service: TokenizationService = Depends( | |
| get_tokenization_service), | |
| ): | |
| try: | |
| df = FileHandler.read_to_dataframe(file) | |
| except FileHandlerError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| if text_column not in df.columns: | |
| raise HTTPException( | |
| status_code=400, detail=f"Column '{text_column}' not found.") | |
| df_slice = df.iloc[offset: offset + limit] | |
| if df_slice.empty: | |
| raise HTTPException( | |
| status_code=400, detail="No data found for the given offset and limit.") | |
| results, stats = tokenization_service.tokenize_dataframe( | |
| df_slice, text_column) | |
| if export_format: | |
| output_df = pd.DataFrame([res.model_dump() for res in results]) | |
| buffer, media_type, filename = FileHandler.export_dataframe( | |
| output_df, export_format) | |
| headers = { | |
| "Content-Disposition": f"attachment; filename=tokenized_{filename}"} | |
| return StreamingResponse(buffer, media_type=media_type, headers=headers) | |
| return TokenizationDatasetResponse(stats=stats, results=results) | |