Update interface.py
Browse files- interface.py +41 -101
interface.py
CHANGED
@@ -1,36 +1,35 @@
|
|
1 |
# interface.py
|
2 |
|
3 |
-
|
4 |
-
import
|
5 |
-
|
|
|
|
|
|
|
6 |
import pandas as pd
|
7 |
import numpy as np
|
8 |
import matplotlib.pyplot as plt
|
9 |
-
import
|
10 |
-
|
11 |
-
from sympy import symbols,
|
12 |
-
import copy
|
13 |
-
from config import DEVICE, MODEL_PATH, MAX_LENGTH, TEMPERATURE
|
14 |
-
from decorators import spaces
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
|
19 |
-
# Cargar el modelo
|
20 |
-
model_path = MODEL_PATH
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
22 |
model = AutoModelForCausalLM.from_pretrained(model_path)
|
23 |
-
# No movemos el modelo al dispositivo aquí
|
24 |
-
|
25 |
-
from decorators import spaces
|
26 |
|
27 |
-
|
|
|
28 |
def generate_analysis(prompt, max_length=1024, device=None):
|
29 |
try:
|
|
|
30 |
if device is None:
|
31 |
device = torch.device('cpu')
|
32 |
|
33 |
-
# Mover el modelo al dispositivo adecuado (GPU o CPU)
|
34 |
if next(model.parameters()).device != device:
|
35 |
model.to(device)
|
36 |
|
@@ -48,6 +47,7 @@ def generate_analysis(prompt, max_length=1024, device=None):
|
|
48 |
early_stopping=True
|
49 |
)
|
50 |
|
|
|
51 |
output_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
52 |
analysis = output_text[len(prompt):].strip()
|
53 |
return analysis
|
@@ -68,7 +68,7 @@ def parse_bounds(bounds_str, num_params):
|
|
68 |
lower_bounds = [-np.inf] * num_params
|
69 |
upper_bounds = [np.inf] * num_params
|
70 |
return lower_bounds, upper_bounds
|
71 |
-
|
72 |
def process_and_plot(
|
73 |
file,
|
74 |
biomass_eq1, biomass_eq2, biomass_eq3,
|
@@ -85,20 +85,23 @@ def process_and_plot(
|
|
85 |
show_params,
|
86 |
biomass_eq_count,
|
87 |
substrate_eq_count,
|
88 |
-
product_eq_count
|
|
|
89 |
):
|
90 |
-
#
|
91 |
df = pd.read_excel(file.name)
|
92 |
-
|
|
|
|
|
93 |
for col in expected_columns:
|
94 |
if col not in df.columns:
|
95 |
raise KeyError(f"La columna esperada '{col}' no se encuentra en el archivo Excel.")
|
96 |
|
97 |
-
#
|
98 |
-
time = df['Tiempo'].values
|
99 |
-
biomass_data = df['Biomasa'].values
|
100 |
-
substrate_data = df['Sustrato'].values
|
101 |
-
product_data = df['Producto'].values
|
102 |
|
103 |
# Convierte los contadores a enteros
|
104 |
biomass_eq_count = int(biomass_eq_count)
|
@@ -139,7 +142,7 @@ def process_and_plot(
|
|
139 |
bounds=(lower_bounds, upper_bounds)
|
140 |
)
|
141 |
biomass_results.append({
|
142 |
-
'model':
|
143 |
'y_pred': y_pred,
|
144 |
'equation': equation
|
145 |
})
|
@@ -179,7 +182,7 @@ def process_and_plot(
|
|
179 |
bounds=(lower_bounds, upper_bounds)
|
180 |
)
|
181 |
substrate_results.append({
|
182 |
-
'model':
|
183 |
'y_pred': y_pred,
|
184 |
'equation': equation
|
185 |
})
|
@@ -214,7 +217,7 @@ def process_and_plot(
|
|
214 |
bounds=(lower_bounds, upper_bounds)
|
215 |
)
|
216 |
product_results.append({
|
217 |
-
'model':
|
218 |
'y_pred': y_pred,
|
219 |
'equation': equation
|
220 |
})
|
@@ -255,83 +258,20 @@ def process_and_plot(
|
|
255 |
buf.seek(0)
|
256 |
image = Image.open(buf)
|
257 |
|
258 |
-
all_results = {
|
259 |
-
'biomass_models': [],
|
260 |
-
'substrate_models': [],
|
261 |
-
'product_models': []
|
262 |
-
}
|
263 |
-
|
264 |
-
for i, result in enumerate(biomass_results):
|
265 |
-
model_info = {
|
266 |
-
'model_number': i + 1,
|
267 |
-
'equation': result['equation'],
|
268 |
-
'parameters': result['model'].params['biomass'],
|
269 |
-
'R2': result['model'].r2['biomass'],
|
270 |
-
'RMSE': result['model'].rmse['biomass']
|
271 |
-
}
|
272 |
-
all_results['biomass_models'].append(model_info)
|
273 |
-
|
274 |
-
for i, result in enumerate(substrate_results):
|
275 |
-
model_info = {
|
276 |
-
'model_number': i + 1,
|
277 |
-
'equation': result['equation'],
|
278 |
-
'parameters': result['model'].params['substrate'],
|
279 |
-
'R2': result['model'].r2['substrate'],
|
280 |
-
'RMSE': result['model'].rmse['substrate']
|
281 |
-
}
|
282 |
-
all_results['substrate_models'].append(model_info)
|
283 |
-
|
284 |
-
for i, result in enumerate(product_results):
|
285 |
-
model_info = {
|
286 |
-
'model_number': i + 1,
|
287 |
-
'equation': result['equation'],
|
288 |
-
'parameters': result['model'].params['product'],
|
289 |
-
'R2': result['model'].r2['product'],
|
290 |
-
'RMSE': result['model'].rmse['product']
|
291 |
-
}
|
292 |
-
all_results['product_models'].append(model_info)
|
293 |
-
|
294 |
-
results_text = "Resultados Experimentales:\n\n"
|
295 |
-
|
296 |
-
results_text += "Modelos de Biomasa:\n"
|
297 |
-
for model_info in all_results['biomass_models']:
|
298 |
-
results_text += f"""
|
299 |
-
Modelo {model_info['model_number']}:
|
300 |
-
Ecuación: {model_info['equation']}
|
301 |
-
Parámetros: {model_info['parameters']}
|
302 |
-
R²: {model_info['R2']:.4f}
|
303 |
-
RMSE: {model_info['RMSE']:.4f}
|
304 |
-
"""
|
305 |
-
|
306 |
-
results_text += "\nModelos de Sustrato:\n"
|
307 |
-
for model_info in all_results['substrate_models']:
|
308 |
-
results_text += f"""
|
309 |
-
Modelo {model_info['model_number']}:
|
310 |
-
Ecuación: {model_info['equation']}
|
311 |
-
Parámetros: {model_info['parameters']}
|
312 |
-
R²: {model_info['R2']:.4f}
|
313 |
-
RMSE: {model_info['RMSE']:.4f}
|
314 |
-
"""
|
315 |
-
|
316 |
-
results_text += "\nModelos de Producto:\n"
|
317 |
-
for model_info in all_results['product_models']:
|
318 |
-
results_text += f"""
|
319 |
-
Modelo {model_info['model_number']}:
|
320 |
-
Ecuación: {model_info['equation']}
|
321 |
-
Parámetros: {model_info['parameters']}
|
322 |
-
R²: {model_info['R2']:.4f}
|
323 |
-
RMSE: {model_info['RMSE']:.4f}
|
324 |
-
"""
|
325 |
-
|
326 |
prompt = f"""
|
327 |
Eres un experto en modelado de bioprocesos.
|
328 |
|
329 |
Analiza los siguientes resultados experimentales y proporciona un veredicto sobre la calidad de los modelos, sugiriendo mejoras si es necesario.
|
330 |
|
331 |
-
|
|
|
|
|
|
|
|
|
332 |
|
333 |
-
|
|
|
334 |
"""
|
335 |
-
analysis = generate_analysis(prompt)
|
336 |
|
337 |
return [image], analysis
|
|
|
1 |
# interface.py
|
2 |
|
3 |
+
# Importar 'spaces' y decoradores antes que cualquier biblioteca que pueda inicializar CUDA
|
4 |
+
from decorators import gpu_decorator
|
5 |
+
|
6 |
+
# Luego importar cualquier cosa relacionada con PyTorch o el modelo que va a usar la GPU
|
7 |
+
import torch
|
8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
import pandas as pd
|
10 |
import numpy as np
|
11 |
import matplotlib.pyplot as plt
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
+
from sympy import symbols, lambdify, sympify
|
|
|
|
|
|
|
15 |
|
16 |
+
# Importar otras partes necesarias del código (config, etc.)
|
17 |
+
from config import DEVICE, MODEL_PATH, MAX_LENGTH, TEMPERATURE
|
18 |
|
19 |
+
# Cargar el modelo fuera de la función para evitar la inicialización innecesaria cada vez que se llame a la función
|
20 |
+
model_path = MODEL_PATH
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
22 |
model = AutoModelForCausalLM.from_pretrained(model_path)
|
|
|
|
|
|
|
23 |
|
24 |
+
# Decorador GPU aplicado para manejar la ejecución en GPU si está disponible
|
25 |
+
@gpu_decorator(duration=300)
|
26 |
def generate_analysis(prompt, max_length=1024, device=None):
|
27 |
try:
|
28 |
+
# Si el dispositivo no se especifica, usa CPU por defecto
|
29 |
if device is None:
|
30 |
device = torch.device('cpu')
|
31 |
|
32 |
+
# Mover el modelo al dispositivo adecuado (GPU o CPU) si es necesario
|
33 |
if next(model.parameters()).device != device:
|
34 |
model.to(device)
|
35 |
|
|
|
47 |
early_stopping=True
|
48 |
)
|
49 |
|
50 |
+
# Decodificar la respuesta generada
|
51 |
output_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
52 |
analysis = output_text[len(prompt):].strip()
|
53 |
return analysis
|
|
|
68 |
lower_bounds = [-np.inf] * num_params
|
69 |
upper_bounds = [np.inf] * num_params
|
70 |
return lower_bounds, upper_bounds
|
71 |
+
|
72 |
def process_and_plot(
|
73 |
file,
|
74 |
biomass_eq1, biomass_eq2, biomass_eq3,
|
|
|
85 |
show_params,
|
86 |
biomass_eq_count,
|
87 |
substrate_eq_count,
|
88 |
+
product_eq_count,
|
89 |
+
device=None
|
90 |
):
|
91 |
+
# Leer el archivo Excel
|
92 |
df = pd.read_excel(file.name)
|
93 |
+
|
94 |
+
# Verificar que las columnas necesarias estén presentes
|
95 |
+
expected_columns = ['Tiempo', 'Biomasa', 'Sustrato', 'Producto']
|
96 |
for col in expected_columns:
|
97 |
if col not in df.columns:
|
98 |
raise KeyError(f"La columna esperada '{col}' no se encuentra en el archivo Excel.")
|
99 |
|
100 |
+
# Asignar los datos desde las columnas
|
101 |
+
time = df['Tiempo'].values
|
102 |
+
biomass_data = df['Biomasa'].values
|
103 |
+
substrate_data = df['Sustrato'].values
|
104 |
+
product_data = df['Producto'].values
|
105 |
|
106 |
# Convierte los contadores a enteros
|
107 |
biomass_eq_count = int(biomass_eq_count)
|
|
|
142 |
bounds=(lower_bounds, upper_bounds)
|
143 |
)
|
144 |
biomass_results.append({
|
145 |
+
'model': model,
|
146 |
'y_pred': y_pred,
|
147 |
'equation': equation
|
148 |
})
|
|
|
182 |
bounds=(lower_bounds, upper_bounds)
|
183 |
)
|
184 |
substrate_results.append({
|
185 |
+
'model': model,
|
186 |
'y_pred': y_pred,
|
187 |
'equation': equation
|
188 |
})
|
|
|
217 |
bounds=(lower_bounds, upper_bounds)
|
218 |
)
|
219 |
product_results.append({
|
220 |
+
'model': model,
|
221 |
'y_pred': y_pred,
|
222 |
'equation': equation
|
223 |
})
|
|
|
258 |
buf.seek(0)
|
259 |
image = Image.open(buf)
|
260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
prompt = f"""
|
262 |
Eres un experto en modelado de bioprocesos.
|
263 |
|
264 |
Analiza los siguientes resultados experimentales y proporciona un veredicto sobre la calidad de los modelos, sugiriendo mejoras si es necesario.
|
265 |
|
266 |
+
Biomasa:
|
267 |
+
{biomass_results}
|
268 |
+
|
269 |
+
Sustrato:
|
270 |
+
{substrate_results}
|
271 |
|
272 |
+
Producto:
|
273 |
+
{product_results}
|
274 |
"""
|
275 |
+
analysis = generate_analysis(prompt, device=device)
|
276 |
|
277 |
return [image], analysis
|