Create interface.py
Browse files- interface.py +74 -0
interface.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# interface.py
|
| 2 |
+
|
| 3 |
+
from UI import create_interface
|
| 4 |
+
from models import BioprocessModel
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import numpy as np
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import torch
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 12 |
+
import copy
|
| 13 |
+
from config import DEVICE, MODEL_PATH, MAX_LENGTH, TEMPERATURE
|
| 14 |
+
|
| 15 |
+
device = DEVICE
|
| 16 |
+
model_path = MODEL_PATH
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(model_path).to(device).eval()
|
| 19 |
+
|
| 20 |
+
def generate_analysis(prompt, max_length=MAX_LENGTH):
|
| 21 |
+
try:
|
| 22 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
|
| 23 |
+
generated_ids = model.generate(
|
| 24 |
+
input_ids=input_ids,
|
| 25 |
+
max_length=max_length + len(input_ids[0]),
|
| 26 |
+
temperature=TEMPERATURE,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
no_repeat_ngram_size=2,
|
| 29 |
+
early_stopping=True
|
| 30 |
+
)
|
| 31 |
+
output_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 32 |
+
analysis = output_text[len(prompt):].strip()
|
| 33 |
+
return analysis
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"An error occurred during analysis: {e}"
|
| 36 |
+
|
| 37 |
+
def parse_bounds(bounds_str, num_params):
|
| 38 |
+
try:
|
| 39 |
+
bounds = eval(f"[{bounds_str}]")
|
| 40 |
+
if len(bounds) != num_params:
|
| 41 |
+
raise ValueError
|
| 42 |
+
lower_bounds = [b[0] for b in bounds]
|
| 43 |
+
upper_bounds = [b[1] for b in bounds]
|
| 44 |
+
return lower_bounds, upper_bounds
|
| 45 |
+
except:
|
| 46 |
+
lower_bounds = [-np.inf] * num_params
|
| 47 |
+
upper_bounds = [np.inf] * num_params
|
| 48 |
+
return lower_bounds, upper_bounds
|
| 49 |
+
|
| 50 |
+
def process_and_plot(
|
| 51 |
+
file,
|
| 52 |
+
biomass_equations_list,
|
| 53 |
+
biomass_params_list,
|
| 54 |
+
biomass_bounds_list,
|
| 55 |
+
substrate_equations_list,
|
| 56 |
+
substrate_params_list,
|
| 57 |
+
substrate_bounds_list,
|
| 58 |
+
product_equations_list,
|
| 59 |
+
product_params_list,
|
| 60 |
+
product_bounds_list,
|
| 61 |
+
legend_position,
|
| 62 |
+
show_legend,
|
| 63 |
+
show_params,
|
| 64 |
+
biomass_eq_count,
|
| 65 |
+
substrate_eq_count,
|
| 66 |
+
product_eq_count
|
| 67 |
+
):
|
| 68 |
+
# Implement the function to process data, fit models, generate plots, and get analysis
|
| 69 |
+
# Return the plot image and analysis
|
| 70 |
+
return [image], analysis
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo = create_interface(process_and_plot)
|
| 74 |
+
demo.launch()
|