Spaces:
Running
Running
Upload prediction.py
Browse files- prediction.py +73 -0
prediction.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from rdkit import Chem
|
4 |
+
import datetime
|
5 |
+
from db import get_database
|
6 |
+
|
7 |
+
# Function to validate SMILES string
|
8 |
+
def is_valid_smiles(smiles):
|
9 |
+
""" Validate if the input is a valid SMILES string using RDKit """
|
10 |
+
mol = Chem.MolFromSmiles(smiles)
|
11 |
+
return mol is not None
|
12 |
+
|
13 |
+
# Function to save prediction to MongoDB
|
14 |
+
def save_to_db(smiles_input, predictions):
|
15 |
+
db = get_database()
|
16 |
+
collection = db["polymers"] # your collection
|
17 |
+
doc = {
|
18 |
+
"smiles": smiles_input,
|
19 |
+
"predictions": predictions,
|
20 |
+
"timestamp": datetime.datetime.utcnow()
|
21 |
+
}
|
22 |
+
collection.insert_one(doc)
|
23 |
+
|
24 |
+
# Streamlit page for Polymer Property Prediction
|
25 |
+
def show():
|
26 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>π¬ Polymer Property Prediction</h1>", unsafe_allow_html=True)
|
27 |
+
st.markdown("<hr style='border: 1px solid #ccc;'>", unsafe_allow_html=True)
|
28 |
+
|
29 |
+
# Input box with placeholder inside
|
30 |
+
input_text = st.text_input(
|
31 |
+
label="",
|
32 |
+
placeholder="Enter SMILES ",
|
33 |
+
key="smiles_input"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Show Predict button always
|
37 |
+
predict_clicked = st.button("π Predict", use_container_width=True)
|
38 |
+
|
39 |
+
# Predict on button click OR on pressing Enter with input
|
40 |
+
if (predict_clicked or input_text) and input_text.strip():
|
41 |
+
with st.spinner("Predicting..."):
|
42 |
+
if is_valid_smiles(input_text.strip()):
|
43 |
+
try:
|
44 |
+
input_data = {
|
45 |
+
"smiles": input_text.strip() # Only sending smiles
|
46 |
+
}
|
47 |
+
response = requests.post("http://127.0.0.1:8000/predict", json=input_data)
|
48 |
+
if response.status_code == 200:
|
49 |
+
result = response.json()
|
50 |
+
renamed_properties = {
|
51 |
+
"property1": "Tensile_strength (Mpa)",
|
52 |
+
"property2": "Ionization_Energy (eV)",
|
53 |
+
"property3": "Electron_Affinity (eV)",
|
54 |
+
"property4": "LogP",
|
55 |
+
"property5": "Refractive_Index",
|
56 |
+
"property6": "Molecular_Weight (g/mol)"
|
57 |
+
}
|
58 |
+
predictions = {}
|
59 |
+
for key, name in renamed_properties.items():
|
60 |
+
value = result.get(key, 'N/A')
|
61 |
+
st.markdown(f"<div style='font-size:18px; padding: 6px 0;'><strong>{name}:</strong> {value}</div>", unsafe_allow_html=True)
|
62 |
+
predictions[name] = value
|
63 |
+
|
64 |
+
# Save prediction to MongoDB
|
65 |
+
save_to_db(input_text.strip(), predictions)
|
66 |
+
|
67 |
+
st.success("Prediction saved successfully!")
|
68 |
+
else:
|
69 |
+
st.error("Prediction failed. Please try again.")
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Error: {e}")
|
72 |
+
else:
|
73 |
+
st.error("β Invalid SMILES input. Please enter a correct SMILES string.")
|