Spaces:
Running
Running
Update prediction.py
Browse files- prediction.py +126 -73
prediction.py
CHANGED
@@ -1,73 +1,126 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from transformers import AutoTokenizer, AutoModel
|
5 |
+
from rdkit import Chem
|
6 |
+
from rdkit.Chem import AllChem, Descriptors
|
7 |
+
from torch import nn
|
8 |
+
import pandas as pd
|
9 |
+
import requests
|
10 |
+
import datetime
|
11 |
+
from db import get_database # Assuming you have a file db.py with get_database function to connect to MongoDB
|
12 |
+
|
13 |
+
# Model Setup
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained("seyonec/ChemBERTa-zinc-base-v1")
|
15 |
+
chemberta = AutoModel.from_pretrained("seyonec/ChemBERTa-zinc-base-v1")
|
16 |
+
chemberta.eval()
|
17 |
+
|
18 |
+
# Define your model architecture
|
19 |
+
class TransformerRegressor(nn.Module):
|
20 |
+
def __init__(self, emb_dim=768, feat_dim=2058, output_dim=6, nhead=8, num_layers=2):
|
21 |
+
super().__init__()
|
22 |
+
self.feat_proj = nn.Linear(feat_dim, emb_dim)
|
23 |
+
encoder_layer = nn.TransformerEncoderLayer(d_model=768, nhead=8, dim_feedforward=1024, dropout=0.1, batch_first=True)
|
24 |
+
self.transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
|
25 |
+
self.regression_head = nn.Sequential(
|
26 |
+
nn.Linear(emb_dim, 256), nn.ReLU(),
|
27 |
+
nn.Linear(256, 128), nn.ReLU(),
|
28 |
+
nn.Linear(128, output_dim)
|
29 |
+
)
|
30 |
+
|
31 |
+
def forward(self, x, feat):
|
32 |
+
feat_emb = self.feat_proj(feat)
|
33 |
+
stacked = torch.stack([x, feat_emb], dim=1)
|
34 |
+
encoded = self.transformer_encoder(stacked)
|
35 |
+
aggregated = encoded.mean(dim=1)
|
36 |
+
return self.regression_head(aggregated)
|
37 |
+
|
38 |
+
# Load model
|
39 |
+
model = TransformerRegressor()
|
40 |
+
model.load_state_dict(torch.load("transformer_model.pt", map_location=torch.device('cpu')))
|
41 |
+
model.eval()
|
42 |
+
|
43 |
+
# Feature Functions
|
44 |
+
descriptor_fns = [Descriptors.MolWt, Descriptors.MolLogP, Descriptors.TPSA,
|
45 |
+
Descriptors.NumRotatableBonds, Descriptors.NumHAcceptors,
|
46 |
+
Descriptors.NumHDonors, Descriptors.RingCount,
|
47 |
+
Descriptors.FractionCSP3, Descriptors.HeavyAtomCount,
|
48 |
+
Descriptors.NHOHCount]
|
49 |
+
|
50 |
+
def fix_smiles(s):
|
51 |
+
try:
|
52 |
+
mol = Chem.MolFromSmiles(s.strip())
|
53 |
+
if mol:
|
54 |
+
return Chem.MolToSmiles(mol)
|
55 |
+
except:
|
56 |
+
return None
|
57 |
+
return None
|
58 |
+
|
59 |
+
def compute_features(smiles):
|
60 |
+
mol = Chem.MolFromSmiles(smiles)
|
61 |
+
if not mol:
|
62 |
+
return [0]*10 + [0]*2048
|
63 |
+
desc = [fn(mol) for fn in descriptor_fns]
|
64 |
+
fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=2048)
|
65 |
+
return desc + list(fp)
|
66 |
+
|
67 |
+
def embed_smiles(smiles_list):
|
68 |
+
inputs = tokenizer(smiles_list, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
69 |
+
outputs = chemberta(**inputs)
|
70 |
+
return outputs.last_hidden_state[:, 0, :]
|
71 |
+
|
72 |
+
# Function to validate SMILES string
|
73 |
+
def is_valid_smiles(smiles):
|
74 |
+
""" Validate if the input is a valid SMILES string using RDKit """
|
75 |
+
mol = Chem.MolFromSmiles(smiles)
|
76 |
+
return mol is not None
|
77 |
+
|
78 |
+
# Function to save prediction to MongoDB
|
79 |
+
def save_to_db(smiles_input, predictions):
|
80 |
+
db = get_database()
|
81 |
+
collection = db["polymers"] # your collection
|
82 |
+
doc = {
|
83 |
+
"smiles": smiles_input,
|
84 |
+
"predictions": predictions,
|
85 |
+
"timestamp": datetime.datetime.utcnow()
|
86 |
+
}
|
87 |
+
collection.insert_one(doc)
|
88 |
+
|
89 |
+
# Prediction Page UI
|
90 |
+
def show():
|
91 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π¬ Polymer Property Prediction</h1>", unsafe_allow_html=True)
|
92 |
+
st.markdown("<hr style='border: 1px solid #ccc;'>", unsafe_allow_html=True)
|
93 |
+
|
94 |
+
smiles_input = st.text_input("Enter SMILES Representation of Polymer")
|
95 |
+
|
96 |
+
if st.button("Predict"):
|
97 |
+
fixed = fix_smiles(smiles_input)
|
98 |
+
if not fixed:
|
99 |
+
st.error("Invalid SMILES string.")
|
100 |
+
else:
|
101 |
+
features = compute_features(fixed)
|
102 |
+
features_tensor = torch.tensor(features, dtype=torch.float32).unsqueeze(0)
|
103 |
+
embedding = embed_smiles([fixed])
|
104 |
+
|
105 |
+
with torch.no_grad():
|
106 |
+
pred = model(embedding, features_tensor)
|
107 |
+
result = pred.numpy().flatten()
|
108 |
+
|
109 |
+
properties = [
|
110 |
+
"Tensile Strength",
|
111 |
+
"Ionization Energy",
|
112 |
+
"Electron Affinity",
|
113 |
+
"logP",
|
114 |
+
"Refractive Index",
|
115 |
+
"Molecular Weight"
|
116 |
+
]
|
117 |
+
|
118 |
+
predictions = {}
|
119 |
+
st.success("Predicted Polymer Properties:")
|
120 |
+
for prop, val in zip(properties, result):
|
121 |
+
st.write(f"**{prop}**: {val:.4f}")
|
122 |
+
predictions[prop] = val
|
123 |
+
|
124 |
+
# Save the prediction to MongoDB
|
125 |
+
save_to_db(smiles_input, predictions)
|
126 |
+
st.success("Prediction saved successfully!")
|