Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,7 +3,8 @@ 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
|
|
@@ -11,7 +12,7 @@ from embeddingonnx import text_to_embedding, query_to_embedding # دوال ال
|
|
| 11 |
app = FastAPI(title="Arabic Text Embedding API")
|
| 12 |
|
| 13 |
# ==============================
|
| 14 |
-
#
|
| 15 |
# ==============================
|
| 16 |
executor = ThreadPoolExecutor(max_workers=2)
|
| 17 |
|
|
@@ -41,7 +42,6 @@ def embed_endpoint(request: TextRequest):
|
|
| 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:
|
|
@@ -56,7 +56,6 @@ def query_endpoint(request: TextRequest):
|
|
| 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:
|
|
@@ -97,13 +96,24 @@ def query_get(text: str = Query(..., description="النص المراد تحوي
|
|
| 97 |
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding للاستعلام: {str(e)}")
|
| 98 |
|
| 99 |
# ==============================
|
| 100 |
-
#
|
| 101 |
# ==============================
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
import uvicorn
|
| 109 |
uvicorn.run("main:app", host="0.0.0.0", port=8000)
|
|
|
|
| 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 |
+
import threading
|
| 8 |
|
| 9 |
# ==============================
|
| 10 |
# إنشاء تطبيق FastAPI
|
|
|
|
| 12 |
app = FastAPI(title="Arabic Text Embedding API")
|
| 13 |
|
| 14 |
# ==============================
|
| 15 |
+
# ThreadPoolExecutor لمعالجة الدوال الثقيلة
|
| 16 |
# ==============================
|
| 17 |
executor = ThreadPoolExecutor(max_workers=2)
|
| 18 |
|
|
|
|
| 42 |
if not text:
|
| 43 |
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 44 |
try:
|
|
|
|
| 45 |
future = executor.submit(text_to_embedding, text, True)
|
| 46 |
vector = future.result()
|
| 47 |
if vector is None:
|
|
|
|
| 56 |
if not query_text:
|
| 57 |
raise HTTPException(status_code=400, detail="النص فارغ.")
|
| 58 |
try:
|
|
|
|
| 59 |
future = executor.submit(query_to_embedding, query_text, True)
|
| 60 |
vector = future.result()
|
| 61 |
if vector is None:
|
|
|
|
| 96 |
raise HTTPException(status_code=500, detail=f"خطأ أثناء إنشاء embedding للاستعلام: {str(e)}")
|
| 97 |
|
| 98 |
# ==============================
|
| 99 |
+
# Warm-up تلقائي للنموذج عند بدء التشغيل
|
| 100 |
# ==============================
|
| 101 |
+
def warmup_models():
|
| 102 |
+
try:
|
| 103 |
+
dummy_text = "هذا نص للتجربة"
|
| 104 |
+
# تنفيذ في ThreadPool لتجنب blocking السيرفر
|
| 105 |
+
executor.submit(text_to_embedding, dummy_text, True)
|
| 106 |
+
executor.submit(query_to_embedding, dummy_text, True)
|
| 107 |
+
print("✅ Warm-up للنموذج اكتمل.")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"⚠️ خطأ أثناء warm-up: {e}")
|
| 110 |
+
|
| 111 |
+
# تشغيل Warm-up في thread منفصل
|
| 112 |
+
threading.Thread(target=warmup_models, daemon=True).start()
|
| 113 |
|
| 114 |
+
# ==============================
|
| 115 |
+
# تشغيل السيرفر
|
| 116 |
+
# ==============================
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
import uvicorn
|
| 119 |
uvicorn.run("main:app", host="0.0.0.0", port=8000)
|