Spaces:
Runtime error
Runtime error
Commit ·
c50c307
1
Parent(s): ab3cf88
Speed Memory Usage optimizations
Browse files- Dockerfile +4 -1
- app.py +26 -3
Dockerfile
CHANGED
|
@@ -3,7 +3,10 @@ FROM python:3.10-slim
|
|
| 3 |
# 1) Variables HF avant tout
|
| 4 |
ENV HF_HOME="/home/user/.cache/huggingface" \
|
| 5 |
HF_HUB_CACHE="/home/user/.cache/huggingface/hub" \
|
| 6 |
-
TRANSFORMERS_CACHE="/home/user/.cache/huggingface/transformers"
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# 2) Créer l’utilisateur non-root
|
| 9 |
RUN useradd -m -u 1000 user
|
|
|
|
| 3 |
# 1) Variables HF avant tout
|
| 4 |
ENV HF_HOME="/home/user/.cache/huggingface" \
|
| 5 |
HF_HUB_CACHE="/home/user/.cache/huggingface/hub" \
|
| 6 |
+
TRANSFORMERS_CACHE="/home/user/.cache/huggingface/transformers" \
|
| 7 |
+
DOCLING_ARTIFACTS_PATH="/home/user/.cache/docling/models" \
|
| 8 |
+
OMP_NUM_THREADS=2
|
| 9 |
+
|
| 10 |
|
| 11 |
# 2) Créer l’utilisateur non-root
|
| 12 |
RUN useradd -m -u 1000 user
|
app.py
CHANGED
|
@@ -10,6 +10,8 @@ from dotenv import load_dotenv
|
|
| 10 |
import tempfile
|
| 11 |
from supabase import create_client
|
| 12 |
from huggingface_hub import snapshot_download
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
load_dotenv()
|
|
@@ -37,6 +39,23 @@ print(">>> MODEL CACHE PATH:", MODEL_CACHE, os.listdir(MODEL_CACHE))
|
|
| 37 |
|
| 38 |
device = "gpu" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
| 39 |
dtype = torch.float16 if device in ("mps", "gpu") else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@app.on_event("startup")
|
| 42 |
def startup_supabase():
|
|
@@ -57,17 +76,21 @@ def load_model():
|
|
| 57 |
MODEL_CACHE,
|
| 58 |
local_files_only=True,
|
| 59 |
torch_dtype=dtype,
|
| 60 |
-
trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
| 61 |
).to(device).eval()
|
| 62 |
# tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 63 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 64 |
MODEL_CACHE,
|
| 65 |
local_files_only=True,
|
| 66 |
-
trust_remote_code=True
|
|
|
|
| 67 |
)
|
| 68 |
print("✅ Model and tokenizer loaded from", MODEL_CACHE)
|
| 69 |
|
| 70 |
-
def predict_NuExtract(texts, template, batch_size=
|
| 71 |
print("Starting NuExtract prediction...", flush=True)
|
| 72 |
start_time = time.perf_counter()
|
| 73 |
template_str = json.dumps(json.loads(template), indent=4)
|
|
|
|
| 10 |
import tempfile
|
| 11 |
from supabase import create_client
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
+
from transformers import BitsAndBytesConfig, AutoModelForCausalLM
|
| 14 |
+
|
| 15 |
|
| 16 |
|
| 17 |
load_dotenv()
|
|
|
|
| 39 |
|
| 40 |
device = "gpu" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
| 41 |
dtype = torch.float16 if device in ("mps", "gpu") else torch.float32
|
| 42 |
+
bnb_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# If lower memory usage needed:
|
| 46 |
+
|
| 47 |
+
# bnb_config = BitsAndBytesConfig(
|
| 48 |
+
# load_in_4bit=True,
|
| 49 |
+
# bnb_4bit_use_double_quant=True,
|
| 50 |
+
# bnb_4bit_quant_type="nf4"
|
| 51 |
+
# )
|
| 52 |
+
# model = AutoModelForCausalLM.from_pretrained(
|
| 53 |
+
# MODEL_CACHE,
|
| 54 |
+
# quantization_config=bnb_config,
|
| 55 |
+
# device_map="auto",
|
| 56 |
+
# local_files_only=True,
|
| 57 |
+
# trust_remote_code=True
|
| 58 |
+
# )
|
| 59 |
|
| 60 |
@app.on_event("startup")
|
| 61 |
def startup_supabase():
|
|
|
|
| 76 |
MODEL_CACHE,
|
| 77 |
local_files_only=True,
|
| 78 |
torch_dtype=dtype,
|
| 79 |
+
trust_remote_code=True,
|
| 80 |
+
quantization_config=bnb_config,
|
| 81 |
+
no_split_module_classes=["Block"],
|
| 82 |
+
device_map="auto"
|
| 83 |
).to(device).eval()
|
| 84 |
# tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 85 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 86 |
MODEL_CACHE,
|
| 87 |
local_files_only=True,
|
| 88 |
+
trust_remote_code=True,
|
| 89 |
+
device_map="auto"
|
| 90 |
)
|
| 91 |
print("✅ Model and tokenizer loaded from", MODEL_CACHE)
|
| 92 |
|
| 93 |
+
def predict_NuExtract(texts, template, batch_size=1, max_length=5096, max_new_tokens=1024):
|
| 94 |
print("Starting NuExtract prediction...", flush=True)
|
| 95 |
start_time = time.perf_counter()
|
| 96 |
template_str = json.dumps(json.loads(template), indent=4)
|