ibrahimlasfar commited on
Commit
585128b
·
1 Parent(s): 431e7f9

update full files

Browse files
Files changed (4) hide show
  1. Dockerfile +12 -2
  2. README.md +35 -2
  3. app.py +3 -18
  4. setup.sh +1 -0
Dockerfile CHANGED
@@ -10,9 +10,16 @@ RUN apt-get update && apt-get install -y \
10
  make \
11
  && rm -rf /var/lib/apt/lists/*
12
 
 
 
 
13
  # إعداد مجلد العمل
14
  WORKDIR /app
15
 
 
 
 
 
16
  # نسخ ملفات التطبيق
17
  COPY requirements.txt .
18
  RUN pip install --no-cache-dir -r requirements.txt
@@ -25,9 +32,12 @@ RUN chmod +x setup.sh
25
  # تحميل النموذج
26
  RUN ./setup.sh
27
 
28
- # تعيين المتغيّر لتجنب التحذير
29
  ENV HF_HOME=/app/.cache/huggingface
30
- ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
 
 
 
31
 
32
  # تشغيل التطبيق
33
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
 
10
  make \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
+ # إنشاء مستخدم غير جذر لتشغيل التطبيق
14
+ RUN useradd -m -u 1000 appuser
15
+
16
  # إعداد مجلد العمل
17
  WORKDIR /app
18
 
19
+ # إنشاء مجلد التخزين المؤقت وتغيير الأذونات
20
+ RUN mkdir -p /app/.cache/huggingface && \
21
+ chown -R appuser:appuser /app
22
+
23
  # نسخ ملفات التطبيق
24
  COPY requirements.txt .
25
  RUN pip install --no-cache-dir -r requirements.txt
 
32
  # تحميل النموذج
33
  RUN ./setup.sh
34
 
35
+ # تعيين المتغيرات البيئية
36
  ENV HF_HOME=/app/.cache/huggingface
37
+ ENV PYTHONUNBUFFERED=1
38
+
39
+ # تغيير المستخدم إلى appuser
40
+ USER appuser
41
 
42
  # تشغيل التطبيق
43
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Api Mg
3
  emoji: 🏃
4
  colorFrom: purple
5
  colorTo: gray
@@ -8,5 +8,38 @@ sdk_version: 0.115.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: MGZON/api-mg
12
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: MGZON Smart Assistant
3
  emoji: 🏃
4
  colorFrom: purple
5
  colorTo: gray
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: API for integrating T5 and Mistral-7B (GGUF) models in Hugging Face Spaces
12
  ---
13
+
14
+ # MGZON Smart Assistant
15
+
16
+ This project provides a FastAPI-based API for integrating two language models:
17
+ - **MGZON-FLAN-T5**: A pre-trained T5 model fine-tuned to respond to questions containing keywords like "mgzon", "flan", or "t5".
18
+ - **Mistral-7B-GGUF**: A Mistral-7B model in GGUF format for answering general questions.
19
+
20
+ ![Demo](https://huggingface.co/spaces/MGZON/api-mg/resolve/main/demo.png)
21
+
22
+ ## Setup
23
+ - **Docker**: The image is built using `python:3.10-slim` with development tools (`gcc`, `g++`, `cmake`, `make`) installed to support building `llama-cpp-python`.
24
+ - **System Requirements**: Dependencies are installed from `requirements.txt`, including `transformers`, `torch`, `fastapi`, and `llama-cpp-python`.
25
+ - **Model Download**: The Mistral-7B GGUF model is downloaded via `setup.sh` using `huggingface_hub`.
26
+ - **Environment Variables**:
27
+ - `HF_HOME` and `TRANSFORMERS_CACHE` are set to `/app/.cache/huggingface`.
28
+ - `HF_TOKEN` (secret) is required to access models from the Hugging Face Hub.
29
+
30
+ ## How to Run
31
+ 1. Build the Docker image using the provided `Dockerfile`.
32
+ 2. Ensure the `HF_TOKEN` is set in the Hugging Face Spaces settings.
33
+ 3. Run the application using `uvicorn` on port 8080.
34
+
35
+ ## Endpoint
36
+ - **POST /ask**:
37
+ - **Input**: JSON containing `question` (the query) and `max_new_tokens` (optional, default=150).
38
+ - **Output**: JSON containing `model` (name of the model used) and `response` (the answer).
39
+
40
+ ## Example Usage
41
+ ```bash
42
+ curl -X POST "https://mgzon-api-mg.hf.space/ask" \
43
+ -H "Content-Type: application/json" \
44
+ -d '{"question": "What is MGZON?", "max_new_tokens": 100}'
45
+ ```
app.py CHANGED
@@ -4,27 +4,19 @@ from pydantic import BaseModel
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
  from llama_cpp import Llama
6
 
7
- # -------------------------------------------------
8
  # إعداد مسار الـ cache
9
- # -------------------------------------------------
10
- CACHE_DIR = "/app/.cache/huggingface" # مسار موحد لـ Hugging Face Spaces
11
- os.makedirs(CACHE_DIR, exist_ok=True)
12
 
13
  # تأكد من أن المكتبتين تقرأ المتغيّرات البيئية
14
- os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
15
  os.environ["HF_HOME"] = CACHE_DIR
16
 
17
- # -------------------------------------------------
18
  # إنشاء التطبيق
19
- # -------------------------------------------------
20
  app = FastAPI(
21
  title="MGZON Smart Assistant",
22
  description="دمج نموذج T5 المدرب مع Mistral‑7B (GGUF) داخل Space"
23
  )
24
 
25
- # -------------------------------------------------
26
- # 1️⃣ تحميل نموذج T5 المدرب من Hub
27
- # -------------------------------------------------
28
  T5_REPO = "MGZON/mgzon-flan-t5-base"
29
  try:
30
  t5_tokenizer = AutoTokenizer.from_pretrained(T5_REPO, cache_dir=CACHE_DIR)
@@ -32,9 +24,7 @@ try:
32
  except Exception as e:
33
  raise RuntimeError(f"فشل تحميل نموذج T5 من {T5_REPO}: {str(e)}")
34
 
35
- # -------------------------------------------------
36
- # 2️⃣ تحميل ملف Mistral .gguf
37
- # -------------------------------------------------
38
  gguf_path = os.path.abspath("models/mistral-7b-instruct-v0.1.Q4_K_M.gguf")
39
  if not os.path.exists(gguf_path):
40
  raise RuntimeError(
@@ -52,23 +42,18 @@ try:
52
  except Exception as e:
53
  raise RuntimeError(f"فشل تحميل نموذج Mistral من {gguf_path}: {str(e)}")
54
 
55
- # -------------------------------------------------
56
  # تعريف شكل الطلب (JSON)
57
- # -------------------------------------------------
58
  class AskRequest(BaseModel):
59
  question: str
60
  max_new_tokens: int = 150
61
 
62
- # -------------------------------------------------
63
  # نقطة النهاية /ask
64
- # -------------------------------------------------
65
  @app.post("/ask")
66
  def ask(req: AskRequest):
67
  q = req.question.strip()
68
  if not q:
69
  raise HTTPException(status_code=400, detail="Empty question")
70
 
71
- # منطق اختيار النموذج
72
  try:
73
  if any(tok in q.lower() for tok in ["mgzon", "flan", "t5"]):
74
  # نموذج T5
 
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
  from llama_cpp import Llama
6
 
 
7
  # إعداد مسار الـ cache
8
+ CACHE_DIR = os.environ.get("HF_HOME", "/app/.cache/huggingface")
 
 
9
 
10
  # تأكد من أن المكتبتين تقرأ المتغيّرات البيئية
 
11
  os.environ["HF_HOME"] = CACHE_DIR
12
 
 
13
  # إنشاء التطبيق
 
14
  app = FastAPI(
15
  title="MGZON Smart Assistant",
16
  description="دمج نموذج T5 المدرب مع Mistral‑7B (GGUF) داخل Space"
17
  )
18
 
19
+ # تحميل نموذج T5 المدرب من Hub
 
 
20
  T5_REPO = "MGZON/mgzon-flan-t5-base"
21
  try:
22
  t5_tokenizer = AutoTokenizer.from_pretrained(T5_REPO, cache_dir=CACHE_DIR)
 
24
  except Exception as e:
25
  raise RuntimeError(f"فشل تحميل نموذج T5 من {T5_REPO}: {str(e)}")
26
 
27
+ # تحميل ملف Mistral .gguf
 
 
28
  gguf_path = os.path.abspath("models/mistral-7b-instruct-v0.1.Q4_K_M.gguf")
29
  if not os.path.exists(gguf_path):
30
  raise RuntimeError(
 
42
  except Exception as e:
43
  raise RuntimeError(f"فشل تحميل نموذج Mistral من {gguf_path}: {str(e)}")
44
 
 
45
  # تعريف شكل الطلب (JSON)
 
46
  class AskRequest(BaseModel):
47
  question: str
48
  max_new_tokens: int = 150
49
 
 
50
  # نقطة النهاية /ask
 
51
  @app.post("/ask")
52
  def ask(req: AskRequest):
53
  q = req.question.strip()
54
  if not q:
55
  raise HTTPException(status_code=400, detail="Empty question")
56
 
 
57
  try:
58
  if any(tok in q.lower() for tok in ["mgzon", "flan", "t5"]):
59
  # نموذج T5
setup.sh CHANGED
@@ -8,6 +8,7 @@ fi
8
 
9
  # إنشاء مجلد لتخزين النموذج
10
  mkdir -p models
 
11
 
12
  # تحميل ملف .gguf إذا لم يكن موجودًا مسبقًا
13
  python - <<PY
 
8
 
9
  # إنشاء مجلد لتخزين النموذج
10
  mkdir -p models
11
+ chown -R appuser:appuser models
12
 
13
  # تحميل ملف .gguf إذا لم يكن موجودًا مسبقًا
14
  python - <<PY