File size: 2,329 Bytes
844a0f9
0d6afb7
 
 
 
844a0f9
6e3aaba
844a0f9
 
0d6afb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1d5013
 
 
 
0d6afb7
 
0059ef4
0d6afb7
 
 
 
 
 
0059ef4
0d6afb7
 
 
 
 
 
0059ef4
0d6afb7
0059ef4
 
 
 
 
726215a
0059ef4
 
 
 
 
 
 
 
0d6afb7
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

@app.post("/predict")
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,
    )