Update inference.py
Browse files- inference.py +23 -14
inference.py
CHANGED
@@ -1,28 +1,37 @@
|
|
1 |
import joblib
|
2 |
import numpy as np
|
3 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
|
5 |
-
# Cargar el vectorizador
|
6 |
-
|
7 |
-
vectorizer
|
|
|
|
|
|
|
8 |
|
9 |
-
# Cargar el modelo
|
10 |
-
|
11 |
-
model
|
|
|
|
|
|
|
12 |
|
13 |
def preprocess(text):
|
14 |
"""Preprocesa el texto para la inferencia."""
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
def predict(text):
|
19 |
"""Realiza la predicción a partir del texto ingresado."""
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
if __name__ == "__main__":
|
25 |
-
#
|
26 |
-
test_text = "
|
27 |
result = predict(test_text)
|
28 |
print(f"Predicción: {result}")
|
|
|
1 |
import joblib
|
2 |
import numpy as np
|
|
|
3 |
|
4 |
+
# Cargar el vectorizador
|
5 |
+
try:
|
6 |
+
with open('vectorizer.pkl', 'rb') as f:
|
7 |
+
vectorizer = joblib.load(f)
|
8 |
+
except Exception as e:
|
9 |
+
print(f"Error al cargar el vectorizador: {e}")
|
10 |
|
11 |
+
# Cargar el modelo
|
12 |
+
try:
|
13 |
+
with open('model.pkl', 'rb') as f:
|
14 |
+
model = joblib.load(f)
|
15 |
+
except Exception as e:
|
16 |
+
print(f"Error al cargar el modelo: {e}")
|
17 |
|
18 |
def preprocess(text):
|
19 |
"""Preprocesa el texto para la inferencia."""
|
20 |
+
try:
|
21 |
+
return vectorizer.transform([text])
|
22 |
+
except Exception as e:
|
23 |
+
print(f"Error en el preprocesamiento: {e}")
|
24 |
|
25 |
def predict(text):
|
26 |
"""Realiza la predicción a partir del texto ingresado."""
|
27 |
+
try:
|
28 |
+
X = preprocess(text)
|
29 |
+
return model.predict(X)
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error en la predicción: {e}")
|
32 |
|
33 |
if __name__ == "__main__":
|
34 |
+
# Prueba del modelo
|
35 |
+
test_text = "Texto de prueba para predecir"
|
36 |
result = predict(test_text)
|
37 |
print(f"Predicción: {result}")
|