import gradio as gr import pandas as pd import io from PIL import Image from matplotlib import pyplot as plt from bioprocess_model import BioprocessModel # Asegúrate de que BioprocessModel esté en su propio archivo def create_interface(process_fn): with gr.Blocks() as demo: gr.Markdown("# Interfaz de Usuario para el Procesamiento de Datos de Bioproceso") # Inputs de archivo y parámetros file_input = gr.File(label="Subir archivo Excel con los datos", file_types=[".xls", ".xlsx"]) with gr.Row(): with gr.Column(): legend_position = gr.Radio( choices=["upper left", "upper right", "lower left", "lower right", "best"], label="Posición de la leyenda", value="best" ) show_legend = gr.Checkbox(label="Mostrar Leyenda", value=True) with gr.Column(): params_positions = ["upper left", "upper right", "lower left", "lower right", "outside right"] params_position = gr.Radio( choices=params_positions, label="Posición de los parámetros", value="upper right" ) show_params = gr.Checkbox(label="Mostrar Parámetros", value=True) # Parámetros adicionales model_type = gr.Radio(["logistic", "luedeking-piret"], label="Tipo de Modelo", value="logistic") mode = gr.Radio(["independent", "average", "combinado"], label="Modo de Análisis", value="independent") # Estilo gráfico styles = ['white', 'dark', 'whitegrid', 'darkgrid', 'ticks'] style_dropdown = gr.Dropdown(choices=styles, label="Selecciona el estilo de gráfico", value='whitegrid') line_color_picker = gr.ColorPicker(label="Color de la línea", value='#0000FF') point_color_picker = gr.ColorPicker(label="Color de los puntos", value='#000000') line_style_options = ['-', '--', '-.', ':'] line_style_dropdown = gr.Dropdown(choices=line_style_options, label="Estilo de línea", value='-') marker_style_options = ['o', 's', '^', 'v', 'D', 'x', '+', '*'] marker_style_dropdown = gr.Dropdown(choices=marker_style_options, label="Estilo de punto", value='o') # Botón de Procesar process_button = gr.Button("Procesar") # Salida output_gallery = gr.Gallery(label="Gráfico Generado", columns=2, height='auto') output_text = gr.Textbox(label="Análisis Generado", lines=10) # Conectar el botón con la función de procesamiento process_button.click( fn=process_fn, inputs=[file_input, legend_position, params_position, model_type, mode, style_dropdown, line_color_picker, point_color_picker, line_style_dropdown, marker_style_dropdown, show_legend, show_params], outputs=[output_gallery, output_text] ) return demo def process_and_plot(file, legend_position, params_position, model_type, mode, style, line_color, point_color, line_style, marker_style, show_legend, show_params): # Leer el archivo Excel proporcionado excel_data = pd.ExcelFile(file.name) figures = [] model = BioprocessModel() model.fit_model(model_type) for sheet_name in excel_data.sheet_names: df = pd.read_excel(excel_data, sheet_name=sheet_name) model.process_data(df) # Obtener los datos de tiempo, biomasa, sustrato y producto time = model.time biomass = model.dataxp[-1] substrate = model.datasp[-1] product = model.datapp[-1] # Generar los gráficos fig = model.plot_combined_results(time, biomass, substrate, product, model.dataxp[-1], model.datasp[-1], model.datapp[-1], model.datax_std[-1], model.datas_std[-1], model.datap_std[-1], experiment_name=sheet_name, legend_position=legend_position, params_position=params_position, show_legend=show_legend, show_params=show_params, style=style, line_color=line_color, point_color=point_color, line_style=line_style, marker_style=marker_style) figures.append(fig) # Convertir los gráficos en imágenes image_list = [] for fig in figures: buf = io.BytesIO() fig.savefig(buf, format='png') buf.seek(0) image = Image.open(buf) image_list.append(image) # Análisis básico generado analysis = f"Se han procesado {len(excel_data.sheet_names)} hojas con el modelo {model_type}." return image_list, analysis # Crear y lanzar la interfaz demo = create_interface(process_and_plot) demo.launch(share=True)