Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ Original file is located at
|
|
| 7 |
https://colab.research.google.com/drive/1GgGC-fVnA0fSxU859NjSi_jH8yiWUKOu
|
| 8 |
"""
|
| 9 |
|
| 10 |
-
!pip install tensorflow==2.15
|
| 11 |
|
| 12 |
"""# Chem simulation using scipy"""
|
| 13 |
|
|
@@ -383,7 +383,7 @@ classifier = tf.estimator.DNNClassifier(
|
|
| 383 |
|
| 384 |
classifier.train(
|
| 385 |
input_fn=lambda: input_fn(train_normalized, train_y_encoded, training=True),
|
| 386 |
-
steps=
|
| 387 |
)
|
| 388 |
|
| 389 |
test_y_encoded = le.fit_transform(test_y) #we used sckit label encoder to encode the values better than 1 2 3 4 5 blah blah
|
|
@@ -398,30 +398,6 @@ classifier.evaluate(input_fn=lambda: input_fn(test_normalized,test_y_encoded,tra
|
|
| 398 |
- best ml model predicts the order of the differential equation from that
|
| 399 |
"""
|
| 400 |
|
| 401 |
-
def predict_order(inputs):
|
| 402 |
-
|
| 403 |
-
try:
|
| 404 |
-
# Create a pandas DataFrame from the input dictionary
|
| 405 |
-
input_df = pd.DataFrame(inputs, index=[0])
|
| 406 |
-
|
| 407 |
-
# Normalize the numerical features
|
| 408 |
-
input_df[NUMERIC_COLUMNS] = scaler.transform(input_df[NUMERIC_COLUMNS])
|
| 409 |
-
|
| 410 |
-
# Make a prediction
|
| 411 |
-
predictions = classifier.predict(input_fn=lambda: input_fn(input_df, labels=None, training=False))
|
| 412 |
-
|
| 413 |
-
# Get the predicted class and probability
|
| 414 |
-
for pred_dict in predictions:
|
| 415 |
-
class_id = pred_dict['class_ids'][0]
|
| 416 |
-
probability = pred_dict['probabilities'][class_id]
|
| 417 |
-
# Get the class name from the label encoder
|
| 418 |
-
class_name = le.inverse_transform([class_id])[0]
|
| 419 |
-
print('Order is "{}" ({:.1f}%)'.format(class_name, 100 * probability))
|
| 420 |
-
return class_name
|
| 421 |
-
except Exception as e:
|
| 422 |
-
print(f"An error occurred: {e}")
|
| 423 |
-
return None
|
| 424 |
-
|
| 425 |
def ode2(A0, B0, C0, temp, Ea, A_factor, is_reversible, order):
|
| 426 |
y0 = [A0, B0, C0]
|
| 427 |
|
|
@@ -448,9 +424,57 @@ def ode2(A0, B0, C0, temp, Ea, A_factor, is_reversible, order):
|
|
| 448 |
|
| 449 |
return solution.t, solution.y[0], solution.y[1], solution.y[2], k, k_1
|
| 450 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
"""## gradio"""
|
| 452 |
|
| 453 |
-
!pip install gradio
|
| 454 |
|
| 455 |
import gradio as gr
|
| 456 |
import pandas as pd
|
|
@@ -458,34 +482,14 @@ import numpy as np
|
|
| 458 |
import matplotlib.pyplot as plt
|
| 459 |
|
| 460 |
def run_simulation_and_plot(temp, Ea, A_factor, pH, pressure, is_reversible, structure, catalyst, A0, B0, C0):
|
| 461 |
-
#
|
| 462 |
-
#
|
| 463 |
-
|
|
|
|
| 464 |
|
| 465 |
-
# Create a dictionary with all the necessary inputs for the model
|
| 466 |
-
inputs = {
|
| 467 |
-
'temp': temp,
|
| 468 |
-
'pH': pH,
|
| 469 |
-
'Ea': Ea,
|
| 470 |
-
'A_factor': A_factor,
|
| 471 |
-
'pressure': pressure,
|
| 472 |
-
'log_pressure': np.log(pressure),
|
| 473 |
-
'weight': 150, # Using a placeholder value as it's not a user input
|
| 474 |
-
'structure': structure,
|
| 475 |
-
'catalyst': catalyst,
|
| 476 |
-
'is_reversible': int(is_reversible),
|
| 477 |
-
'k': k_pred, # Use simulated k
|
| 478 |
-
'k_1': k_1_pred, # Use simulated k_1
|
| 479 |
-
'A0': A_pred[0], 'A1': A_pred[1], 'A2': A_pred[2], 'A3': A_pred[3], 'A4': A_pred[4],
|
| 480 |
-
'A5': A_pred[5], 'A6': A_pred[6], 'A7': A_pred[7], 'A8': A_pred[8], 'A9': A_pred[9], 'A10': A_pred[10],
|
| 481 |
-
'B0': B_pred[0], 'B1': B_pred[1], 'B2': B_pred[2], 'B3': B_pred[3], 'B4': B_pred[4],
|
| 482 |
-
'B5': B_pred[5], 'B6': B_pred[6], 'B7': B_pred[7], 'B8': B_pred[8], 'B9': B_pred[9], 'B10': B_pred[10],
|
| 483 |
-
'C0': C_pred[0], 'C1': C_pred[1], 'C2': C_pred[2], 'C3': C_pred[3], 'C4': C_pred[4],
|
| 484 |
-
'C5': C_pred[5], 'C6': C_pred[6], 'C7': C_pred[7], 'C8': C_pred[8], 'C9': C_pred[9], 'C10': C_pred[10]
|
| 485 |
-
}
|
| 486 |
|
| 487 |
# --- 2. Prediction ---
|
| 488 |
-
predicted_order = predict_order(
|
| 489 |
|
| 490 |
# --- 3. Simulation with ode2 and Predicted Order ---
|
| 491 |
# Use ode2 for the final simulation and plotting
|
|
@@ -542,104 +546,104 @@ with gr.Blocks() as iface:
|
|
| 542 |
)
|
| 543 |
|
| 544 |
|
| 545 |
-
iface.launch()
|
| 546 |
|
| 547 |
!gradio deploy
|
| 548 |
|
| 549 |
"""## Streamlit Stuff"""
|
| 550 |
|
| 551 |
-
!pip install -q streamlit
|
| 552 |
|
| 553 |
-
import streamlit as st
|
| 554 |
-
import pandas as pd
|
| 555 |
-
import numpy as np
|
| 556 |
-
import matplotlib.pyplot as plt
|
| 557 |
|
| 558 |
-
# Assuming the functions compute_k, ode1, ode2, predict_order, and the classifier, scaler, and le objects are already defined and available in the notebook's global scope from previous cells.
|
| 559 |
|
| 560 |
-
st.set_page_config(layout="wide", page_title="Chemical Reaction Simulator") # Set page layout to wide and add a page title
|
| 561 |
|
| 562 |
-
st.title("π§ͺ Chemical Reaction Order Prediction and Simulation β¨")
|
| 563 |
-
st.markdown("Adjust the parameters below to predict the reaction order and visualize the concentration changes over time. π")
|
| 564 |
|
| 565 |
-
# Use columns for a better layout of inputs
|
| 566 |
-
col1, col2 = st.columns(2)
|
| 567 |
|
| 568 |
-
with col1:
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
|
| 579 |
-
with col2:
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
|
| 585 |
-
st.markdown("---") # Add a horizontal rule for separation
|
| 586 |
|
| 587 |
-
if st.button("π Predict and Plot Reaction"):
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
|
| 635 |
-
|
| 636 |
|
| 637 |
-
st.markdown("---")
|
| 638 |
-
st.markdown("App created with β€οΈ using Streamlit")
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
-
!npm install -g localtunnel
|
| 643 |
|
| 644 |
"""Main code for Steamlit pipeline
|
| 645 |
|
|
|
|
| 7 |
https://colab.research.google.com/drive/1GgGC-fVnA0fSxU859NjSi_jH8yiWUKOu
|
| 8 |
"""
|
| 9 |
|
| 10 |
+
# !pip install tensorflow==2.15
|
| 11 |
|
| 12 |
"""# Chem simulation using scipy"""
|
| 13 |
|
|
|
|
| 383 |
|
| 384 |
classifier.train(
|
| 385 |
input_fn=lambda: input_fn(train_normalized, train_y_encoded, training=True),
|
| 386 |
+
steps=5000
|
| 387 |
)
|
| 388 |
|
| 389 |
test_y_encoded = le.fit_transform(test_y) #we used sckit label encoder to encode the values better than 1 2 3 4 5 blah blah
|
|
|
|
| 398 |
- best ml model predicts the order of the differential equation from that
|
| 399 |
"""
|
| 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
def ode2(A0, B0, C0, temp, Ea, A_factor, is_reversible, order):
|
| 402 |
y0 = [A0, B0, C0]
|
| 403 |
|
|
|
|
| 424 |
|
| 425 |
return solution.t, solution.y[0], solution.y[1], solution.y[2], k, k_1
|
| 426 |
|
| 427 |
+
def predict_order(A, B, C, temp, Ea, A_factor, pH, pressure, is_reversible, structure, catalyst):
|
| 428 |
+
"""
|
| 429 |
+
Predicts the order of a chemical reaction based on concentration time series data and reaction conditions.
|
| 430 |
+
"""
|
| 431 |
+
try:
|
| 432 |
+
# Create a dictionary with all the necessary inputs for the model
|
| 433 |
+
inputs = {
|
| 434 |
+
'temp': temp,
|
| 435 |
+
'pH': pH,
|
| 436 |
+
'Ea': Ea,
|
| 437 |
+
'A_factor': A_factor,
|
| 438 |
+
'pressure': pressure,
|
| 439 |
+
'log_pressure': np.log(pressure),
|
| 440 |
+
'weight': 150, # Placeholder
|
| 441 |
+
'structure': structure,
|
| 442 |
+
'catalyst': catalyst,
|
| 443 |
+
'is_reversible': int(is_reversible),
|
| 444 |
+
'k': compute_k(temp, Ea, A_factor),
|
| 445 |
+
'k_1': compute_k(temp, Ea, A_factor) * 0.7, # Consistent with ode2
|
| 446 |
+
'A0': A[0], 'A1': A[1], 'A2': A[2], 'A3': A[3], 'A4': A[4],
|
| 447 |
+
'A5': A[5], 'A6': A[6], 'A7': A[7], 'A8': A[8], 'A9': A[9], 'A10': A[10],
|
| 448 |
+
'B0': B[0], 'B1': B[1], 'B2': B[2], 'B3': B[3], 'B4': B[4],
|
| 449 |
+
'B5': B[5], 'B6': B[6], 'B7': B[7], 'B8': B[8], 'B9': B[9], 'B10': B[10],
|
| 450 |
+
'C0': C[0], 'C1': C[1], 'C2': C[2], 'C3': C[3], 'C4': C[4],
|
| 451 |
+
'C5': C[5], 'C6': C[6], 'C7': C[7], 'C8': C[8], 'C9': C[9], 'C10': C[10]
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
# Create a pandas DataFrame from the input dictionary
|
| 455 |
+
input_df = pd.DataFrame(inputs, index=[0])
|
| 456 |
+
|
| 457 |
+
# Normalize the numerical features
|
| 458 |
+
input_df[NUMERIC_COLUMNS] = scaler.transform(input_df[NUMERIC_COLUMNS])
|
| 459 |
+
|
| 460 |
+
# Make a prediction
|
| 461 |
+
predictions = classifier.predict(input_fn=lambda: input_fn(input_df, labels=None, training=False))
|
| 462 |
+
|
| 463 |
+
# Get the predicted class and probability
|
| 464 |
+
for pred_dict in predictions:
|
| 465 |
+
class_id = pred_dict['class_ids'][0]
|
| 466 |
+
probability = pred_dict['probabilities'][class_id]
|
| 467 |
+
# Get the class name from the label encoder
|
| 468 |
+
class_name = le.inverse_transform([class_id])[0]
|
| 469 |
+
print('Order is "{}" ({:.1f}%)'.format(class_name, 100 * probability))
|
| 470 |
+
return class_name
|
| 471 |
+
except Exception as e:
|
| 472 |
+
print(f"An error occurred: {e}")
|
| 473 |
+
return None
|
| 474 |
+
|
| 475 |
"""## gradio"""
|
| 476 |
|
| 477 |
+
# !pip install gradio
|
| 478 |
|
| 479 |
import gradio as gr
|
| 480 |
import pandas as pd
|
|
|
|
| 482 |
import matplotlib.pyplot as plt
|
| 483 |
|
| 484 |
def run_simulation_and_plot(temp, Ea, A_factor, pH, pressure, is_reversible, structure, catalyst, A0, B0, C0):
|
| 485 |
+
# --- 1. Simulation with ode2 to get data for prediction ---
|
| 486 |
+
# For prediction, we need a simulated order. We can use a default or a simplified logic.
|
| 487 |
+
# Here, we'll arbitrarily use 'first' for the initial simulation to get data.
|
| 488 |
+
time_pred, A_pred, B_pred, C_pred, k_pred, k_1_pred = ode2(A0, B0, C0, temp, Ea, A_factor, int(is_reversible), 'first')
|
| 489 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
# --- 2. Prediction ---
|
| 492 |
+
predicted_order = predict_order(A_pred, B_pred, C_pred, temp, Ea, A_factor, pH, pressure, is_reversible, structure, catalyst)
|
| 493 |
|
| 494 |
# --- 3. Simulation with ode2 and Predicted Order ---
|
| 495 |
# Use ode2 for the final simulation and plotting
|
|
|
|
| 546 |
)
|
| 547 |
|
| 548 |
|
| 549 |
+
iface.launch(debug=True)
|
| 550 |
|
| 551 |
!gradio deploy
|
| 552 |
|
| 553 |
"""## Streamlit Stuff"""
|
| 554 |
|
| 555 |
+
# !pip install -q streamlit
|
| 556 |
|
| 557 |
+
# import streamlit as st
|
| 558 |
+
# import pandas as pd
|
| 559 |
+
# import numpy as np
|
| 560 |
+
# import matplotlib.pyplot as plt
|
| 561 |
|
| 562 |
+
# # Assuming the functions compute_k, ode1, ode2, predict_order, and the classifier, scaler, and le objects are already defined and available in the notebook's global scope from previous cells.
|
| 563 |
|
| 564 |
+
# st.set_page_config(layout="wide", page_title="Chemical Reaction Simulator") # Set page layout to wide and add a page title
|
| 565 |
|
| 566 |
+
# st.title("π§ͺ Chemical Reaction Order Prediction and Simulation β¨")
|
| 567 |
+
# st.markdown("Adjust the parameters below to predict the reaction order and visualize the concentration changes over time. π")
|
| 568 |
|
| 569 |
+
# # Use columns for a better layout of inputs
|
| 570 |
+
# col1, col2 = st.columns(2)
|
| 571 |
|
| 572 |
+
# with col1:
|
| 573 |
+
# st.header("βοΈ Reaction Conditions")
|
| 574 |
+
# temp = st.slider("Temperature (K) π‘οΈ", 270.0, 280.0, value=277.0)
|
| 575 |
+
# Ea = st.slider("Activation Energy (Ea, kJ/mol) π₯", 90.0, 100.0, value=93.0)
|
| 576 |
+
# A_factor = st.slider("Pre-exponential Factor (A_factor) π", 2e16, 5e17, value=4.2e17, format="%e") # Use scientific notation format
|
| 577 |
+
# pH = st.slider("pH π§ͺ", 1.0, 14.0, value=6.5)
|
| 578 |
+
# pressure = st.slider("Pressure π«οΈ", 0.5, 5.0, value=3.0)
|
| 579 |
+
# is_reversible = st.checkbox("Is Reversible? π", value=False)
|
| 580 |
+
# structure = st.selectbox("Structure βοΈ", ['Linear', 'Ring', 'Branched', 'Unknown'], index=1)
|
| 581 |
+
# catalyst = st.selectbox("Catalyst β¨", ['None', 'Enzyme', 'Acid', 'Base'], index=2)
|
| 582 |
|
| 583 |
+
# with col2:
|
| 584 |
+
# st.header("π Initial Concentrations")
|
| 585 |
+
# A0 = st.slider("Initial Concentration of A (Aβ)", 0.0, 10.0, value=5.0)
|
| 586 |
+
# B0 = st.slider("Initial Concentration of B (Bβ)", 0.0, 10.0, value=2.0)
|
| 587 |
+
# C0 = st.slider("Initial Concentration of C (Cβ)", 0.0, 10.0, value=1.0)
|
| 588 |
|
| 589 |
+
# st.markdown("---") # Add a horizontal rule for separation
|
| 590 |
|
| 591 |
+
# if st.button("π Predict and Plot Reaction"):
|
| 592 |
+
# # Data Preparation for Prediction
|
| 593 |
+
# # Simulate the reaction using ode1 to get concentrations over time for prediction features
|
| 594 |
+
# time_pred, A_pred, B_pred, C_pred, k_pred, k_1_pred, is_reversible_simulated, order_simulated = ode1(A0, B0, C0, temp, Ea, A_factor)
|
| 595 |
|
| 596 |
+
# # Create a dictionary with all the necessary inputs for the model
|
| 597 |
+
# inputs = {
|
| 598 |
+
# 'temp': temp,
|
| 599 |
+
# 'pH': pH,
|
| 600 |
+
# 'Ea': Ea,
|
| 601 |
+
# 'A_factor': A_factor,
|
| 602 |
+
# 'pressure': pressure,
|
| 603 |
+
# 'log_pressure': np.log(pressure),
|
| 604 |
+
# 'weight': 150, # Using a placeholder value as it's not a user input
|
| 605 |
+
# 'structure': structure,
|
| 606 |
+
# 'catalyst': catalyst,
|
| 607 |
+
# 'is_reversible': int(is_reversible),
|
| 608 |
+
# 'k': k_pred, # Use simulated k
|
| 609 |
+
# 'k_1': k_1_pred, # Use simulated k_1
|
| 610 |
+
# 'A0': A_pred[0], 'A1': A_pred[1], 'A2': A_pred[2], 'A3': A_pred[3], 'A4': A_pred[4],
|
| 611 |
+
# 'A5': A_pred[5], 'A6': A_pred[6], 'A7': A_pred[7], 'A8': A_pred[8], 'A9': A_pred[9], 'A10': A_pred[10],
|
| 612 |
+
# 'B0': B_pred[0], 'B1': B_pred[1], 'B2': B_pred[2], 'B3': B_pred[3], 'B4': B_pred[4],
|
| 613 |
+
# 'B5': B_pred[5], 'B6': B_pred[6], 'B7': B_pred[7], 'B8': B_pred[8], 'B9': B_pred[9], 'B10': B_pred[10],
|
| 614 |
+
# 'C0': C_pred[0], 'C1': C_pred[1], 'C2': C_pred[2], 'C3': C_pred[3], 'C4': C_pred[4],
|
| 615 |
+
# 'C5': C_pred[5], 'C6': C_pred[6], 'C7': C_pred[7], 'C8': C_pred[8], 'C9': C_pred[9], 'C10': C_pred[10]
|
| 616 |
+
# }
|
| 617 |
|
| 618 |
+
# # --- 2. Prediction ---
|
| 619 |
+
# with st.spinner('Predicting reaction order...'):
|
| 620 |
+
# predicted_order = predict_order(inputs)
|
| 621 |
+
# st.success(f"β
Predicted Order: **{predicted_order}**")
|
| 622 |
|
| 623 |
+
# # --- 3. Simulation with ode2 and Predicted Order ---
|
| 624 |
+
# with st.spinner('Simulating reaction...'):
|
| 625 |
+
# time_sim, A_sim, B_sim, C_sim, k_sim, k_1_sim = ode2(A0, B0, C0, temp, Ea, A_factor, int(is_reversible), predicted_order)
|
| 626 |
|
| 627 |
+
# # --- 4. Plotting ---
|
| 628 |
+
# st.header("π Concentration vs. Time Plot")
|
| 629 |
+
# fig, ax = plt.subplots()
|
| 630 |
+
# ax.plot(time_sim, A_sim, label='A', marker='o') # Add markers to plot points
|
| 631 |
+
# ax.plot(time_sim, B_sim, label='B', marker='x')
|
| 632 |
+
# ax.plot(time_sim, C_sim, label='C', marker='s')
|
| 633 |
+
# ax.set_xlabel('Time')
|
| 634 |
+
# ax.set_ylabel('Concentration')
|
| 635 |
+
# ax.set_title(f'Concentration vs. Time (Predicted Order: {predicted_order})')
|
| 636 |
+
# ax.legend()
|
| 637 |
+
# ax.grid(True)
|
| 638 |
|
| 639 |
+
# st.pyplot(fig)
|
| 640 |
|
| 641 |
+
# st.markdown("---")
|
| 642 |
+
# st.markdown("App created with β€οΈ using Streamlit")
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
+
# !npm install -g localtunnel
|
| 647 |
|
| 648 |
"""Main code for Steamlit pipeline
|
| 649 |
|