Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from huggingface_hub import hf_hub_download | |
| import os | |
| from pydantic import BaseModel | |
| from fastapi.responses import JSONResponse | |
| print("Version ---- 3") | |
| app = FastAPI() | |
| def download_file_from_hf(repo_id, filename): | |
| target_dir = os.path.expanduser("~/.sinatools") | |
| os.makedirs(target_dir, exist_ok=True) | |
| file_path = hf_hub_download( | |
| repo_id=repo_id, | |
| filename=filename, | |
| local_dir=target_dir, | |
| local_dir_use_symlinks=False | |
| ) | |
| return file_path | |
| download_file_from_hf("SinaLab/ALMA","lemmas_dic.pickle") | |
| download_file_from_hf("SinaLab/synonyms_models","graph_l2.pkl") | |
| download_file_from_hf("SinaLab/synonyms_models","graph_l3.pkl") | |
| download_file_from_hf("SinaLab/ArabGlossBERT","two_grams.pickle") | |
| download_file_from_hf("SinaLab/ArabGlossBERT","three_grams.pickle") | |
| download_file_from_hf("SinaLab/ArabGlossBERT","four_grams.pickle") | |
| download_file_from_hf("SinaLab/ArabGlossBERT","five_grams.pickle") | |
| from sinatools.synonyms.synonyms_generator import extend_synonyms | |
| # from sinatools.synonyms.synonyms_generator import evaluate_synonyms | |
| from sinatools.morphology.morph_analyzer import analyze | |
| class synonymsRequest(BaseModel): | |
| synset: str | |
| useALMA: str | |
| level: str | |
| # serviceType: str | |
| def predict(request: synonymsRequest): | |
| synset = request.synset | |
| useALMA = request.useALMA | |
| level = int(request.level) | |
| # serviceType = request.serviceType | |
| # if serviceType == "evaluate": | |
| # list_of_synon_with_fuzzy_value = evaluate_synonyms(synset, level) | |
| # content = {"resp": list_of_synon_with_fuzzy_value, "statusText": "OK","statusCode" : 0} | |
| # else: | |
| final_synset = [] | |
| if useALMA == "true": | |
| synonyms = synset.split("|") | |
| for synonym in synonyms: | |
| final_synset.append(analyze(synonym.strip(), language = "MSA", task = "lemmatization", flag = "1")[0]["lemma"]) | |
| list_of_synon_with_fuzzy_value = extend_synonyms(" | ".join(final_synset), level) | |
| else: | |
| list_of_synon_with_fuzzy_value = extend_synonyms(synset, level) | |
| content = {"resp": list_of_synon_with_fuzzy_value, "statusText": "OK","statusCode" : 0} | |
| return JSONResponse( | |
| content=content, | |
| media_type="application/json", | |
| status_code=200, | |
| ) |