Spaces:
Sleeping
Sleeping
Update models.py
Browse files
models.py
CHANGED
@@ -7,18 +7,23 @@ from config import EMBEDDING_MODEL_NAME
|
|
7 |
|
8 |
# Cargar el modelo de embeddings
|
9 |
def load_embedding_model():
|
10 |
-
#
|
11 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
device = torch.device(device_str)
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
print(f"Embedding model loaded on device: {embedding_model.device}")
|
17 |
return embedding_model
|
18 |
|
19 |
-
# Cargar el modelo Yi-Coder
|
20 |
def load_yi_coder_model():
|
21 |
-
# CORRECTO: Se determina el dispositivo y se convierte a un objeto torch.device.
|
22 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
23 |
device = torch.device(device_str)
|
24 |
|
@@ -26,13 +31,13 @@ def load_yi_coder_model():
|
|
26 |
|
27 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
28 |
|
29 |
-
# El modelo se carga y luego se envía al dispositivo correcto usando .to(device).
|
30 |
yi_coder_model = AutoModelForCausalLM.from_pretrained(
|
31 |
model_path,
|
32 |
torch_dtype=torch.float16,
|
33 |
-
low_cpu_mem_usage=True
|
|
|
|
|
34 |
).to(device).eval()
|
35 |
|
36 |
print(f"Yi-Coder model loaded on device: {yi_coder_model.device}")
|
37 |
-
# Se devuelve el objeto device para ser usado en otras partes de la app.
|
38 |
return tokenizer, yi_coder_model, device
|
|
|
7 |
|
8 |
# Cargar el modelo de embeddings
|
9 |
def load_embedding_model():
|
10 |
+
# Se determina el dispositivo y se convierte a un objeto torch.device.
|
11 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
device = torch.device(device_str)
|
13 |
|
14 |
+
# LA CORRECCIÓN: Añadir el parámetro 'use_safetensors=True'
|
15 |
+
# Esto fuerza a la librería a cargar 'model.safetensors' en lugar de 'pytorch_model.bin'.
|
16 |
+
embedding_model = SentenceTransformer(
|
17 |
+
EMBEDDING_MODEL_NAME,
|
18 |
+
device=device,
|
19 |
+
use_safetensors=True
|
20 |
+
)
|
21 |
+
|
22 |
print(f"Embedding model loaded on device: {embedding_model.device}")
|
23 |
return embedding_model
|
24 |
|
25 |
+
# Cargar el modelo Yi-Coder (esta función ya estaba bien)
|
26 |
def load_yi_coder_model():
|
|
|
27 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
28 |
device = torch.device(device_str)
|
29 |
|
|
|
31 |
|
32 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
33 |
|
|
|
34 |
yi_coder_model = AutoModelForCausalLM.from_pretrained(
|
35 |
model_path,
|
36 |
torch_dtype=torch.float16,
|
37 |
+
low_cpu_mem_usage=True,
|
38 |
+
# También es buena práctica añadirlo aquí, aunque no era la causa del error.
|
39 |
+
use_safetensors=True
|
40 |
).to(device).eval()
|
41 |
|
42 |
print(f"Yi-Coder model loaded on device: {yi_coder_model.device}")
|
|
|
43 |
return tokenizer, yi_coder_model, device
|