Upload 2 files
Browse files- api.py +92 -0
- requirements.txt +108 -0
api.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import gdown
|
| 4 |
+
import torch
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from transformers import AutoTokenizer, MT5ForConditionalGeneration
|
| 8 |
+
import uvicorn
|
| 9 |
+
|
| 10 |
+
# 1. إعداد الروابط والمسارات
|
| 11 |
+
# ضعي الـ ID الخاص بالملف المضغوط هنا (يستخرج من رابط المشاركة)
|
| 12 |
+
DRIVE_FILE_ID = "ضعي_معرف_الملف_هنا"
|
| 13 |
+
ZIP_PATH = "./model.zip"
|
| 14 |
+
MODEL_PATH = "./saved_openie_model"
|
| 15 |
+
|
| 16 |
+
# 2. دالة لتحميل وفك ضغط النموذج إذا لم يكن موجوداً
|
| 17 |
+
def download_and_extract_model():
|
| 18 |
+
if not os.path.exists(MODEL_PATH) or not os.listdir(MODEL_PATH):
|
| 19 |
+
print("جاري تحميل النموذج من Google Drive...")
|
| 20 |
+
|
| 21 |
+
url = f"https://drive.google.com/uc?id=1MZHAeCaQAyyfi6b2Dh3V_JCdTe8fTVmP"
|
| 22 |
+
# تحميل الملف
|
| 23 |
+
gdown.download(url, ZIP_PATH, quiet=False)
|
| 24 |
+
|
| 25 |
+
print("جاري فك ضغط النموذج...")
|
| 26 |
+
with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
|
| 27 |
+
# استخراج الملفات في الدليل الحالي (يُفترض أن الملف المضغوط يحتوي على مجلد saved_openie_model)
|
| 28 |
+
zip_ref.extractall(".")
|
| 29 |
+
|
| 30 |
+
# اختيارياً: حذف الملف المضغوط لتوفير المساحة على الخادم
|
| 31 |
+
if os.path.exists(ZIP_PATH):
|
| 32 |
+
os.remove(ZIP_PATH)
|
| 33 |
+
|
| 34 |
+
print(" تم تجهيز النموذج محلياً بنجاح!")
|
| 35 |
+
else:
|
| 36 |
+
print(" النموذج موجود مسبقاً، تخطي مرحلة التحميل.")
|
| 37 |
+
|
| 38 |
+
# تشغيل دالة التحميل قبل أي شيء آخر
|
| 39 |
+
download_and_extract_model()
|
| 40 |
+
|
| 41 |
+
# 3. إعداد تطبيق FastAPI
|
| 42 |
+
app = FastAPI(title="mT5 Relation Extraction API", version="1.0")
|
| 43 |
+
device = torch.device("cpu")
|
| 44 |
+
|
| 45 |
+
# 4. تحميل النموذج والمُرمّز في الذاكرة
|
| 46 |
+
print(" جاري تحميل النموذج والمُرمّز في الذاكرة...")
|
| 47 |
+
try:
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 49 |
+
model = MT5ForConditionalGeneration.from_pretrained(MODEL_PATH)
|
| 50 |
+
model.to(device)
|
| 51 |
+
model.eval() # وضع الاستنتاج
|
| 52 |
+
print("تم تحميل النموذج بنجاح! الخادم جاهز لاستقبال الطلبات.")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f" حدث خطأ أثناء تحميل النموذج: {e}")
|
| 55 |
+
|
| 56 |
+
# 5. تعريف شكل البيانات المستقبلة
|
| 57 |
+
class RelationRequest(BaseModel):
|
| 58 |
+
text: str
|
| 59 |
+
|
| 60 |
+
# 6. نقطة النهاية (Endpoint) لاستقبال الطلبات
|
| 61 |
+
@app.post("/extract_relation")
|
| 62 |
+
async def extract_relation_api(request: RelationRequest):
|
| 63 |
+
try:
|
| 64 |
+
prompt = "استخراج العلاقة: " + request.text
|
| 65 |
+
|
| 66 |
+
inputs = tokenizer(
|
| 67 |
+
prompt,
|
| 68 |
+
return_tensors="pt",
|
| 69 |
+
max_length=64,
|
| 70 |
+
padding="max_length",
|
| 71 |
+
truncation=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
outputs = model.generate(
|
| 76 |
+
input_ids=inputs["input_ids"].to(device),
|
| 77 |
+
attention_mask=inputs["attention_mask"].to(device),
|
| 78 |
+
max_new_tokens=15,
|
| 79 |
+
num_beams=3,
|
| 80 |
+
early_stopping=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
predicted_relation = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 84 |
+
|
| 85 |
+
return {"relation": predicted_relation}
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
|
| 92 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
annotated-doc==0.0.5
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.12.1
|
| 4 |
+
asttokens==3.0.2
|
| 5 |
+
beautifulsoup4==4.15.0
|
| 6 |
+
cachetools==6.0.0
|
| 7 |
+
camel-kenlm==2025.9.16
|
| 8 |
+
camel_tools==1.5.7
|
| 9 |
+
certifi==2026.7.22
|
| 10 |
+
cffi==2.0.0
|
| 11 |
+
charset-normalizer==3.4.9
|
| 12 |
+
click==8.1.8
|
| 13 |
+
contourpy==1.3.0
|
| 14 |
+
cryptography==50.0.0
|
| 15 |
+
cycler==0.12.1
|
| 16 |
+
decorator==5.3.1
|
| 17 |
+
dill==0.4.1
|
| 18 |
+
docopt==0.6.2
|
| 19 |
+
editdistance==0.8.1
|
| 20 |
+
emoji==2.15.0
|
| 21 |
+
exceptiongroup==1.3.1
|
| 22 |
+
executing==2.2.1
|
| 23 |
+
fastapi==0.128.8
|
| 24 |
+
filelock==3.19.1
|
| 25 |
+
fonttools==4.60.2
|
| 26 |
+
fsspec==2025.10.0
|
| 27 |
+
future==1.0.0
|
| 28 |
+
gdown==5.2.2
|
| 29 |
+
google-ai-generativelanguage==0.6.15
|
| 30 |
+
google-api-core==2.30.3
|
| 31 |
+
google-api-python-client==2.198.0
|
| 32 |
+
google-auth==2.50.0
|
| 33 |
+
google-auth-httplib2==0.3.1
|
| 34 |
+
google-generativeai==0.8.6
|
| 35 |
+
googleapis-common-protos==1.75.0
|
| 36 |
+
grpcio==1.80.0
|
| 37 |
+
grpcio-status==1.71.2
|
| 38 |
+
h11==0.16.0
|
| 39 |
+
hf-xet==1.5.2
|
| 40 |
+
httplib2==0.32.0
|
| 41 |
+
huggingface_hub==0.36.2
|
| 42 |
+
idna==3.18
|
| 43 |
+
importlib_resources==6.5.2
|
| 44 |
+
ipython==8.18.1
|
| 45 |
+
jedi==0.19.2
|
| 46 |
+
Jinja2==3.1.6
|
| 47 |
+
joblib==1.5.3
|
| 48 |
+
kiwisolver==1.4.7
|
| 49 |
+
MarkupSafe==3.0.3
|
| 50 |
+
matplotlib==3.9.4
|
| 51 |
+
matplotlib-inline==0.2.2
|
| 52 |
+
mpmath==1.3.0
|
| 53 |
+
muddler==0.1.3
|
| 54 |
+
networkx==3.2.1
|
| 55 |
+
nltk==3.9.2
|
| 56 |
+
numpy==1.26.4
|
| 57 |
+
packaging==26.2
|
| 58 |
+
pandas==2.3.3
|
| 59 |
+
parso==0.8.7
|
| 60 |
+
pexpect==4.9.0
|
| 61 |
+
pillow==11.3.0
|
| 62 |
+
prompt_toolkit==3.0.52
|
| 63 |
+
proto-plus==1.27.2
|
| 64 |
+
protobuf==5.29.6
|
| 65 |
+
ptyprocess==0.7.0
|
| 66 |
+
pure_eval==0.2.3
|
| 67 |
+
pyasn1==0.6.4
|
| 68 |
+
pyasn1_modules==0.4.2
|
| 69 |
+
pycparser==2.23
|
| 70 |
+
pydantic==2.13.4
|
| 71 |
+
pydantic_core==2.46.4
|
| 72 |
+
Pygments==2.20.0
|
| 73 |
+
pyparsing==3.3.2
|
| 74 |
+
pyrsistent==0.20.0
|
| 75 |
+
PySocks==1.7.1
|
| 76 |
+
python-dateutil==2.9.0.post0
|
| 77 |
+
pytz==2026.3.post1
|
| 78 |
+
PyYAML==6.0.3
|
| 79 |
+
regex==2026.1.15
|
| 80 |
+
requests==2.32.5
|
| 81 |
+
safetensors==0.7.0
|
| 82 |
+
scikit-learn==1.6.1
|
| 83 |
+
scipy==1.13.1
|
| 84 |
+
sentencepiece==0.2.2
|
| 85 |
+
six==1.17.0
|
| 86 |
+
sklearn==0.0
|
| 87 |
+
soupsieve==2.8.4
|
| 88 |
+
stack-data==0.6.3
|
| 89 |
+
stanza==1.11.0
|
| 90 |
+
starlette==0.49.3
|
| 91 |
+
sympy==1.14.0
|
| 92 |
+
tabulate==0.9.0
|
| 93 |
+
threadpoolctl==3.6.0
|
| 94 |
+
tiktoken==0.13.0
|
| 95 |
+
tokenizers==0.19.1
|
| 96 |
+
tomli==2.4.1
|
| 97 |
+
torch==2.8.0
|
| 98 |
+
tqdm==4.70.0
|
| 99 |
+
traitlets==5.15.1
|
| 100 |
+
transformers==4.43.4
|
| 101 |
+
typing-inspection==0.4.2
|
| 102 |
+
typing_extensions==4.16.0
|
| 103 |
+
tzdata==2026.3
|
| 104 |
+
uritemplate==4.2.0
|
| 105 |
+
urllib3==2.6.3
|
| 106 |
+
uvicorn==0.39.0
|
| 107 |
+
wcwidth==0.8.2
|
| 108 |
+
zipp==3.23.1
|