forked from ChangwenXu98/TransPolymer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
375 lines (317 loc) · 13.2 KB
/
app.py
File metadata and controls
375 lines (317 loc) · 13.2 KB
1
2
3
4
5
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import streamlit as st
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import json
import os
from copy import deepcopy
import warnings
warnings.filterwarnings("ignore")
# Import custom modules
from PolymerSmilesTokenization import PolymerSmilesTokenizer
from transformers import RobertaModel, RobertaConfig, RobertaTokenizer
# Page configuration
st.set_page_config(
page_title="TransPolymer - Polymer Property Predictor",
page_icon="🧪",
layout="wide",
initial_sidebar_state="expanded"
)
# Model definition (from Downstream.py)
class DownstreamRegression(nn.Module):
def __init__(self, pretrained_model, drop_rate=0.1):
super(DownstreamRegression, self).__init__()
self.PretrainedModel = deepcopy(pretrained_model)
self.Regressor = nn.Sequential(
nn.Dropout(drop_rate),
nn.Linear(self.PretrainedModel.config.hidden_size, self.PretrainedModel.config.hidden_size),
nn.SiLU(),
nn.Linear(self.PretrainedModel.config.hidden_size, 1)
)
def forward(self, input_ids, attention_mask):
outputs = self.PretrainedModel(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs.last_hidden_state[:, 0, :]
output = self.Regressor(logits)
return output
# Property information
PROPERTY_INFO = {
'PE_I': {
'name': 'Polymer Electrolyte Conductivity I',
'units': 'S/cm',
'description': 'Ionic conductivity of polymer electrolytes',
'example_smiles': 'CC(C)(C)OC(=O)NC1=CC=CC=C1'
},
'PE_II': {
'name': 'Polymer Electrolyte Conductivity II',
'units': 'S/cm',
'description': 'Alternative polymer electrolyte conductivity dataset',
'example_smiles': 'CC(C)OC(=O)NC1=CC=C(C=C1)O'
},
'OPV': {
'name': 'Organic Photovoltaic Efficiency',
'units': '%',
'description': 'Power conversion efficiency in organic solar cells',
'example_smiles': 'c1ccc(cc1)c2ccc(cc2)C3=CC=C(C=C3)c4ccccc4'
},
'Egc': {
'name': 'Band Gap (Egc)',
'units': 'eV',
'description': 'Electronic band gap of the polymer',
'example_smiles': 'c1ccc2c(c1)oc1ccccc12'
},
'Egb': {
'name': 'Band Gap (Egb)',
'units': 'eV',
'description': 'Alternative band gap measurement',
'example_smiles': 'c1ccc2c(c1)sc1ccccc12'
},
'Eea': {
'name': 'Electron Affinity',
'units': 'eV',
'description': 'Energy released when electron is added',
'example_smiles': 'c1ccc2c(c1)c1ccccc1n2'
},
'Ei': {
'name': 'Ionization Energy',
'units': 'eV',
'description': 'Energy required to remove an electron',
'example_smiles': 'c1ccc2c(c1)c1ccccc1o2'
},
'Xc': {
'name': 'Crystallization Tendency',
'units': '-',
'description': 'Tendency of polymer to crystallize',
'example_smiles': 'CC(C)CC(C)(C)c1ccccc1'
},
'EPS': {
'name': 'Dielectric Constant',
'units': '-',
'description': 'Relative permittivity of the polymer',
'example_smiles': 'c1ccc(cc1)C(c2ccccc2)c3ccccc3'
},
'Nc': {
'name': 'Refractive Index',
'units': '-',
'description': 'Measure of light bending in the polymer',
'example_smiles': 'c1ccc(cc1)C#Cc2ccccc2'
}
}
@st.cache_resource
def load_model():
"""Load the TransPolymer model (cached for performance)"""
try:
# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load tokenizer
try:
# Try to load custom tokenizer if files exist
if os.path.exists("ckpt/pretrain.pt") and os.path.exists("ckpt/pretrain.pt"):
tokenizer = PolymerSmilesTokenizer.from_pretrained("roberta-base")
else:
# Fallback to RoBERTa base
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
except:
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
# Load model configuration
model_config = {
"hidden_size": 768,
"num_hidden_layers": 6,
"num_attention_heads": 12,
"intermediate_size": 3072,
"vocab_size": 50265
}
# Create model
config = RobertaConfig(**model_config)
pretrained_model = RobertaModel(config)
# Load pretrained weights if available
if os.path.exists("ckpt/pretrain.pt/pytorch_model.bin"):
try:
state_dict = torch.load("ckpt/pretrain.pt/pytorch_model.bin", map_location=device)
pretrained_model.load_state_dict(state_dict, strict=False)
st.success("✅ Loaded pretrained weights!")
except:
st.warning("⚠️ Using random weights (pretrained weights not found)")
else:
st.warning("⚠️ Using random weights (for demo purposes)")
# Create downstream model
model = DownstreamRegression(pretrained_model)
model.PretrainedModel.resize_token_embeddings(len(tokenizer))
model.to(device)
model.eval()
return model, tokenizer, device
except Exception as e:
st.error(f"Error loading model: {e}")
return None, None, None
def predict_property(smiles, property_type, model, tokenizer, device):
"""Make prediction for a SMILES string"""
try:
# Tokenize SMILES
encoded = tokenizer(
[smiles],
padding=True,
truncation=True,
max_length=411,
return_tensors="pt"
)
# Move to device
input_ids = encoded["input_ids"].to(device)
attention_mask = encoded["attention_mask"].to(device)
# Make prediction
with torch.no_grad():
outputs = model(input_ids, attention_mask)
prediction = outputs.cpu().numpy().flatten()[0]
return float(prediction)
except Exception as e:
st.error(f"Error making prediction: {e}")
return None
def validate_smiles(smiles):
"""Basic SMILES validation"""
if not smiles or len(smiles.strip()) == 0:
return False, "SMILES string cannot be empty"
# Basic chemical structure validation
invalid_chars = set(smiles) - set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()[]=#@+-.")
if invalid_chars:
return False, f"Invalid characters in SMILES: {invalid_chars}"
return True, "Valid SMILES string"
def main():
"""Main Streamlit application"""
# Header
st.title("🧪 TransPolymer: Polymer Property Predictor")
st.markdown("""
**TransPolymer** is a Transformer-based language model for polymer property predictions.
Enter a polymer SMILES string and select a property to predict its value.
📄 [Paper](https://www.nature.com/articles/s41524-023-01016-5) |
💻 [Code](https://github.com/ChangwenXu98/TransPolymer)
""")
# Load model
with st.spinner("Loading TransPolymer model..."):
model, tokenizer, device = load_model()
if model is None:
st.error("Failed to load model. Please check the model files.")
return
# Sidebar for property selection
with st.sidebar:
st.header("🎯 Property Selection")
property_type = st.selectbox(
"Select property to predict:",
options=list(PROPERTY_INFO.keys()),
format_func=lambda x: f"{x}: {PROPERTY_INFO[x]['name']}"
)
# Display property information
prop_info = PROPERTY_INFO[property_type]
st.info(f"""
**{prop_info['name']}**
{prop_info['description']}
**Units:** {prop_info['units']}
""")
# Example SMILES
st.subheader("📝 Example")
if st.button("Use Example SMILES"):
st.session_state.example_smiles = prop_info['example_smiles']
# Main interface
col1, col2 = st.columns([2, 1])
with col1:
st.header("📮 Input")
# SMILES input
smiles_input = st.text_area(
"Enter polymer SMILES string:",
value=st.session_state.get('example_smiles', ''),
height=100,
placeholder="e.g., CC(C)(C)OC(=O)NC1=CC=CC=C1",
help="SMILES (Simplified Molecular Input Line Entry System) notation for the polymer structure"
)
# Validation
if smiles_input:
is_valid, message = validate_smiles(smiles_input)
if is_valid:
st.success(f"✅ {message}")
else:
st.error(f"❌ {message}")
# Predict button
predict_button = st.button("🔮 Predict Property", type="primary", disabled=not smiles_input)
with col2:
st.header("📊 Results")
if predict_button and smiles_input:
with st.spinner("Making prediction..."):
prediction = predict_property(smiles_input, property_type, model, tokenizer, device)
if prediction is not None:
prop_info = PROPERTY_INFO[property_type]
# Display prediction
st.metric(
label=f"{prop_info['name']}",
value=f"{prediction:.2e}" if abs(prediction) < 0.01 else f"{prediction:.3f}",
delta=None
)
st.info(f"**Units:** {prop_info['units']}")
# Additional info
with st.expander("📋 Prediction Details"):
st.json({
"smiles": smiles_input,
"property": property_type,
"prediction": float(prediction),
"units": prop_info['units'],
"model": "TransPolymer",
"note": "This is a research model for demonstration purposes"
})
# Additional sections
st.markdown("---")
# Batch prediction
with st.expander("📊 Batch Predictions"):
st.markdown("Upload a CSV file with SMILES strings for batch prediction:")
uploaded_file = st.file_uploader("Choose CSV file", type="csv")
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
st.write("Preview of uploaded data:")
st.dataframe(df.head())
if 'smiles' in df.columns:
if st.button("Run Batch Prediction"):
with st.spinner("Processing batch predictions..."):
predictions = []
progress_bar = st.progress(0)
for i, smiles in enumerate(df['smiles']):
pred = predict_property(smiles, property_type, model, tokenizer, device)
predictions.append(pred)
progress_bar.progress((i + 1) / len(df))
df[f'predicted_{property_type}'] = predictions
st.success("✅ Batch prediction completed!")
st.dataframe(df)
# Download results
csv = df.to_csv(index=False)
st.download_button(
label="📥 Download Results",
data=csv,
file_name=f"transpolymer_predictions_{property_type}.csv",
mime="text/csv"
)
else:
st.error("CSV file must contain a 'smiles' column")
except Exception as e:
st.error(f"Error processing file: {e}")
# About section
with st.expander("ℹ️ About TransPolymer"):
st.markdown("""
**TransPolymer** is a Transformer-based language model for polymer property predictions developed by researchers at Carnegie Mellon University.
**Key Features:**
- 6-layer RoBERTa architecture with 768 hidden dimensions
- Pretrained on ~5 million polymer SMILES sequences
- Custom chemical-aware tokenization
- Supports 10+ different polymer properties
**Citation:**
```Q
@article{xu2023transpolymer,
title={TransPolymer: a Transformer-based language model for polymer property predictions},
author={Xu, Changwen and Wang, Yuyang and Barati Farimani, Amir},
journal={npj Computational Materials},
volume={9},
number={1},
pages={64},
year={2023},
publisher={Nature Publishing Group UK London}
}
```
**Disclaimer:** This is a research model for demonstration purposes. Results should be validated experimentally.
""")
if __name__ == "__main__":
main()