File size: 7,383 Bytes
c38370d 1b2d2f4 c38370d 3eb1c78 c38370d 3eb1c78 c38370d 9ff9768 c38370d f1da700 c38370d f1da700 c38370d 1b2d2f4 c38370d 6cabdcf 1b2d2f4 6cabdcf c38370d 6cabdcf c38370d ab7c792 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | from fastapi import FastAPI, HTTPException, Query
from contextlib import asynccontextmanager
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
from huggingface_hub import snapshot_download
from typing import Optional
import numpy as np
import os
import shutil
import requests
import zipfile
import tempfile
# Constantes
DEVICE = "cuda" if os.environ.get("USE_CUDA", "0") == "1" else "cpu"
MODELS_DIR = "./models"
DEFAULT_MODEL_NAME = os.path.join(MODELS_DIR, "esti-rag-ft")
app = FastAPI()
# Singleton model
default_model: SentenceTransformer = None
current_model_path: str = DEFAULT_MODEL_NAME
def load_model(model_path: str) -> SentenceTransformer:
print(f"🧠 Chargement SentenceTransformer depuis : {model_path}")
return SentenceTransformer(model_path, device=DEVICE)
# Constantes
DEVICE = "cuda" if os.environ.get("USE_CUDA", "0") == "1" else "cpu"
MODELS_DIR = "./models"
DEFAULT_MODEL_NAME = os.path.join(MODELS_DIR, "esti-rag-ft")
# Globales
default_model: SentenceTransformer = None
current_model_path: str = DEFAULT_MODEL_NAME
def load_model_by_url(url: str, version: str) -> SentenceTransformer:
"""
Télécharge un modèle zippé depuis une URL, l'extrait dans ./models/{version},
puis le charge avec SentenceTransformer.
"""
with tempfile.TemporaryDirectory() as tmpdir:
zip_path = os.path.join(tmpdir, "model.zip")
# Téléchargement
print(f"⬇️ Téléchargement du modèle depuis {url}")
r = requests.get(url)
r.raise_for_status()
with open(zip_path, "wb") as f:
f.write(r.content)
# Extraction
target_dir = os.path.join(MODELS_DIR, version)
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
os.makedirs(target_dir, exist_ok=True)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(target_dir)
print(f"✅ Modèle extrait dans {target_dir}")
# Chargement
model = SentenceTransformer(target_dir, device=DEVICE)
return model
@asynccontextmanager
async def lifespan(app: FastAPI):
global default_model, current_model_path
if os.path.exists(DEFAULT_MODEL_NAME):
default_model = load_model(DEFAULT_MODEL_NAME)
current_model_path = DEFAULT_MODEL_NAME
else:
fallback_model = "sentence-transformers/all-MiniLM-L6-v2"
print(f"⚠️ Modèle {DEFAULT_MODEL_NAME} introuvable. Utilisation du modèle par défaut HuggingFace : {fallback_model}")
default_model = load_model(fallback_model)
current_model_path = fallback_model
yield
app = FastAPI(lifespan=lifespan)
class TextRequest(BaseModel):
texts: list[str]
model: Optional[str] = ""
@app.get("/")
def greet_json():
return {"Hello": "World!"}
@app.post("/embed")
def get_embedding(req: TextRequest):
try:
if req.model:
model = load_model(req.model)
embeddings = model.encode(req.texts, convert_to_numpy=True, normalize_embeddings=True)
else:
embeddings = default_model.encode(req.texts, convert_to_numpy=True, normalize_embeddings=True)
return {"embeddings": embeddings.tolist()}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/reload-model")
def reload_model(version: Optional[str] = Query(default=None), url: Optional[str] = Query(default=None)):
"""
Recharger dynamiquement le modèle par défaut.
- Si 'version' est fourni, copie models/{version} → models/esti-rag-ft
- Si 'url' est une archive zip → télécharge et extrait
- Si 'url' est une URL Hugging Face → télécharge le modèle via snapshot_download
"""
global default_model, current_model_path
try:
if url and version:
print(f"🔁 Téléchargement du modèle depuis URL : {url} → models/esti-rag-ft")
extract_dir = os.path.join(MODELS_DIR, "esti-rag-ft")
if os.path.exists(extract_dir):
shutil.rmtree(extract_dir)
os.makedirs(extract_dir, exist_ok=True)
if "huggingface.co" in url:
# ✅ Cas Hugging Face → utiliser snapshot_download
parts = url.split("huggingface.co/")[1].split("/")
if len(parts) >= 2:
repo_id = f"{parts[0]}/{parts[1]}"
subfolder = "/".join(parts[4:]) if "tree" in parts else ""
with tempfile.TemporaryDirectory() as tmp_download_dir:
model_path = snapshot_download(
repo_id=repo_id,
allow_patterns=[f"{subfolder}/*"] if subfolder else None,
local_dir=tmp_download_dir,
local_dir_use_symlinks=False
)
sub_model_path = os.path.join(model_path, subfolder) if subfolder else model_path
for item in os.listdir(sub_model_path):
s = os.path.join(sub_model_path, item)
d = os.path.join(extract_dir, item)
if os.path.isdir(s):
shutil.copytree(s, d)
else:
shutil.copy2(s, d)
else:
raise HTTPException(status_code=400, detail="❌ URL Hugging Face invalide.")
else:
# 📦 Cas archive ZIP classique
with tempfile.TemporaryDirectory() as tmpdir:
zip_path = os.path.join(tmpdir, "model.zip")
r = requests.get(url)
r.raise_for_status()
with open(zip_path, "wb") as f:
f.write(r.content)
try:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
except zipfile.BadZipFile:
raise HTTPException(status_code=400, detail="❌ Le fichier téléchargé n’est pas une archive ZIP valide.")
# ✅ Charger le modèle
default_model = load_model(extract_dir)
current_model_path = extract_dir
return {"status": "success", "model_path": extract_dir}
elif version:
source_path = os.path.join(MODELS_DIR, version)
if not os.path.exists(source_path):
raise FileNotFoundError(f"Modèle {version} introuvable à {source_path}")
if os.path.exists(DEFAULT_MODEL_NAME):
shutil.rmtree(DEFAULT_MODEL_NAME)
shutil.copytree(src=source_path, dst=DEFAULT_MODEL_NAME)
default_model = load_model(DEFAULT_MODEL_NAME)
current_model_path = DEFAULT_MODEL_NAME
return {"status": "success", "model_path": DEFAULT_MODEL_NAME}
else:
raise ValueError("Il faut fournir à la fois 'version' et 'url' pour téléchargement, ou juste 'version'.")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erreur rechargement modèle: {str(e)}")
@app.get("/status")
def status():
return {
"device": DEVICE,
"model_path": current_model_path
}
|