Spaces:
Running
Running
Commit
·
0d6afb7
1
Parent(s):
844a0f9
Use sinatools synonyms generator
Browse files- app.py +52 -3
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,7 +1,56 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
import os
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
|
| 7 |
+
print("Version ---- 1")
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
def download_file_from_hf(repo_id, filename):
|
| 11 |
+
target_dir = os.path.expanduser("~/.sinatools")
|
| 12 |
+
os.makedirs(target_dir, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
file_path = hf_hub_download(
|
| 15 |
+
repo_id=repo_id,
|
| 16 |
+
filename=filename,
|
| 17 |
+
local_dir=target_dir,
|
| 18 |
+
local_dir_use_symlinks=False
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
return file_path
|
| 22 |
+
|
| 23 |
+
download_file_from_hf("SinaLab/ALMA","lemmas_dic.pickle")
|
| 24 |
+
download_file_from_hf("SinaLab/synonyms_models","graph_l2.pkl")
|
| 25 |
+
download_file_from_hf("SinaLab/synonyms_models","graph_l3.pkl")
|
| 26 |
+
|
| 27 |
+
from sinatools.synonyms.synonyms_generator import extend_synonyms
|
| 28 |
+
from sinatools.morphology.morph_analyzer import analyze
|
| 29 |
+
|
| 30 |
+
class synonymsRequest(BaseModel):
|
| 31 |
+
synset: str
|
| 32 |
+
useALMA: str
|
| 33 |
+
level: str
|
| 34 |
+
|
| 35 |
+
@app.post("/predict")
|
| 36 |
+
def predict(request: synonymsRequest):
|
| 37 |
+
synset = request.synset
|
| 38 |
+
useALMA = request.useALMA
|
| 39 |
+
level = int(request.level)
|
| 40 |
+
|
| 41 |
+
final_synset = []
|
| 42 |
+
if useALMA == True:
|
| 43 |
+
synonyms = synset.split("|")
|
| 44 |
+
for synonym in synonyms:
|
| 45 |
+
final_synset.append(analyze(synonym.strip(), language = "MSA", task = "lemmatization", flag = "1")[0]["lemma"])
|
| 46 |
+
list_of_synon_with_fuzzy_value = extend_synonyms(" | ".join(final_synset), level)
|
| 47 |
+
else:
|
| 48 |
+
list_of_synon_with_fuzzy_value = extend_synonyms(synset, level)
|
| 49 |
+
|
| 50 |
+
content = {"resp": list_of_synon_with_fuzzy_value, "statusText": "OK","statusCode" : 0}
|
| 51 |
+
|
| 52 |
+
return JSONResponse(
|
| 53 |
+
content=content,
|
| 54 |
+
media_type="application/json",
|
| 55 |
+
status_code=200,
|
| 56 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn[standard]
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
SinaTools
|