Update app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,31 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import torch
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI(title="Qwen API Server")
|
| 7 |
|
| 8 |
-
# إعدادات المودل (تم اختيار 3B ليتناسب مع الـ CPU)
|
| 9 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
MODEL_NAME
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
class ChatMessage(BaseModel):
|
| 24 |
role: str
|
|
@@ -29,10 +37,18 @@ class ChatRequest(BaseModel):
|
|
| 29 |
max_tokens: int = 512
|
| 30 |
temperature: float = 0.7
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@app.post("/v1/chat/completions")
|
| 33 |
async def chat_completion(request: ChatRequest):
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
# تحويل الرسائل إلى صيغة الـ Chat Template الخاصة بـ Qwen
|
| 36 |
formatted_messages = [{"role": m.role, "content": m.content} for m in request.messages]
|
| 37 |
|
| 38 |
text = tokenizer.apply_chat_template(
|
|
@@ -43,17 +59,15 @@ async def chat_completion(request: ChatRequest):
|
|
| 43 |
|
| 44 |
inputs = tokenizer([text], return_tensors="pt")
|
| 45 |
|
| 46 |
-
# توليد الرد
|
| 47 |
with torch.no_grad():
|
| 48 |
outputs = model.generate(
|
| 49 |
**inputs,
|
| 50 |
max_new_tokens=request.max_tokens,
|
| 51 |
-
temperature=request.
|
| 52 |
do_sample=True,
|
| 53 |
top_p=0.9
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# استخراج النص المُولد فقط (بدون المدخلات)
|
| 57 |
generated_ids = [
|
| 58 |
output[len(input_ids):] for input_ids, output in zip(inputs.input_ids, outputs)
|
| 59 |
]
|
|
@@ -72,10 +86,6 @@ async def chat_completion(request: ChatRequest):
|
|
| 72 |
except Exception as e:
|
| 73 |
raise HTTPException(status_code=500, detail=str(e))
|
| 74 |
|
| 75 |
-
@app.get("/")
|
| 76 |
-
def home():
|
| 77 |
-
return {"status": "Running", "model": MODEL_NAME}
|
| 78 |
-
|
| 79 |
if __name__ == "__main__":
|
| 80 |
import uvicorn
|
| 81 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import torch
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
import threading
|
| 6 |
|
| 7 |
app = FastAPI(title="Qwen API Server")
|
| 8 |
|
|
|
|
| 9 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 10 |
|
| 11 |
+
# متغيرات لحفظ حالة التحميل
|
| 12 |
+
tokenizer = None
|
| 13 |
+
model = None
|
| 14 |
+
is_model_loaded = False
|
| 15 |
|
| 16 |
+
def load_model_background():
|
| 17 |
+
global tokenizer, model, is_model_loaded
|
| 18 |
+
print(f"Loading Model in background: {MODEL_NAME} ...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_NAME,
|
| 22 |
+
torch_dtype=torch.float32,
|
| 23 |
+
device_map="cpu"
|
| 24 |
+
)
|
| 25 |
+
is_model_loaded = True
|
| 26 |
+
print("Model successfully loaded on CPU!")
|
| 27 |
+
|
| 28 |
+
# تشغيل تحميل النموذج في Background Thread لكي يبدأ الـ FastAPI فوراً ولا يتم عمل Shutdown
|
| 29 |
+
threading.Thread(target=load_model_background).start()
|
| 30 |
|
| 31 |
class ChatMessage(BaseModel):
|
| 32 |
role: str
|
|
|
|
| 37 |
max_tokens: int = 512
|
| 38 |
temperature: float = 0.7
|
| 39 |
|
| 40 |
+
@app.get("/")
|
| 41 |
+
def home():
|
| 42 |
+
if not is_model_loaded:
|
| 43 |
+
return {"status": "Loading model...", "model": MODEL_NAME}
|
| 44 |
+
return {"status": "Running", "model": MODEL_NAME}
|
| 45 |
+
|
| 46 |
@app.post("/v1/chat/completions")
|
| 47 |
async def chat_completion(request: ChatRequest):
|
| 48 |
+
if not is_model_loaded:
|
| 49 |
+
raise HTTPException(status_code=503, detail="Model is still loading, please try again in a minute.")
|
| 50 |
+
|
| 51 |
try:
|
|
|
|
| 52 |
formatted_messages = [{"role": m.role, "content": m.content} for m in request.messages]
|
| 53 |
|
| 54 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 59 |
|
| 60 |
inputs = tokenizer([text], return_tensors="pt")
|
| 61 |
|
|
|
|
| 62 |
with torch.no_grad():
|
| 63 |
outputs = model.generate(
|
| 64 |
**inputs,
|
| 65 |
max_new_tokens=request.max_tokens,
|
| 66 |
+
temperature=request.temperature,
|
| 67 |
do_sample=True,
|
| 68 |
top_p=0.9
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
generated_ids = [
|
| 72 |
output[len(input_ids):] for input_ids, output in zip(inputs.input_ids, outputs)
|
| 73 |
]
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
raise HTTPException(status_code=500, detail=str(e))
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if __name__ == "__main__":
|
| 90 |
import uvicorn
|
| 91 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|