Spaces:
Running
Running
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
+
import numpy as np
|
| 6 |
+
from embeddingonnx import text_to_embedding, query_to_embedding # دوال التحويل موجودة مسبقًا
|
| 7 |
+
|
| 8 |
+
# ==============================
|
| 9 |
+
# إنشاء تطبيق FastAPI
|
| 10 |
+
# ==============================
|
| 11 |
+
app = FastAPI(title="Arabic Text Embedding API")
|
| 12 |
+
|
| 13 |
+
# ==============================
|
| 14 |
+
# إنشاء ThreadPoolExecutor
|
| 15 |
+
# ==============================
|
| 16 |
+
executor = ThreadPoolExecutor(max_workers=2)
|
| 17 |
+
|
| 18 |
+
# ==============================
|
| 19 |
+
# نموذج البيانات الوارد
|
| 20 |
+
# ==============================
|
| 21 |
+
class TextRequest(BaseModel):
|
| 22 |
+
text: str
|
| 23 |
+
|
| 24 |
+
# ==============================
|
| 25 |
+
# نقاط النهاية الأساسية
|
| 26 |
+
# ==============================
|
| 27 |
+
@app.get("/")
|
| 28 |
+
def root():
|
| 29 |
+
return {"message": "✅ Arabic Text Embedding API is running."}
|
| 30 |
+
|
| 31 |
+
@app.get("/health")
|
| 32 |
+
def health():
|
| 33 |
+
return {"status": "ok"}
|
| 34 |
+
|
| 35 |
+
# ==============================
|
| 36 |
+
# نقاط النهاية POST
|
| 37 |
+
# ==============================
|
| 38 |
+
@app.post("/embed")
|
| 39 |
+
def embed_endpoint(request: TextRequest):
|
| 40 |
+
text = request.text.strip()
|
| 41 |
+
if not text:
|
| 42 |
+
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 43 |
+
try:
|
| 44 |
+
# تنفيذ في ThreadPool
|
| 45 |
+
future = executor.submit(text_to_embedding, text, True)
|
| 46 |
+
vector = future.result()
|
| 47 |
+
if vector is None:
|
| 48 |
+
raise HTTPException(status_code=400, detail="لم يتم إنشاء embedding للنص.")
|
| 49 |
+
return {"embedding": vector.tolist()}
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding: {str(e)}")
|
| 52 |
+
|
| 53 |
+
@app.post("/query")
|
| 54 |
+
def query_endpoint(request: TextRequest):
|
| 55 |
+
query_text = request.text.strip()
|
| 56 |
+
if not query_text:
|
| 57 |
+
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 58 |
+
try:
|
| 59 |
+
# تنفيذ في ThreadPool
|
| 60 |
+
future = executor.submit(query_to_embedding, query_text, True)
|
| 61 |
+
vector = future.result()
|
| 62 |
+
if vector is None:
|
| 63 |
+
raise HTTPException(status_code=400, detail="لم يتم إنشاء embedding للاستعلام.")
|
| 64 |
+
return {"query_embedding": vector.tolist()}
|
| 65 |
+
except Exception as e:
|
| 66 |
+
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding للاستعلام: {str(e)}")
|
| 67 |
+
|
| 68 |
+
# ==============================
|
| 69 |
+
# نقاط النهاية GET
|
| 70 |
+
# ==============================
|
| 71 |
+
@app.get("/embed")
|
| 72 |
+
def embed_get(text: str = Query(..., description="النص المراد تحويله إلى embedding")):
|
| 73 |
+
text = text.strip()
|
| 74 |
+
if not text:
|
| 75 |
+
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 76 |
+
try:
|
| 77 |
+
future = executor.submit(text_to_embedding, text, True)
|
| 78 |
+
vector = future.result()
|
| 79 |
+
if vector is None:
|
| 80 |
+
raise HTTPException(status_code=400, detail="لم يتم إنشاء embedding للنص.")
|
| 81 |
+
return {"embedding": vector.tolist()}
|
| 82 |
+
except Exception as e:
|
| 83 |
+
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding: {str(e)}")
|
| 84 |
+
|
| 85 |
+
@app.get("/query")
|
| 86 |
+
def query_get(text: str = Query(..., description="النص المراد تحويله إلى query embedding")):
|
| 87 |
+
text = text.strip()
|
| 88 |
+
if not text:
|
| 89 |
+
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 90 |
+
try:
|
| 91 |
+
future = executor.submit(query_to_embedding, text, True)
|
| 92 |
+
vector = future.result()
|
| 93 |
+
if vector is None:
|
| 94 |
+
raise HTTPException(status_code=400, detail="لم يتم إنشاء embedding للاستعلام.")
|
| 95 |
+
return {"query_embedding": vector.tolist()}
|
| 96 |
+
except Exception as e:
|
| 97 |
+
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding للاستعلام: {str(e)}")
|
| 98 |
+
|
| 99 |
+
# ==============================
|
| 100 |
+
# تشغيل السيرفر مع warm-up اختياري
|
| 101 |
+
# ==============================
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
# Warm-up للنموذج لتقليل احتمال فشل أول طلب
|
| 104 |
+
dummy_text = "هذا نص للتجربة"
|
| 105 |
+
text_to_embedding(dummy_text)
|
| 106 |
+
query_to_embedding(dummy_text)
|
| 107 |
+
|
| 108 |
+
import uvicorn
|
| 109 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000)
|