File size: 1,520 Bytes
483b3b0 61b2eb7 483b3b0 61b2eb7 483b3b0 61b2eb7 483b3b0 61b2eb7 483b3b0 | 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 | # Dans app.py — section de chargement du modèle
print("⏳ Chargement du modèle Mistral-7B...")
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
model_name = "mistralai/Mistral-7B-Instruct-v0.3"
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Détecter si GPU disponible
use_gpu = torch.cuda.is_available()
print(f"{'✅ GPU détecté.' if use_gpu else '⚠️ Aucun GPU — utilisation du CPU (plus lent).'}")
if use_gpu:
# Charger en 4-bit si GPU
from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16
)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True
)
device = 0
else:
# Charger en FP16/FP32 sur CPU (nécessite ~14 Go de RAM)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16, # ou torch.float32 si mémoire insuffisante
low_cpu_mem_usage=True,
trust_remote_code=True
)
device = -1
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=512,
temperature=0.3,
top_p=0.95,
repetition_penalty=1.2,
device=device
)
def llm_generate(prompt: str) -> str:
response = pipe(prompt)
return response[0]["generated_text"].replace(prompt, "").strip() |