Update app.py
Browse files9 unmodified lines101112131415161718192021222324252694 unmodified lines1211221231241251261272 unmodified lines13013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116225 unmodified lines18818919019119219319419591 unmodified lines28728828929029129211 unmodified lines30430530630730830931014 unmodified lines32532632732832933033125 unmodified lines3573583593603613623639 unmodified linesimport networkx as nxfrom pyvis.network import Networkclient = chromadb.Client()collection_name = "BioModelsRAG"global dbdb = client.get_or_create_collection(name=collection_name)#Todolists#1. if MODEL (cannot download) don't even include (TICK)#2. switch the choosing and groq api key so if they just want to visualize thats fine (TICK)class BioModelFetcher:def __init__(self, github_owner="TheBobBob", github_repo_cache="BiomodelsCache", biomodels_json_db_path="src/cached_biomodels.json"):94 unmodified linesdef __init__(self, groq_api_key):self.groq_client = Groq(api_key=groq_api_key)def split_biomodels(self, antimony_file_path, models):text_splitter = CharacterTextSplitter(separator=" // ",chunk_size=1000,2 unmodified linesis_separator_regex=False,)directory_path = os.path.dirname(os.path.abspath(antimony_file_path))files = os.listdir(directory_path)for file in files:file_path = os.path.join(directory_path, file)try:with open(file_path, 'r') as f:file_content = f.read()items = text_splitter.create_documents([file_content])self.create_vector_db(items, models)breakexcept Exception as e:print(f"Error reading file {file_path}: {e}")return dbdef create_vector_db(self, final_items, models):counter = 0for model_id in models:try:results = db.get(where={"document": {"$eq": model_id}})#might be a problem here?if results['documents']:continue#could also be a problem in how the IDs are createdfor item in final_items:counter += 1 # Increment counter for each itemitem_id = f"{counter}_{model_id}"25 unmodified lines)else:print(f"Error: No content returned from Groq for model {model_id}.")except Exception as e:print(f"Error processing model {model_id}: {e}")class SBMLNetworkVisualizer:91 unmodified linesif models:model_ids = list(models.keys())model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]if models:selected_models = st.multiselect("Select biomodels to analyze",11 unmodified linesnet = self.visualizer.sbml_to_network(model_file_path)st.subheader(f"Model: {model_data['title']}")net.show(f"sbml_network_{model_id}.html")HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")14 unmodified linesantimony_file_path = model_file_path.replace(".xml", ".txt")AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)self.splitter.split_biomodels(antimony_file_path, selected_models)st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")25 unmodified linesflat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]query_results_final += "\n\n".join(flat_recommendation) + "\n\n"prompt_template = f"""Using the context and previous conversation provided below, answer the following question. If the information is insufficient to answer the question, please state that clearly:9 unmodified lines10111213141516171819202194 unmodified lines1161171181191201211222 unmodified lines12512612712812913013113213313413513613713813914014114214325 unmodified lines16917017117217317417517691 unmodified lines26826927027127227327411 unmodified lines28628728828929029129214 unmodified lines30730830931031131231325 unmodified lines3393403413423433449 unmodified linesimport networkx as nxfrom pyvis.network import Networkclient = chromadb.Client()collection_name = "BioModelsRAG"global dbdb = client.get_or_create_collection(name=collection_name)class BioModelFetcher:def __init__(self, github_owner="TheBobBob", github_repo_cache="BiomodelsCache", biomodels_json_db_path="src/cached_biomodels.json"):94 unmodified linesdef __init__(self, groq_api_key):self.groq_client = Groq(api_key=groq_api_key)def split_biomodels(self, antimony_file_path, models, model_id):text_splitter = CharacterTextSplitter(separator=" // ",chunk_size=1000,2 unmodified linesis_separator_regex=False,)with open(antimony_file_path) as f:file_content = f.read()items = text_splitter.create_documents([file_content])self.create_vector_db(items, model_id)return dbdef create_vector_db(self, final_items, model_id):counter = 0try:results = db.get(where={"document": model_id})if len(results['documents']) == 0:for item in final_items:counter += 1 # Increment counter for each itemitem_id = f"{counter}_{model_id}"25 unmodified lines)else:print(f"Error: No content returned from Groq for model {model_id}.")except Exception as e:print(f"Error processing model {model_id}: {e}")class SBMLNetworkVisualizer:91 unmodified linesif models:model_ids = list(models.keys())model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]if models:selected_models = st.multiselect("Select biomodels to analyze",11 unmodified linesnet = self.visualizer.sbml_to_network(model_file_path)st.subheader(f"Model {model_data['title']}")net.show(f"sbml_network_{model_id}.html")HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")14 unmodified linesantimony_file_path = model_file_path.replace(".xml", ".txt")AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)self.splitter.split_biomodels(antimony_file_path, selected_models, model_id)st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")25 unmodified linesflat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]query_results_final += "\n\n".join(flat_recommendation) + "\n\n"prompt_template = f"""Using the context and previous conversation provided below, answer the following question. If the information is insufficient to answer the question, please state that clearly:
app.py+16-35