Biotech2 / interface.py
C2MV's picture
Update interface.py
15a2bb8 verified
raw
history blame
2.58 kB
# interface.py
import gradio as gr
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
# Aqu铆 incluye la funci贸n process_and_plot completa, asegur谩ndote de que no haya referencias a decorators o @spaces.GPU
def process_and_plot(
file,
# Lista completa de par谩metros seg煤n tu c贸digo
# Aseg煤rate de que coincida con los inputs en UI.py
):
# Implementaci贸n de la funci贸n process_and_plot
# Procesa los datos, ajusta los modelos, genera las gr谩ficas y el an谩lisis
pass # Reemplaza con tu implementaci贸n
# Importar la funci贸n create_interface desde UI.py
from UI import create_interface
# Si deseas ejecutar la interfaz desde este archivo, aseg煤rate de que este bloque no cause conflictos al importar
if __name__ == "__main__":
demo = create_interface()
demo.launch()