Commit ·
41869c7
1
Parent(s): f1bc1ad
Add fastapi.responses and starlette.responses imports
Browse files2 unmodified lines34567891 unmodified line1112131415162 unmodified lines1920212223242526272829303132333481 unmodified lines11611711811912012112212312412513 unmodified lines13914014114214314414514614714814915015115215315415515615715815936 unmodified lines1961971981992002012 unmodified linesfrom pathlib import Pathfrom statistics import meanfrom typing import Any, Iterator, Unionimport fasttextfrom dotenv import load_dotenvfrom fastapi import FastAPI1 unmodified linefrom huggingface_hub import hf_hub_downloadfrom huggingface_hub.utils import loggingfrom toolz import concat, groupby, valmapapp = FastAPI()logger = logging.get_logger(__name__)2 unmodified linesos.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"DEFAULT_FAST_TEXT_MODEL = "laurievb/OpenLID"headers = {"authorization": f"Bearer ${HF_TOKEN}",}timeout = Timeout(60, read=120)client = Client(headers=headers, timeout=timeout)async_client = AsyncClient(headers=headers, timeout=timeout)# non exhaustive list of columns that might contain text which can be used for language detection# we prefer to use columns in this order i.e. if there is a column named "text" we will use it firstTARGET_COLUMN_NAMES = {"text","input",81 unmodified linesdef load_model(repo_id: str) -> fasttext.FastText._FastText:model_path = hf_hub_download(repo_id, filename="model.bin")return fasttext.load_model(model_path)def yield_clean_rows(rows: Union[list[str], str], min_length: int = 3) -> Iterator[str]:for row in rows:if isinstance(row, str):13 unmodified linescontinueFASTTEXT_PREFIX_LENGTH = 9 # fasttext labels are formatted like "__label__eng_Latn"# model = load_model(DEFAULT_FAST_TEXT_MODEL)Path("code/models").mkdir(parents=True, exist_ok=True)model = fasttext.load_model(hf_hub_download("facebook/fasttext-language-identification","model.bin",cache_dir="code/models",local_dir="code/models",local_dir_use_symlinks=False,))def model_predict(inputs: str, k=1) -> list[dict[str, float]]:predictions = model.predict(inputs, k=k)return [36 unmodified lines}@app.get("/predict_dataset_language/{hub_id}")async def predict_language(hub_id: str,2 unmodified lines34567891 unmodified line111213141516172 unmodified lines202122232425262728293031323334353681 unmodified lines11811912012112212312412512612712812913013113213313413513613713 unmodified lines15115215315415515636 unmodified lines1931941951961971981992002012022032042052062072082092 unmodified linesfrom pathlib import Pathfrom statistics import meanfrom typing import Any, Iterator, Unionfrom fastapi.responses import HTMLResponseimport fasttextfrom dotenv import load_dotenvfrom fastapi import FastAPI1 unmodified linefrom huggingface_hub import hf_hub_downloadfrom huggingface_hub.utils import loggingfrom toolz import concat, groupby, valmapfrom starlette.responses import RedirectResponseapp = FastAPI()logger = logging.get_logger(__name__)2 unmodified linesos.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"FASTTEXT_PREFIX_LENGTH = 9 # fasttext labels are formatted like "__label__eng_Latn"BASE_DATASETS_SERVER_URL = "https://datasets-server.huggingface.co"DEFAULT_FAST_TEXT_MODEL = "facebook/fasttext-language-identification"headers = {"authorization": f"Bearer ${HF_TOKEN}",}timeout = Timeout(60, read=120)client = Client(headers=headers, timeout=timeout)async_client = AsyncClient(headers=headers, timeout=timeout)TARGET_COLUMN_NAMES = {"text","input",81 unmodified linesdef load_model(repo_id: str) -> fasttext.FastText._FastText:Path("code/models").mkdir(parents=True, exist_ok=True)model_path = hf_hub_download(repo_id,"model.bin",cache_dir="code/models",local_dir="code/models",local_dir_use_symlinks=False,)return fasttext.load_model(model_path)model = load_model(DEFAULT_FAST_TEXT_MODEL)def yield_clean_rows(rows: Union[list[str], str], min_length: int = 3) -> Iterator[str]:for row in rows:if isinstance(row, str):13 unmodified linescontinuedef model_predict(inputs: str, k=1) -> list[dict[str, float]]:predictions = model.predict(inputs, k=k)return [36 unmodified lines}# @app.get("/", response_class=HTMLResponse)# async def read_index():# html_content = Path("index.html").read_text()# return HTMLResponse(content=html_content)@app.get("/", include_in_schema=False)def root():return RedirectResponse(url="/docs")@app.get("/predict_dataset_language/{hub_id}")async def predict_language(hub_id: str,
main.py+28-20