Spaces:
Sleeping
Sleeping
Update models.py
Browse files
models.py
CHANGED
@@ -1,37 +1,45 @@
|
|
1 |
-
# models.py (
|
2 |
|
3 |
import torch
|
4 |
-
import
|
|
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
from config import EMBEDDING_MODEL_NAME
|
8 |
|
9 |
-
# ======================= C脫DIGO DE DIAGN脫STICO =======================
|
10 |
-
# Estas l铆neas nos dir谩n la verdad sobre tu entorno.
|
11 |
-
print("--- INICIANDO DIAGN脫STICO DE VERSIONES ---")
|
12 |
-
print(f"--- Versi贸n de Sentence-Transformers: {sentence_transformers.__version__}")
|
13 |
-
print(f"--- Versi贸n de PyTorch: {torch.__version__}")
|
14 |
-
print("------------------------------------------")
|
15 |
-
# ====================================================================
|
16 |
-
|
17 |
# Cargar el modelo de embeddings
|
18 |
def load_embedding_model():
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
21 |
device = torch.device(device_str)
|
22 |
|
23 |
embedding_model = SentenceTransformer(
|
24 |
-
|
25 |
-
device=device
|
26 |
-
use_safetensors=True
|
27 |
)
|
28 |
|
29 |
-
print(f"
|
30 |
return embedding_model
|
31 |
|
32 |
-
# Cargar el modelo Yi-Coder
|
33 |
def load_yi_coder_model():
|
34 |
-
# ... el resto de tu c贸digo se mantiene igual ...
|
35 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
36 |
device = torch.device(device_str)
|
37 |
|
@@ -39,12 +47,12 @@ def load_yi_coder_model():
|
|
39 |
|
40 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
41 |
|
|
|
42 |
yi_coder_model = AutoModelForCausalLM.from_pretrained(
|
43 |
model_path,
|
44 |
torch_dtype=torch.float16,
|
45 |
-
low_cpu_mem_usage=True
|
46 |
-
use_safetensors=True
|
47 |
).to(device).eval()
|
48 |
|
49 |
-
print(f"Yi-Coder
|
50 |
return tokenizer, yi_coder_model, device
|
|
|
1 |
+
# models.py (VERSI脫N A PRUEBA DE ENTORNO ROTO)
|
2 |
|
3 |
import torch
|
4 |
+
import os
|
5 |
+
from huggingface_hub import snapshot_download # Importaci贸n clave
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
from config import EMBEDDING_MODEL_NAME
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Cargar el modelo de embeddings
|
11 |
def load_embedding_model():
|
12 |
+
print("--- Iniciando carga manual del modelo de embedding ---")
|
13 |
+
|
14 |
+
# 1. Descargar los archivos del modelo a una cach茅 local y obtener la ruta
|
15 |
+
model_folder = snapshot_download(repo_id=EMBEDDING_MODEL_NAME)
|
16 |
+
print(f"Modelo descargado en: {model_folder}")
|
17 |
+
|
18 |
+
# 2. Construir la ruta al archivo problem谩tico
|
19 |
+
problematic_file_path = os.path.join(model_folder, "pytorch_model.bin")
|
20 |
+
|
21 |
+
# 3. Eliminar el archivo .bin si existe, para forzar el uso de .safetensors
|
22 |
+
if os.path.exists(problematic_file_path):
|
23 |
+
print(f"Eliminando archivo problem谩tico: {problematic_file_path}")
|
24 |
+
os.remove(problematic_file_path)
|
25 |
+
else:
|
26 |
+
print("El archivo pytorch_model.bin no existe, se proceder谩 con safetensors.")
|
27 |
+
|
28 |
+
# 4. Cargar el modelo desde la carpeta local ya "limpia"
|
29 |
+
# Se quita el argumento 'use_safetensors' porque ya no es necesario.
|
30 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
31 |
device = torch.device(device_str)
|
32 |
|
33 |
embedding_model = SentenceTransformer(
|
34 |
+
model_folder, # Cargar desde la ruta local
|
35 |
+
device=device
|
|
|
36 |
)
|
37 |
|
38 |
+
print(f"Modelo de embedding cargado exitosamente desde la ruta local en el dispositivo: {embedding_model.device}")
|
39 |
return embedding_model
|
40 |
|
41 |
+
# Cargar el modelo Yi-Coder (se simplifica para consistencia)
|
42 |
def load_yi_coder_model():
|
|
|
43 |
device_str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
44 |
device = torch.device(device_str)
|
45 |
|
|
|
47 |
|
48 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
49 |
|
50 |
+
# Se quita 'use_safetensors' para evitar cualquier posible conflicto.
|
51 |
yi_coder_model = AutoModelForCausalLM.from_pretrained(
|
52 |
model_path,
|
53 |
torch_dtype=torch.float16,
|
54 |
+
low_cpu_mem_usage=True
|
|
|
55 |
).to(device).eval()
|
56 |
|
57 |
+
print(f"Modelo Yi-Coder cargado en el dispositivo: {yi_coder_model.device}")
|
58 |
return tokenizer, yi_coder_model, device
|