Update app.py
Browse files
app.py
CHANGED
|
@@ -10,17 +10,12 @@ import libsbml
|
|
| 10 |
import networkx as nx
|
| 11 |
from pyvis.network import Network
|
| 12 |
|
| 13 |
-
|
| 14 |
client = chromadb.Client()
|
| 15 |
collection_name = "BioModelsRAG"
|
| 16 |
|
| 17 |
global db
|
| 18 |
db = client.get_or_create_collection(name=collection_name)
|
| 19 |
|
| 20 |
-
#Todolists
|
| 21 |
-
#1. if MODEL (cannot download) don't even include (TICK)
|
| 22 |
-
#2. switch the choosing and groq api key so if they just want to visualize thats fine (TICK)
|
| 23 |
-
|
| 24 |
|
| 25 |
class BioModelFetcher:
|
| 26 |
def __init__(self, github_owner="TheBobBob", github_repo_cache="BiomodelsCache", biomodels_json_db_path="src/cached_biomodels.json"):
|
|
@@ -121,7 +116,7 @@ class BioModelSplitter:
|
|
| 121 |
def __init__(self, groq_api_key):
|
| 122 |
self.groq_client = Groq(api_key=groq_api_key)
|
| 123 |
|
| 124 |
-
def split_biomodels(self, antimony_file_path, models):
|
| 125 |
text_splitter = CharacterTextSplitter(
|
| 126 |
separator=" // ",
|
| 127 |
chunk_size=1000,
|
|
@@ -130,33 +125,19 @@ class BioModelSplitter:
|
|
| 130 |
is_separator_regex=False,
|
| 131 |
)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
with open(file_path, 'r') as f:
|
| 140 |
-
file_content = f.read()
|
| 141 |
-
items = text_splitter.create_documents([file_content])
|
| 142 |
-
self.create_vector_db(items, models)
|
| 143 |
-
break
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"Error reading file {file_path}: {e}")
|
| 146 |
-
|
| 147 |
return db
|
| 148 |
|
| 149 |
-
def create_vector_db(self, final_items,
|
| 150 |
counter = 0
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
#might be a problem here?
|
| 156 |
-
if results['documents']:
|
| 157 |
-
continue
|
| 158 |
-
|
| 159 |
-
#could also be a problem in how the IDs are created
|
| 160 |
for item in final_items:
|
| 161 |
counter += 1 # Increment counter for each item
|
| 162 |
item_id = f"{counter}_{model_id}"
|
|
@@ -188,8 +169,8 @@ class BioModelSplitter:
|
|
| 188 |
)
|
| 189 |
else:
|
| 190 |
print(f"Error: No content returned from Groq for model {model_id}.")
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
|
| 195 |
class SBMLNetworkVisualizer:
|
|
@@ -287,6 +268,7 @@ class StreamlitApp:
|
|
| 287 |
if models:
|
| 288 |
model_ids = list(models.keys())
|
| 289 |
model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]
|
|
|
|
| 290 |
if models:
|
| 291 |
selected_models = st.multiselect(
|
| 292 |
"Select biomodels to analyze",
|
|
@@ -304,7 +286,7 @@ class StreamlitApp:
|
|
| 304 |
|
| 305 |
net = self.visualizer.sbml_to_network(model_file_path)
|
| 306 |
|
| 307 |
-
st.subheader(f"Model
|
| 308 |
net.show(f"sbml_network_{model_id}.html")
|
| 309 |
|
| 310 |
HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")
|
|
@@ -325,7 +307,7 @@ class StreamlitApp:
|
|
| 325 |
antimony_file_path = model_file_path.replace(".xml", ".txt")
|
| 326 |
|
| 327 |
AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)
|
| 328 |
-
self.splitter.split_biomodels(antimony_file_path, selected_models)
|
| 329 |
|
| 330 |
st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")
|
| 331 |
|
|
@@ -357,7 +339,6 @@ class StreamlitApp:
|
|
| 357 |
flat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]
|
| 358 |
query_results_final += "\n\n".join(flat_recommendation) + "\n\n"
|
| 359 |
|
| 360 |
-
|
| 361 |
prompt_template = f"""
|
| 362 |
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:
|
| 363 |
|
|
|
|
| 10 |
import networkx as nx
|
| 11 |
from pyvis.network import Network
|
| 12 |
|
|
|
|
| 13 |
client = chromadb.Client()
|
| 14 |
collection_name = "BioModelsRAG"
|
| 15 |
|
| 16 |
global db
|
| 17 |
db = client.get_or_create_collection(name=collection_name)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class BioModelFetcher:
|
| 21 |
def __init__(self, github_owner="TheBobBob", github_repo_cache="BiomodelsCache", biomodels_json_db_path="src/cached_biomodels.json"):
|
|
|
|
| 116 |
def __init__(self, groq_api_key):
|
| 117 |
self.groq_client = Groq(api_key=groq_api_key)
|
| 118 |
|
| 119 |
+
def split_biomodels(self, antimony_file_path, models, model_id):
|
| 120 |
text_splitter = CharacterTextSplitter(
|
| 121 |
separator=" // ",
|
| 122 |
chunk_size=1000,
|
|
|
|
| 125 |
is_separator_regex=False,
|
| 126 |
)
|
| 127 |
|
| 128 |
+
with open(antimony_file_path) as f:
|
| 129 |
+
file_content = f.read()
|
| 130 |
+
|
| 131 |
+
items = text_splitter.create_documents([file_content])
|
| 132 |
+
self.create_vector_db(items, model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
return db
|
| 134 |
|
| 135 |
+
def create_vector_db(self, final_items, model_id):
|
| 136 |
counter = 0
|
| 137 |
+
try:
|
| 138 |
+
results = db.get(where={"document": model_id})
|
| 139 |
+
|
| 140 |
+
if len(results['documents']) == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
for item in final_items:
|
| 142 |
counter += 1 # Increment counter for each item
|
| 143 |
item_id = f"{counter}_{model_id}"
|
|
|
|
| 169 |
)
|
| 170 |
else:
|
| 171 |
print(f"Error: No content returned from Groq for model {model_id}.")
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print(f"Error processing model {model_id}: {e}")
|
| 174 |
|
| 175 |
|
| 176 |
class SBMLNetworkVisualizer:
|
|
|
|
| 268 |
if models:
|
| 269 |
model_ids = list(models.keys())
|
| 270 |
model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]
|
| 271 |
+
|
| 272 |
if models:
|
| 273 |
selected_models = st.multiselect(
|
| 274 |
"Select biomodels to analyze",
|
|
|
|
| 286 |
|
| 287 |
net = self.visualizer.sbml_to_network(model_file_path)
|
| 288 |
|
| 289 |
+
st.subheader(f"Model {model_data['title']}")
|
| 290 |
net.show(f"sbml_network_{model_id}.html")
|
| 291 |
|
| 292 |
HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")
|
|
|
|
| 307 |
antimony_file_path = model_file_path.replace(".xml", ".txt")
|
| 308 |
|
| 309 |
AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)
|
| 310 |
+
self.splitter.split_biomodels(antimony_file_path, selected_models, model_id)
|
| 311 |
|
| 312 |
st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")
|
| 313 |
|
|
|
|
| 339 |
flat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]
|
| 340 |
query_results_final += "\n\n".join(flat_recommendation) + "\n\n"
|
| 341 |
|
|
|
|
| 342 |
prompt_template = f"""
|
| 343 |
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:
|
| 344 |
|