Biotech2 / interface.py
C2MV's picture
Update interface.py
881af71 verified
raw
history blame
11.1 kB
# interface.py
from models import BioprocessModel
import io
from PIL import Image
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from sympy import symbols, sympify, lambdify
import copy
from config import DEVICE, MODEL_PATH, MAX_LENGTH, TEMPERATURE
# Configuración del dispositivo
device = DEVICE
# Cargar el modelo
model_path = MODEL_PATH # Reemplaza con la ruta real de tu modelo
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
model.to(device)
model.eval()
@torch.no_grad()
def generate_analysis(prompt, max_length=MAX_LENGTH):
try:
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
max_gen_length = min(max_length + input_ids.size(1), model.config.max_position_embeddings)
generated_ids = model.generate(
input_ids=input_ids,
max_length=max_gen_length,
temperature=TEMPERATURE,
num_return_sequences=1,
no_repeat_ngram_size=2,
early_stopping=True
)
output_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
analysis = output_text[len(prompt):].strip()
return analysis
except Exception as e:
return f"Ocurrió un error durante el análisis: {e}"
def parse_bounds(bounds_str, num_params):
try:
bounds = eval(f"[{bounds_str}]")
if len(bounds) != num_params:
raise ValueError
lower_bounds = [b[0] for b in bounds]
upper_bounds = [b[1] for b in bounds]
return lower_bounds, upper_bounds
except:
lower_bounds = [-np.inf] * num_params
upper_bounds = [np.inf] * num_params
return lower_bounds, upper_bounds
def process_and_plot(
file,
biomass_eq1, biomass_eq2, biomass_eq3,
biomass_param1, biomass_param2, biomass_param3,
biomass_bound1, biomass_bound2, biomass_bound3,
substrate_eq1, substrate_eq2, substrate_eq3,
substrate_param1, substrate_param2, substrate_param3,
substrate_bound1, substrate_bound2, substrate_bound3,
product_eq1, product_eq2, product_eq3,
product_param1, product_param2, product_param3,
product_bound1, product_bound2, product_bound3,
legend_position,
show_legend,
show_params,
biomass_eq_count,
substrate_eq_count,
product_eq_count
):
# Convierte los contadores a enteros
biomass_eq_count = int(biomass_eq_count)
substrate_eq_count = int(substrate_eq_count)
product_eq_count = int(product_eq_count)
# Recolecta las ecuaciones, parámetros y límites según los contadores
biomass_eqs = [biomass_eq1, biomass_eq2, biomass_eq3][:biomass_eq_count]
biomass_params = [biomass_param1, biomass_param2, biomass_param3][:biomass_eq_count]
biomass_bounds = [biomass_bound1, biomass_bound2, biomass_bound3][:biomass_eq_count]
substrate_eqs = [substrate_eq1, substrate_eq2, substrate_eq3][:substrate_eq_count]
substrate_params = [substrate_param1, substrate_param2, substrate_param3][:substrate_eq_count]
substrate_bounds = [substrate_bound1, substrate_bound2, substrate_bound3][:substrate_eq_count]
product_eqs = [product_eq1, product_eq2, product_eq3][:product_eq_count]
product_params = [product_param1, product_param2, product_param3][:product_eq_count]
product_bounds = [product_bound1, product_bound2, product_bound3][:product_eq_count]
# Lee el archivo Excel subido
df = pd.read_excel(file.name)
time = df['Time'].values
biomass_data = df['Biomass'].values
substrate_data = df['Substrate'].values
product_data = df['Product'].values
biomass_results = []
substrate_results = []
product_results = []
# Ajusta los modelos de Biomasa
for i in range(len(biomass_eqs)):
equation = biomass_eqs[i]
params_str = biomass_params[i]
bounds_str = biomass_bounds[i]
model = BioprocessModel()
model.set_model('biomass', equation, params_str)
params = [param.strip() for param in params_str.split(',')]
lower_bounds, upper_bounds = parse_bounds(bounds_str, len(params))
y_pred = model.fit_model(
'biomass', time, biomass_data,
bounds=(lower_bounds, upper_bounds)
)
biomass_results.append({
'model': copy.deepcopy(model),
'y_pred': y_pred,
'equation': equation
})
# Usa el primer modelo de biomasa para X(t)
biomass_model = biomass_results[0]['model']
X_t_func = biomass_model.models['biomass']['function']
biomass_params_values = list(biomass_model.params['biomass'].values())
# Ajusta los modelos de Sustrato
for i in range(len(substrate_eqs)):
equation = substrate_eqs[i]
params_str = substrate_params[i]
bounds_str = substrate_bounds[i]
model = BioprocessModel()
t_symbol = symbols('t')
expr_substrate = sympify(equation)
substrate_params_symbols = symbols([param.strip() for param in params_str.split(',')])
substrate_func_expr = expr_substrate.subs('X(t)', X_t_func(t_symbol, *biomass_params_values))
substrate_func = lambdify(
(t_symbol, *substrate_params_symbols),
substrate_func_expr,
'numpy'
)
model.models['substrate'] = {
'function': substrate_func,
'params': [param.strip() for param in params_str.split(',')]
}
params = model.models['substrate']['params']
lower_bounds, upper_bounds = parse_bounds(bounds_str, len(params))
y_pred = model.fit_model(
'substrate', time, substrate_data,
bounds=(lower_bounds, upper_bounds)
)
substrate_results.append({
'model': copy.deepcopy(model),
'y_pred': y_pred,
'equation': equation
})
# Ajusta los modelos de Producto
for i in range(len(product_eqs)):
equation = product_eqs[i]
params_str = product_params[i]
bounds_str = product_bounds[i]
model = BioprocessModel()
t_symbol = symbols('t')
expr_product = sympify(equation)
product_params_symbols = symbols([param.strip() for param in params_str.split(',')])
product_func_expr = expr_product.subs('X(t)', X_t_func(t_symbol, *biomass_params_values))
product_func = lambdify(
(t_symbol, *product_params_symbols),
product_func_expr,
'numpy'
)
model.models['product'] = {
'function': product_func,
'params': [param.strip() for param in params_str.split(',')]
}
params = model.models['product']['params']
lower_bounds, upper_bounds = parse_bounds(bounds_str, len(params))
y_pred = model.fit_model(
'product', time, product_data,
bounds=(lower_bounds, upper_bounds)
)
product_results.append({
'model': copy.deepcopy(model),
'y_pred': y_pred,
'equation': equation
})
# Genera las gráficas
fig, axs = plt.subplots(3, 1, figsize=(10, 15))
# Gráfica de Biomasa
axs[0].plot(time, biomass_data, 'o', label='Datos de Biomasa')
for i, result in enumerate(biomass_results):
axs[0].plot(time, result['y_pred'], '-', label=f'Modelo de Biomasa {i+1}')
axs[0].set_xlabel('Tiempo')
axs[0].set_ylabel('Biomasa')
if show_legend:
axs[0].legend(loc=legend_position)
# Gráfica de Sustrato
axs[1].plot(time, substrate_data, 'o', label='Datos de Sustrato')
for i, result in enumerate(substrate_results):
axs[1].plot(time, result['y_pred'], '-', label=f'Modelo de Sustrato {i+1}')
axs[1].set_xlabel('Tiempo')
axs[1].set_ylabel('Sustrato')
if show_legend:
axs[1].legend(loc=legend_position)
# Gráfica de Producto
axs[2].plot(time, product_data, 'o', label='Datos de Producto')
for i, result in enumerate(product_results):
axs[2].plot(time, result['y_pred'], '-', label=f'Modelo de Producto {i+1}')
axs[2].set_xlabel('Tiempo')
axs[2].set_ylabel('Producto')
if show_legend:
axs[2].legend(loc=legend_position)
plt.tight_layout()
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
image = Image.open(buf)
all_results = {
'biomass_models': [],
'substrate_models': [],
'product_models': []
}
for i, result in enumerate(biomass_results):
model_info = {
'model_number': i + 1,
'equation': result['equation'],
'parameters': result['model'].params['biomass'],
'R2': result['model'].r2['biomass'],
'RMSE': result['model'].rmse['biomass']
}
all_results['biomass_models'].append(model_info)
for i, result in enumerate(substrate_results):
model_info = {
'model_number': i + 1,
'equation': result['equation'],
'parameters': result['model'].params['substrate'],
'R2': result['model'].r2['substrate'],
'RMSE': result['model'].rmse['substrate']
}
all_results['substrate_models'].append(model_info)
for i, result in enumerate(product_results):
model_info = {
'model_number': i + 1,
'equation': result['equation'],
'parameters': result['model'].params['product'],
'R2': result['model'].r2['product'],
'RMSE': result['model'].rmse['product']
}
all_results['product_models'].append(model_info)
results_text = "Resultados Experimentales:\n\n"
results_text += "Modelos de Biomasa:\n"
for model_info in all_results['biomass_models']:
results_text += f"""
Modelo {model_info['model_number']}:
Ecuación: {model_info['equation']}
Parámetros: {model_info['parameters']}
R²: {model_info['R2']:.4f}
RMSE: {model_info['RMSE']:.4f}
"""
results_text += "\nModelos de Sustrato:\n"
for model_info in all_results['substrate_models']:
results_text += f"""
Modelo {model_info['model_number']}:
Ecuación: {model_info['equation']}
Parámetros: {model_info['parameters']}
R²: {model_info['R2']:.4f}
RMSE: {model_info['RMSE']:.4f}
"""
results_text += "\nModelos de Producto:\n"
for model_info in all_results['product_models']:
results_text += f"""
Modelo {model_info['model_number']}:
Ecuación: {model_info['equation']}
Parámetros: {model_info['parameters']}
R²: {model_info['R2']:.4f}
RMSE: {model_info['RMSE']:.4f}
"""
prompt = f"""
Eres un experto en modelado de bioprocesos.
Analiza los siguientes resultados experimentales y proporciona un veredicto sobre la calidad de los modelos, sugiriendo mejoras si es necesario.
{results_text}
Tu análisis debe ser detallado y profesional.
"""
analysis = generate_analysis(prompt)
return [image], analysis