Update interface.py
Browse files- interface.py +0 -306
interface.py
CHANGED
|
@@ -1,309 +1,3 @@
|
|
| 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 |
-
###############################
|
| 25 |
-
|
| 26 |
-
# bioprocess_model.py
|
| 27 |
-
|
| 28 |
-
import numpy as np
|
| 29 |
-
import pandas as pd
|
| 30 |
-
import matplotlib.pyplot as plt
|
| 31 |
-
from scipy.integrate import odeint
|
| 32 |
-
from scipy.optimize import curve_fit
|
| 33 |
-
from sklearn.metrics import mean_squared_error
|
| 34 |
-
import seaborn as sns
|
| 35 |
-
from sympy import symbols, lambdify, sympify
|
| 36 |
-
|
| 37 |
-
class BioprocessModel:
|
| 38 |
-
def __init__(self):
|
| 39 |
-
self.params = {}
|
| 40 |
-
self.r2 = {}
|
| 41 |
-
self.rmse = {}
|
| 42 |
-
self.datax = []
|
| 43 |
-
self.datas = []
|
| 44 |
-
self.datap = []
|
| 45 |
-
self.dataxp = []
|
| 46 |
-
self.datasp = []
|
| 47 |
-
self.datapp = []
|
| 48 |
-
self.datax_std = []
|
| 49 |
-
self.datas_std = []
|
| 50 |
-
self.datap_std = []
|
| 51 |
-
self.models = {} # Initialize the models dictionary
|
| 52 |
-
|
| 53 |
-
@staticmethod
|
| 54 |
-
def logistic(time, xo, xm, um):
|
| 55 |
-
return (xo * np.exp(um * time)) / (1 - (xo / xm) * (1 - np.exp(um * time)))
|
| 56 |
-
|
| 57 |
-
@staticmethod
|
| 58 |
-
def substrate(time, so, p, q, xo, xm, um):
|
| 59 |
-
return so - (p * xo * ((np.exp(um * time)) / (1 - (xo / xm) * (1 - np.exp(um * time))) - 1)) - \
|
| 60 |
-
(q * (xm / um) * np.log(1 - (xo / xm) * (1 - np.exp(um * time))))
|
| 61 |
-
|
| 62 |
-
@staticmethod
|
| 63 |
-
def product(time, po, alpha, beta, xo, xm, um):
|
| 64 |
-
return po + (alpha * xo * ((np.exp(um * time) / (1 - (xo / xm) * (1 - np.exp(um * time)))) - 1)) + \
|
| 65 |
-
(beta * (xm / um) * np.log(1 - (xo / xm) * (1 - np.exp(um * time))))
|
| 66 |
-
|
| 67 |
-
@staticmethod
|
| 68 |
-
def logistic_diff(X, t, params):
|
| 69 |
-
xo, xm, um = params
|
| 70 |
-
dXdt = um * X * (1 - X / xm)
|
| 71 |
-
return dXdt
|
| 72 |
-
|
| 73 |
-
def substrate_diff(self, S, t, params, biomass_params, X_func):
|
| 74 |
-
so, p, q = params
|
| 75 |
-
xo, xm, um = biomass_params
|
| 76 |
-
X_t = X_func(t)
|
| 77 |
-
dSdt = -p * (um * X_t * (1 - X_t / xm)) - q * X_t
|
| 78 |
-
return dSdt
|
| 79 |
-
|
| 80 |
-
def product_diff(self, P, t, params, biomass_params, X_func):
|
| 81 |
-
po, alpha, beta = params
|
| 82 |
-
xo, xm, um = biomass_params
|
| 83 |
-
X_t = X_func(t)
|
| 84 |
-
dPdt = alpha * (um * X_t * (1 - X_t / xm)) + beta * X_t
|
| 85 |
-
return dPdt
|
| 86 |
-
|
| 87 |
-
def process_data(self, df):
|
| 88 |
-
biomass_cols = [col for col in df.columns if 'Biomasa' in col]
|
| 89 |
-
substrate_cols = [col for col in df.columns if 'Sustrato' in col]
|
| 90 |
-
product_cols = [col for col in df.columns if 'Producto' in col]
|
| 91 |
-
|
| 92 |
-
time_col = [col for col in df.columns if 'Tiempo' in col][0]
|
| 93 |
-
time = df[time_col].values
|
| 94 |
-
|
| 95 |
-
data_biomass = np.array([df[col].values for col in biomass_cols])
|
| 96 |
-
self.datax.append(data_biomass)
|
| 97 |
-
self.dataxp.append(np.mean(data_biomass, axis=0))
|
| 98 |
-
self.datax_std.append(np.std(data_biomass, axis=0, ddof=1))
|
| 99 |
-
|
| 100 |
-
data_substrate = np.array([df[col].values for col in substrate_cols])
|
| 101 |
-
self.datas.append(data_substrate)
|
| 102 |
-
self.datasp.append(np.mean(data_substrate, axis=0))
|
| 103 |
-
self.datas_std.append(np.std(data_substrate, axis=0, ddof=1))
|
| 104 |
-
|
| 105 |
-
data_product = np.array([df[col].values for col in product_cols])
|
| 106 |
-
self.datap.append(data_product)
|
| 107 |
-
self.datapp.append(np.mean(data_product, axis=0))
|
| 108 |
-
self.datap_std.append(np.std(data_product, axis=0, ddof=1))
|
| 109 |
-
|
| 110 |
-
self.time = time
|
| 111 |
-
|
| 112 |
-
def set_model(self, model_type, equation, params_str):
|
| 113 |
-
"""
|
| 114 |
-
Sets up the model based on the type, equation, and parameters.
|
| 115 |
-
|
| 116 |
-
:param model_type: Type of the model ('biomass', 'substrate', 'product')
|
| 117 |
-
:param equation: The equation as a string
|
| 118 |
-
:param params_str: Comma-separated string of parameter names
|
| 119 |
-
"""
|
| 120 |
-
t_symbol = symbols('t')
|
| 121 |
-
expr = sympify(equation)
|
| 122 |
-
params = [param.strip() for param in params_str.split(',')]
|
| 123 |
-
params_symbols = symbols(params)
|
| 124 |
-
|
| 125 |
-
# Extraer s铆mbolos utilizados en la expresi贸n
|
| 126 |
-
used_symbols = expr.free_symbols
|
| 127 |
-
# Convertir s铆mbolos a strings
|
| 128 |
-
used_params = [str(s) for s in used_symbols if s != t_symbol]
|
| 129 |
-
|
| 130 |
-
# Verificar que todos los par谩metros en params_str est茅n usados en la ecuaci贸n
|
| 131 |
-
for param in params:
|
| 132 |
-
if param not in used_params:
|
| 133 |
-
raise ValueError(f"El par谩metro '{param}' no se usa en la ecuaci贸n '{equation}'.")
|
| 134 |
-
|
| 135 |
-
if model_type == 'biomass':
|
| 136 |
-
# Biomasa como funci贸n de tiempo y par谩metros
|
| 137 |
-
func_expr = expr
|
| 138 |
-
func = lambdify((t_symbol, *params_symbols), func_expr, 'numpy')
|
| 139 |
-
self.models['biomass'] = {
|
| 140 |
-
'function': func,
|
| 141 |
-
'params': params
|
| 142 |
-
}
|
| 143 |
-
elif model_type in ['substrate', 'product']:
|
| 144 |
-
# Estos modelos dependen de biomasa, que ya deber铆a estar establecida
|
| 145 |
-
if 'biomass' not in self.models:
|
| 146 |
-
raise ValueError("Biomasa debe estar configurada antes de Sustrato o Producto.")
|
| 147 |
-
biomass_func = self.models['biomass']['function']
|
| 148 |
-
# Reemplazar 'X(t)' por la funci贸n de biomasa
|
| 149 |
-
func_expr = expr.subs('X(t)', biomass_func)
|
| 150 |
-
func = lambdify((t_symbol, *params_symbols), func_expr, 'numpy')
|
| 151 |
-
self.models[model_type] = {
|
| 152 |
-
'function': func,
|
| 153 |
-
'params': params
|
| 154 |
-
}
|
| 155 |
-
else:
|
| 156 |
-
raise ValueError(f"Tipo de modelo no soportado: {model_type}")
|
| 157 |
-
|
| 158 |
-
def fit_model(self, model_type, time, data, bounds=([-np.inf], [np.inf])):
|
| 159 |
-
"""
|
| 160 |
-
Fits the model to the data.
|
| 161 |
-
|
| 162 |
-
:param model_type: Type of the model ('biomass', 'substrate', 'product')
|
| 163 |
-
:param time: Time data
|
| 164 |
-
:param data: Observed data to fit
|
| 165 |
-
:param bounds: Bounds for the parameters
|
| 166 |
-
:return: Predicted data from the model
|
| 167 |
-
"""
|
| 168 |
-
if model_type not in self.models:
|
| 169 |
-
raise ValueError(f"Model type '{model_type}' is not set. Please use set_model first.")
|
| 170 |
-
|
| 171 |
-
func = self.models[model_type]['function']
|
| 172 |
-
params = self.models[model_type]['params']
|
| 173 |
-
|
| 174 |
-
# Depuraci贸n: Asegurarse de que los par谩metros est茅n bien definidos
|
| 175 |
-
print(f"Fitting {model_type} model with function: {func} and parameters: {params}")
|
| 176 |
-
|
| 177 |
-
# Definir la funci贸n de ajuste (asegurarse de que toma los par谩metros correctamente)
|
| 178 |
-
def fit_func(t, *args):
|
| 179 |
-
try:
|
| 180 |
-
y = func(t, *args)
|
| 181 |
-
print(f"fit_func called with args: {args}")
|
| 182 |
-
print(f"y_pred: {y}")
|
| 183 |
-
return y
|
| 184 |
-
except Exception as e:
|
| 185 |
-
print(f"Error in fit_func: {e}")
|
| 186 |
-
raise
|
| 187 |
-
|
| 188 |
-
# Depuraci贸n: Verificar el n煤mero de par谩metros que se espera ajustar
|
| 189 |
-
print(f"Number of parameters to fit: {len(params)}")
|
| 190 |
-
|
| 191 |
-
# Definir una estimaci贸n inicial para los par谩metros
|
| 192 |
-
p0 = [1.0] * len(params) # Puedes ajustar estos valores seg煤n sea necesario
|
| 193 |
-
print(f"Initial parameter guesses (p0): {p0}")
|
| 194 |
-
|
| 195 |
-
try:
|
| 196 |
-
# Verifica que curve_fit puede recibir la funci贸n correctamente
|
| 197 |
-
print(f"Calling curve_fit with time: {time}, data: {data}, bounds: {bounds}, p0: {p0}")
|
| 198 |
-
|
| 199 |
-
# Intentar ajustar el modelo usando curve_fit con p0
|
| 200 |
-
popt, _ = curve_fit(fit_func, time, data, p0=p0, bounds=bounds, maxfev=10000)
|
| 201 |
-
print(f"Optimal parameters found: {popt}")
|
| 202 |
-
|
| 203 |
-
# Guardar los par谩metros ajustados en el modelo
|
| 204 |
-
self.params[model_type] = {param: val for param, val in zip(params, popt)}
|
| 205 |
-
y_pred = fit_func(time, *popt)
|
| 206 |
-
self.r2[model_type] = 1 - (np.sum((data - y_pred) ** 2) / np.sum((data - np.mean(data)) ** 2))
|
| 207 |
-
self.rmse[model_type] = np.sqrt(mean_squared_error(data, y_pred))
|
| 208 |
-
return y_pred
|
| 209 |
-
except Exception as e:
|
| 210 |
-
print(f"Error while fitting {model_type} model: {str(e)}")
|
| 211 |
-
raise
|
| 212 |
-
|
| 213 |
-
def plot_combined_results(self, time, biomass, substrate, product,
|
| 214 |
-
y_pred_biomass, y_pred_substrate, y_pred_product,
|
| 215 |
-
biomass_std=None, substrate_std=None, product_std=None,
|
| 216 |
-
experiment_name='', legend_position='best', params_position='upper right',
|
| 217 |
-
show_legend=True, show_params=True,
|
| 218 |
-
style='whitegrid', line_color='#0000FF', point_color='#000000',
|
| 219 |
-
line_style='-', marker_style='o'):
|
| 220 |
-
sns.set_style(style)
|
| 221 |
-
|
| 222 |
-
fig, axs = plt.subplots(3, 1, figsize=(10, 15))
|
| 223 |
-
|
| 224 |
-
# Gr谩fica de Biomasa
|
| 225 |
-
axs[0].plot(time, biomass, 'o', label='Datos de Biomasa')
|
| 226 |
-
for i, result in enumerate(biomass_results):
|
| 227 |
-
axs[0].plot(time, result['y_pred'], '-', label=f'Modelo de Biomasa {i+1}')
|
| 228 |
-
axs[0].set_xlabel('Tiempo')
|
| 229 |
-
axs[0].set_ylabel('Biomasa')
|
| 230 |
-
if show_legend:
|
| 231 |
-
axs[0].legend(loc=legend_position)
|
| 232 |
-
|
| 233 |
-
# Gr谩fica de Sustrato
|
| 234 |
-
axs[1].plot(time, substrate, 'o', label='Datos de Sustrato')
|
| 235 |
-
for i, result in enumerate(substrate_results):
|
| 236 |
-
axs[1].plot(time, result['y_pred'], '-', label=f'Modelo de Sustrato {i+1}')
|
| 237 |
-
axs[1].set_xlabel('Tiempo')
|
| 238 |
-
axs[1].set_ylabel('Sustrato')
|
| 239 |
-
if show_legend:
|
| 240 |
-
axs[1].legend(loc=legend_position)
|
| 241 |
-
|
| 242 |
-
# Gr谩fica de Producto
|
| 243 |
-
axs[2].plot(time, product, 'o', label='Datos de Producto')
|
| 244 |
-
for i, result in enumerate(product_results):
|
| 245 |
-
axs[2].plot(time, result['y_pred'], '-', label=f'Modelo de Producto {i+1}')
|
| 246 |
-
axs[2].set_xlabel('Tiempo')
|
| 247 |
-
axs[2].set_ylabel('Producto')
|
| 248 |
-
if show_legend:
|
| 249 |
-
axs[2].legend(loc=legend_position)
|
| 250 |
-
|
| 251 |
-
plt.tight_layout()
|
| 252 |
-
return fig
|
| 253 |
-
|
| 254 |
-
###############################
|
| 255 |
-
|
| 256 |
-
# Decorador GPU aplicado para manejar la ejecuci贸n en GPU si est谩 disponible
|
| 257 |
-
@gpu_decorator(duration=300)
|
| 258 |
-
def generate_analysis(prompt, max_length=1024, device=None):
|
| 259 |
-
try:
|
| 260 |
-
# Si el dispositivo no se especifica, usa CPU por defecto
|
| 261 |
-
if device is None:
|
| 262 |
-
device = torch.device('cpu')
|
| 263 |
-
|
| 264 |
-
# Mover el modelo al dispositivo adecuado (GPU o CPU) si es necesario
|
| 265 |
-
if next(model.parameters()).device != device:
|
| 266 |
-
model.to(device)
|
| 267 |
-
|
| 268 |
-
# Preparar los datos de entrada en el dispositivo correcto
|
| 269 |
-
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
|
| 270 |
-
max_gen_length = min(max_length + input_ids.size(1), model.config.max_position_embeddings)
|
| 271 |
-
|
| 272 |
-
# Generar el texto
|
| 273 |
-
generated_ids = model.generate(
|
| 274 |
-
input_ids=input_ids,
|
| 275 |
-
max_length=max_gen_length,
|
| 276 |
-
temperature=0.7,
|
| 277 |
-
num_return_sequences=1,
|
| 278 |
-
no_repeat_ngram_size=2,
|
| 279 |
-
early_stopping=True
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
# Decodificar la respuesta generada
|
| 283 |
-
output_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 284 |
-
analysis = output_text[len(prompt):].strip()
|
| 285 |
-
return analysis
|
| 286 |
-
except RuntimeError as e:
|
| 287 |
-
return f"Error durante la ejecuci贸n: {str(e)}"
|
| 288 |
-
except Exception as e:
|
| 289 |
-
return f"Ocurri贸 un error durante el an谩lisis: {e}"
|
| 290 |
-
|
| 291 |
-
def parse_bounds(bounds_str, num_params):
|
| 292 |
-
try:
|
| 293 |
-
# Reemplazar 'inf' por 'np.inf' si el usuario lo escribi贸 as铆
|
| 294 |
-
bounds_str = bounds_str.replace('inf', 'np.inf')
|
| 295 |
-
bounds = eval(f"[{bounds_str}]")
|
| 296 |
-
if len(bounds) != num_params:
|
| 297 |
-
raise ValueError("N煤mero de l铆mites no coincide con el n煤mero de par谩metros.")
|
| 298 |
-
lower_bounds = [b[0] for b in bounds]
|
| 299 |
-
upper_bounds = [b[1] for b in bounds]
|
| 300 |
-
return lower_bounds, upper_bounds
|
| 301 |
-
except Exception as e:
|
| 302 |
-
print(f"Error al parsear los l铆mites: {e}. Usando l铆mites por defecto.")
|
| 303 |
-
lower_bounds = [-np.inf] * num_params
|
| 304 |
-
upper_bounds = [np.inf] * num_params
|
| 305 |
-
return lower_bounds, upper_bounds
|
| 306 |
-
|
| 307 |
def process_and_plot(
|
| 308 |
file,
|
| 309 |
biomass_eq1, biomass_eq2, biomass_eq3,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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