Commit
·
7c0d80d
1
Parent(s):
29436df
Add ALMA logic
Browse files
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 snapshot_download
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import os
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
def download_file_from_hf(repo_id, filename):
|
| 11 |
+
"""
|
| 12 |
+
Downloads a single file from a Hugging Face repo into ~/.sinatools
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
repo_id (str): Hugging Face repo id, e.g. "SinaLab/ArabGlossBERT"
|
| 16 |
+
filename (str): Path of the file inside the repo, e.g. "config.json"
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: Absolute path to the downloaded file
|
| 20 |
+
"""
|
| 21 |
+
target_dir = os.path.expanduser("~/.sinatools")
|
| 22 |
+
os.makedirs(target_dir, exist_ok=True)
|
| 23 |
+
|
| 24 |
+
file_path = hf_hub_download(
|
| 25 |
+
repo_id=repo_id,
|
| 26 |
+
filename=filename,
|
| 27 |
+
local_dir=target_dir,
|
| 28 |
+
local_dir_use_symlinks=False
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
return file_path
|
| 32 |
+
|
| 33 |
+
download_file_from_hf("SinaLab/ALMA","lemmas_dic.pickle")
|
| 34 |
+
|
| 35 |
+
from sinatools.morphology.morph_analyzer import analyze
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class ALMARequest(BaseModel):
|
| 39 |
+
text: str
|
| 40 |
+
|
| 41 |
+
@app.post("/predict")
|
| 42 |
+
def predict(request: ALMARequest):
|
| 43 |
+
text = request.text
|
| 44 |
+
|
| 45 |
+
alma_output = analyze(text)
|
| 46 |
+
content = {
|
| 47 |
+
"resp": alma_output,
|
| 48 |
+
"statusText": "OK",
|
| 49 |
+
"statusCode": 0,
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return JSONResponse(
|
| 53 |
+
content=content,
|
| 54 |
+
media_type="application/json",
|
| 55 |
+
status_code=200,
|
| 56 |
+
)
|