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}"
|
|
@@ -168,6 +149,7 @@ class BioModelSplitter:
|
|
| 168 |
2. Maintain all original values and include any mathematical expressions or values in full.
|
| 169 |
3. Ensure that all variable names and their values are clearly presented.
|
| 170 |
4. Write the summary in paragraph format, putting an emphasis on clarity and completeness.
|
|
|
|
| 171 |
Segment of Antimony: {item}
|
| 172 |
"""
|
| 173 |
|
|
@@ -185,10 +167,11 @@ class BioModelSplitter:
|
|
| 185 |
metadatas=[{"document": model_id}],
|
| 186 |
documents=[chat_completion.choices[0].message.content],
|
| 187 |
)
|
|
|
|
| 188 |
else:
|
| 189 |
print(f"Error: No content returned from Groq for model {model_id}.")
|
| 190 |
-
|
| 191 |
-
|
| 192 |
|
| 193 |
|
| 194 |
class SBMLNetworkVisualizer:
|
|
@@ -286,6 +269,7 @@ class StreamlitApp:
|
|
| 286 |
if models:
|
| 287 |
model_ids = list(models.keys())
|
| 288 |
model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]
|
|
|
|
| 289 |
if models:
|
| 290 |
selected_models = st.multiselect(
|
| 291 |
"Select biomodels to analyze",
|
|
@@ -303,7 +287,7 @@ class StreamlitApp:
|
|
| 303 |
|
| 304 |
net = self.visualizer.sbml_to_network(model_file_path)
|
| 305 |
|
| 306 |
-
st.subheader(f"Model
|
| 307 |
net.show(f"sbml_network_{model_id}.html")
|
| 308 |
|
| 309 |
HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")
|
|
@@ -324,7 +308,7 @@ class StreamlitApp:
|
|
| 324 |
antimony_file_path = model_file_path.replace(".xml", ".txt")
|
| 325 |
|
| 326 |
AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)
|
| 327 |
-
self.splitter.split_biomodels(antimony_file_path, selected_models)
|
| 328 |
|
| 329 |
st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")
|
| 330 |
|
|
@@ -352,21 +336,25 @@ class StreamlitApp:
|
|
| 352 |
n_results=5,
|
| 353 |
where={"document": {"$eq": model_id}},
|
| 354 |
)
|
|
|
|
| 355 |
best_recommendation = query_results['documents']
|
| 356 |
flat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]
|
| 357 |
query_results_final += "\n\n".join(flat_recommendation) + "\n\n"
|
| 358 |
|
| 359 |
-
|
| 360 |
prompt_template = f"""
|
| 361 |
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:
|
|
|
|
| 362 |
Context:
|
| 363 |
{query_results_final}
|
|
|
|
| 364 |
Previous Conversation:
|
| 365 |
{history}
|
|
|
|
| 366 |
Instructions:
|
| 367 |
1. Cross-Reference: Use all provided context to define variables and identify any unknown entities.
|
| 368 |
2. Mathematical Calculations: Perform any necessary calculations based on the context and available data.
|
| 369 |
3. Consistency: Remember and incorporate previous responses if the question is related to earlier information.
|
|
|
|
| 370 |
Question:
|
| 371 |
{prompt}
|
| 372 |
"""
|
|
|
|
| 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 |
+
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 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}"
|
|
|
|
| 149 |
2. Maintain all original values and include any mathematical expressions or values in full.
|
| 150 |
3. Ensure that all variable names and their values are clearly presented.
|
| 151 |
4. Write the summary in paragraph format, putting an emphasis on clarity and completeness.
|
| 152 |
+
|
| 153 |
Segment of Antimony: {item}
|
| 154 |
"""
|
| 155 |
|
|
|
|
| 167 |
metadatas=[{"document": model_id}],
|
| 168 |
documents=[chat_completion.choices[0].message.content],
|
| 169 |
)
|
| 170 |
+
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 171 |
else:
|
| 172 |
print(f"Error: No content returned from Groq for model {model_id}.")
|
| 173 |
+
except Exception as e:
|
| 174 |
+
print(f"Error processing model {model_id}: {e}")
|
| 175 |
|
| 176 |
|
| 177 |
class SBMLNetworkVisualizer:
|
|
|
|
| 269 |
if models:
|
| 270 |
model_ids = list(models.keys())
|
| 271 |
model_ids = [model_id for model_id in model_ids if not str(model_id).startswith("MODEL")]
|
| 272 |
+
|
| 273 |
if models:
|
| 274 |
selected_models = st.multiselect(
|
| 275 |
"Select biomodels to analyze",
|
|
|
|
| 287 |
|
| 288 |
net = self.visualizer.sbml_to_network(model_file_path)
|
| 289 |
|
| 290 |
+
st.subheader(f"Model {model_data['title']}")
|
| 291 |
net.show(f"sbml_network_{model_id}.html")
|
| 292 |
|
| 293 |
HtmlFile = open(f"sbml_network_{model_id}.html", "r", encoding="utf-8")
|
|
|
|
| 308 |
antimony_file_path = model_file_path.replace(".xml", ".txt")
|
| 309 |
|
| 310 |
AntimonyConverter.convert_sbml_to_antimony(model_file_path, antimony_file_path)
|
| 311 |
+
self.splitter.split_biomodels(antimony_file_path, selected_models, model_id)
|
| 312 |
|
| 313 |
st.info(f"Model {model_id} {model_data['name']} has successfully been added to the database! :) ")
|
| 314 |
|
|
|
|
| 336 |
n_results=5,
|
| 337 |
where={"document": {"$eq": model_id}},
|
| 338 |
)
|
| 339 |
+
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 340 |
best_recommendation = query_results['documents']
|
| 341 |
flat_recommendation = [item for sublist in best_recommendation for item in (sublist if isinstance(sublist, list) else [sublist])]
|
| 342 |
query_results_final += "\n\n".join(flat_recommendation) + "\n\n"
|
| 343 |
|
|
|
|
| 344 |
prompt_template = f"""
|
| 345 |
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:
|
| 346 |
+
|
| 347 |
Context:
|
| 348 |
{query_results_final}
|
| 349 |
+
|
| 350 |
Previous Conversation:
|
| 351 |
{history}
|
| 352 |
+
|
| 353 |
Instructions:
|
| 354 |
1. Cross-Reference: Use all provided context to define variables and identify any unknown entities.
|
| 355 |
2. Mathematical Calculations: Perform any necessary calculations based on the context and available data.
|
| 356 |
3. Consistency: Remember and incorporate previous responses if the question is related to earlier information.
|
| 357 |
+
|
| 358 |
Question:
|
| 359 |
{prompt}
|
| 360 |
"""
|