Commit
·
1cf32f2
1
Parent(s):
b3dcc54
Add Dockerfile + auto‑download Mistral .gguf + FastAPI
Browse files- Dockerfile +17 -7
- app.py +8 -11
- setup.sh +3 -3
Dockerfile
CHANGED
|
@@ -1,18 +1,28 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
|
| 9 |
-
#
|
| 10 |
WORKDIR /app
|
|
|
|
|
|
|
| 11 |
COPY . /app
|
| 12 |
|
| 13 |
-
# تثبيت
|
| 14 |
RUN pip install --upgrade pip && \
|
| 15 |
pip install -r requirements.txt
|
| 16 |
|
| 17 |
-
# تشغيل
|
| 18 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
# -------------------------------------------------
|
| 2 |
+
# Dockerfile – يستخدم صورة Python كاملة (تحتوي على gcc)
|
| 3 |
+
# -------------------------------------------------
|
| 4 |
|
| 5 |
+
# 1️⃣ صورة الأساس
|
| 6 |
+
FROM python:3.10
|
| 7 |
+
|
| 8 |
+
# 2️⃣ تثبيت الأدوات اللازمة لبناء llama‑cpp‑python
|
| 9 |
+
# (gcc, g++, make, cmake)
|
| 10 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 11 |
+
gcc \
|
| 12 |
+
g++ \
|
| 13 |
+
make \
|
| 14 |
+
cmake \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
+
# 3️⃣ تعيين دليل العمل داخل الحاوية
|
| 18 |
WORKDIR /app
|
| 19 |
+
|
| 20 |
+
# 4️⃣ نسخ كل الملفات من المستودع إلى داخل الحاوية
|
| 21 |
COPY . /app
|
| 22 |
|
| 23 |
+
# 5️⃣ تثبيت المكتبات Python من requirements.txt
|
| 24 |
RUN pip install --upgrade pip && \
|
| 25 |
pip install -r requirements.txt
|
| 26 |
|
| 27 |
+
# 6️⃣ تشغيل FastAPI عندما تُنشأ الحاوية
|
| 28 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -12,46 +12,43 @@ app = FastAPI(
|
|
| 12 |
# -------------------------------------------------
|
| 13 |
# 1️⃣ تحميل نموذج T5 المدرب من Hub
|
| 14 |
# -------------------------------------------------
|
| 15 |
-
T5_REPO = "MGZON/mgzon-flan-t5-base"
|
| 16 |
t5_tokenizer = AutoTokenizer.from_pretrained(T5_REPO)
|
| 17 |
t5_model = AutoModelForSeq2SeqLM.from_pretrained(T5_REPO)
|
| 18 |
|
| 19 |
# -------------------------------------------------
|
| 20 |
-
# 2️⃣ تحميل ملف Mistral .gguf (تم تنزيله
|
| 21 |
# -------------------------------------------------
|
| 22 |
gguf_path = os.path.abspath("models/mistral-7b-instruct-v0.1.Q4_K_M.gguf")
|
| 23 |
mistral = Llama(
|
| 24 |
model_path=gguf_path,
|
| 25 |
n_ctx=2048,
|
| 26 |
-
n_threads=8,
|
| 27 |
# إذا كان لديك GPU داخل Space يمكنك إضافة n_gpu_layers=35
|
| 28 |
)
|
| 29 |
|
| 30 |
# -------------------------------------------------
|
| 31 |
-
# 3️⃣
|
| 32 |
# -------------------------------------------------
|
| 33 |
class AskRequest(BaseModel):
|
| 34 |
question: str
|
| 35 |
-
max_new_tokens: int = 150
|
| 36 |
|
| 37 |
-
# -------------------------------------------------
|
| 38 |
-
# 4️⃣ نقطة النهاية /ask
|
| 39 |
-
# -------------------------------------------------
|
| 40 |
@app.post("/ask")
|
| 41 |
def ask(req: AskRequest):
|
| 42 |
q = req.question.strip()
|
| 43 |
if not q:
|
| 44 |
raise HTTPException(status_code=400, detail="Empty question")
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if any(tok in q.lower() for tok in ["mgzon", "flan", "t5"]):
|
| 48 |
-
#
|
| 49 |
inputs = t5_tokenizer(q, return_tensors="pt", truncation=True, max_length=256)
|
| 50 |
out_ids = t5_model.generate(**inputs, max_length=req.max_new_tokens)
|
| 51 |
answer = t5_tokenizer.decode(out_ids[0], skip_special_tokens=True)
|
| 52 |
model_name = "MGZON‑FLAN‑T5"
|
| 53 |
else:
|
| 54 |
-
#
|
| 55 |
out = mistral(prompt=q, max_tokens=req.max_new_tokens)
|
| 56 |
answer = out["choices"][0]["text"].strip()
|
| 57 |
model_name = "Mistral‑7B‑GGUF"
|
|
|
|
| 12 |
# -------------------------------------------------
|
| 13 |
# 1️⃣ تحميل نموذج T5 المدرب من Hub
|
| 14 |
# -------------------------------------------------
|
| 15 |
+
T5_REPO = "MGZON/mgzon-flan-t5-base" # إذا رفعت النموذج إلى مستودع آخر استبدل الاسم
|
| 16 |
t5_tokenizer = AutoTokenizer.from_pretrained(T5_REPO)
|
| 17 |
t5_model = AutoModelForSeq2SeqLM.from_pretrained(T5_REPO)
|
| 18 |
|
| 19 |
# -------------------------------------------------
|
| 20 |
+
# 2️⃣ تحميل ملف Mistral .gguf (تم تنزيله بواسطة setup.sh)
|
| 21 |
# -------------------------------------------------
|
| 22 |
gguf_path = os.path.abspath("models/mistral-7b-instruct-v0.1.Q4_K_M.gguf")
|
| 23 |
mistral = Llama(
|
| 24 |
model_path=gguf_path,
|
| 25 |
n_ctx=2048,
|
| 26 |
+
n_threads=8, # عدد الـ threads المتاح داخل Space (CPU)
|
| 27 |
# إذا كان لديك GPU داخل Space يمكنك إضافة n_gpu_layers=35
|
| 28 |
)
|
| 29 |
|
| 30 |
# -------------------------------------------------
|
| 31 |
+
# 3️⃣ طلب API
|
| 32 |
# -------------------------------------------------
|
| 33 |
class AskRequest(BaseModel):
|
| 34 |
question: str
|
| 35 |
+
max_new_tokens: int = 150
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.post("/ask")
|
| 38 |
def ask(req: AskRequest):
|
| 39 |
q = req.question.strip()
|
| 40 |
if not q:
|
| 41 |
raise HTTPException(status_code=400, detail="Empty question")
|
| 42 |
|
| 43 |
+
# اختيار النموذج بناءً على كلمة مفتاحية (يمكنك تعديل المنطق)
|
| 44 |
if any(tok in q.lower() for tok in ["mgzon", "flan", "t5"]):
|
| 45 |
+
# --------- T5 ----------
|
| 46 |
inputs = t5_tokenizer(q, return_tensors="pt", truncation=True, max_length=256)
|
| 47 |
out_ids = t5_model.generate(**inputs, max_length=req.max_new_tokens)
|
| 48 |
answer = t5_tokenizer.decode(out_ids[0], skip_special_tokens=True)
|
| 49 |
model_name = "MGZON‑FLAN‑T5"
|
| 50 |
else:
|
| 51 |
+
# --------- Mistral ----------
|
| 52 |
out = mistral(prompt=q, max_tokens=req.max_new_tokens)
|
| 53 |
answer = out["choices"][0]["text"].strip()
|
| 54 |
model_name = "Mistral‑7B‑GGUF"
|
setup.sh
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
set -e
|
| 3 |
|
| 4 |
-
# 1️⃣ مجلد لتخزين النموذج
|
| 5 |
mkdir -p models
|
| 6 |
|
| 7 |
-
# 2️⃣ تحميل ملف .gguf إذا لم يكن موجودًا
|
| 8 |
python - <<PY
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import os
|
|
@@ -24,4 +24,4 @@ if not os.path.exists(os.path.join(local_dir, filename)):
|
|
| 24 |
print("✅ تم تحميل Mistral .gguf")
|
| 25 |
else:
|
| 26 |
print("✅ الملف موجود مسبقًا")
|
| 27 |
-
PY
|
|
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
set -e
|
| 3 |
|
| 4 |
+
# 1️⃣ إنشاء مجلد لتخزين النموذج
|
| 5 |
mkdir -p models
|
| 6 |
|
| 7 |
+
# 2️⃣ تحميل ملف .gguf إذا لم يكن موجودًا مسبقًا
|
| 8 |
python - <<PY
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import os
|
|
|
|
| 24 |
print("✅ تم تحميل Mistral .gguf")
|
| 25 |
else:
|
| 26 |
print("✅ الملف موجود مسبقًا")
|
| 27 |
+
PY
|