Mazenbs commited on
Commit
5f501a9
·
verified ·
1 Parent(s): fbb05ab

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -10
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
- # إنشاء ThreadPoolExecutor
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
- # تشغيل السيرفر مع 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)
 
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)