Spaces:
Runtime error
Runtime error
cryptocalypse
commited on
Commit
•
b3a2703
1
Parent(s):
4669527
model manager
Browse files- lib/models.py +98 -0
lib/models.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pipes
|
2 |
+
|
3 |
+
class ModelManager:
|
4 |
+
def __init__(self):
|
5 |
+
self.models = {} # Un diccionario para almacenar los modelos disponibles
|
6 |
+
|
7 |
+
def list_models(self):
|
8 |
+
return list(self.models.keys())
|
9 |
+
|
10 |
+
def add_model(self, pipe_func, model_name, args):
|
11 |
+
self.models[model_name] = {"pipeline": pipe_func, "args": args}
|
12 |
+
|
13 |
+
def load_transformers_model(self, model_name, args):
|
14 |
+
if hasattr(pipes, model_name):
|
15 |
+
pipe_func = getattr(pipes, model_name)
|
16 |
+
self.add_model(pipe_func, model_name, args)
|
17 |
+
else:
|
18 |
+
print(f"Error: {model_name} no está definido en el módulo pipes.")
|
19 |
+
|
20 |
+
def train_transformers_model(self, model_name, train_dataset, eval_dataset, training_args):
|
21 |
+
if model_name not in self.models:
|
22 |
+
print(f"Error: {model_name} no está en la lista de modelos disponibles.")
|
23 |
+
return
|
24 |
+
|
25 |
+
pipeline = self.models[model_name]["pipeline"]
|
26 |
+
pipeline.train(train_dataset=train_dataset, eval_dataset=eval_dataset, training_args=training_args)
|
27 |
+
|
28 |
+
def test_model(self, model_name, test_dataset):
|
29 |
+
if model_name not in self.models:
|
30 |
+
print(f"Error: {model_name} no está en la lista de modelos disponibles.")
|
31 |
+
return
|
32 |
+
|
33 |
+
pipeline = self.models[model_name]["pipeline"]
|
34 |
+
return pipeline.test(test_dataset)
|
35 |
+
|
36 |
+
def remove_model(self, model_name):
|
37 |
+
if model_name in self.models:
|
38 |
+
del self.models[model_name]
|
39 |
+
else:
|
40 |
+
print(f"Error: {model_name} no está en la lista de modelos disponibles.")
|
41 |
+
|
42 |
+
def execute_model(self, model_name, *args, **kwargs):
|
43 |
+
if model_name not in self.models:
|
44 |
+
print(f"Error: {model_name} no está en la lista de modelos disponibles.")
|
45 |
+
return None
|
46 |
+
|
47 |
+
pipe_func = self.models[model_name]["pipeline"]
|
48 |
+
args = self.models[model_name]["args"]
|
49 |
+
return pipe_func(*args, **kwargs)
|
50 |
+
|
51 |
+
def choose_best_pipeline(self, prompt, task):
|
52 |
+
available_pipelines = self.models.keys()
|
53 |
+
best_pipeline = None
|
54 |
+
best_score = float('-inf')
|
55 |
+
|
56 |
+
for pipeline_name in available_pipelines:
|
57 |
+
pipeline = self.models[pipeline_name]["pipeline"]
|
58 |
+
score = self.evaluate_pipeline(pipeline, prompt, task)
|
59 |
+
if score > best_score:
|
60 |
+
best_score = score
|
61 |
+
best_pipeline = pipeline_name
|
62 |
+
|
63 |
+
return best_pipeline
|
64 |
+
|
65 |
+
def evaluate_pipeline(self, pipeline, prompt, task):
|
66 |
+
# Aquí puedes implementar la lógica para evaluar qué pipeline es mejor para la tarea específica
|
67 |
+
# En este ejemplo, utilizamos la métrica de exactitud para el análisis de sentimiento
|
68 |
+
if task == "sentiment_analysis":
|
69 |
+
# Supongamos que test_dataset contiene pares de (texto, etiqueta) para análisis de sentimiento
|
70 |
+
test_dataset = [("Texto de prueba 1", "positivo"), ("Texto de prueba 2", "negativo")]
|
71 |
+
correct_predictions = 0
|
72 |
+
total_predictions = len(test_dataset)
|
73 |
+
|
74 |
+
for text, label in test_dataset:
|
75 |
+
prediction = pipeline(text)
|
76 |
+
if prediction == label:
|
77 |
+
correct_predictions += 1
|
78 |
+
|
79 |
+
accuracy = correct_predictions / total_predictions
|
80 |
+
return accuracy
|
81 |
+
else:
|
82 |
+
# Implementa la lógica de evaluación para otras tareas aquí
|
83 |
+
return 0.5 # Por ahora, retornamos un valor de evaluación arbitrario
|
84 |
+
|
85 |
+
# Ejemplo de uso
|
86 |
+
if __name__ == "__main__":
|
87 |
+
manager = ModelManager()
|
88 |
+
|
89 |
+
# Añadir pipelines
|
90 |
+
manager.load_transformers_model("sentiment_tags", args={})
|
91 |
+
manager.load_transformers_model("entity_pos_tagger", args={})
|
92 |
+
|
93 |
+
# Decidir qué pipeline usar para el análisis de sentimiento
|
94 |
+
prompt = "Este es un texto de ejemplo para analizar el sentimiento."
|
95 |
+
task = "sentiment_analysis"
|
96 |
+
best_pipeline = manager.choose_best_pipeline(prompt, task)
|
97 |
+
print(f"La mejor pipa para {task} es: {best_pipeline}")
|
98 |
+
|