Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +436 -185
src/streamlit_app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
# app.py -- Public Gateway (Full UI Replica)
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import streamlit as st
|
| 5 |
import json
|
| 6 |
import pandas as pd
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
st.set_page_config(page_title="Gateway Demo for Reviewers", layout="
|
| 10 |
|
| 11 |
# ============ Configuration ============
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
@@ -17,95 +20,336 @@ if not HF_TOKEN or not PRIVATE_SPACE_API:
|
|
| 17 |
st.info("Required Secrets:\n- HF_TOKEN: Your HuggingFace write token\n- PRIVATE_SPACE_API: https://ytbai-mof-hydrogen-storage-capacity-prediction-model.hf.space/predict")
|
| 18 |
st.stop()
|
| 19 |
|
| 20 |
-
# ============ UI
|
| 21 |
-
|
| 22 |
-
<
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
</p>
|
| 28 |
-
<hr style='margin-top: 0.5rem; margin-bottom: 0.5rem;'>
|
| 29 |
-
<p style='font-size: 1.05em; color: #333;'>
|
| 30 |
-
Upload and analyze metalβorganic framework (MOF) CIF files to estimate hydrogen storage capacities under given thermodynamic conditions.
|
| 31 |
-
</p>
|
| 32 |
-
""", unsafe_allow_html=True)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
sample_file_path = "src/NU-1501-Al.cif"
|
| 39 |
sample_file_exists = os.path.exists(sample_file_path)
|
| 40 |
|
| 41 |
-
# Initialize session state for checkbox if not exists
|
| 42 |
if 'use_sample' not in st.session_state:
|
| 43 |
st.session_state.use_sample = False
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
with
|
| 48 |
-
|
| 49 |
use_sample = st.checkbox(
|
| 50 |
-
"Use sample file (NU-1501-Al.cif)",
|
| 51 |
value=st.session_state.use_sample,
|
| 52 |
key="sample_checkbox"
|
| 53 |
)
|
| 54 |
st.session_state.use_sample = use_sample
|
| 55 |
-
|
| 56 |
-
if use_sample and not sample_file_exists:
|
| 57 |
-
st.error("β Sample file not found. Please upload 'NU-1501-Al.cif' to this space.")
|
| 58 |
-
|
| 59 |
-
with col2:
|
| 60 |
-
pass
|
| 61 |
-
|
| 62 |
-
with col2:
|
| 63 |
-
pass
|
| 64 |
-
|
| 65 |
-
cif_text = None
|
| 66 |
-
filename = None
|
| 67 |
-
|
| 68 |
-
# ============ Load Sample or Upload File ============
|
| 69 |
-
if st.session_state.use_sample and sample_file_exists:
|
| 70 |
-
try:
|
| 71 |
-
with open(sample_file_path, 'r', encoding='utf-8') as f:
|
| 72 |
-
cif_text = f.read()
|
| 73 |
-
filename = "NU-1501-Al"
|
| 74 |
-
st.success(f"β
Loaded sample file: {sample_file_path}")
|
| 75 |
-
except Exception as e:
|
| 76 |
-
st.error(f"Error loading sample file: {str(e)}")
|
| 77 |
-
cif_text = None
|
| 78 |
-
filename = None
|
| 79 |
-
else:
|
| 80 |
-
st.header("Upload CIF File")
|
| 81 |
-
file_upload = st.file_uploader(
|
| 82 |
-
"Select a CIF file to upload",
|
| 83 |
-
type="cif",
|
| 84 |
-
accept_multiple_files=False,
|
| 85 |
-
key="file_uploader"
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
if
|
| 89 |
-
|
| 90 |
-
if file_size_mb > 5:
|
| 91 |
-
st.error(f"β The uploaded file '{file_upload.name}' is too large ({file_size_mb:.2f} MB). Please upload a file smaller than 5 MB.")
|
| 92 |
-
else:
|
| 93 |
-
st.success(f"β
Successfully uploaded file: {file_upload.name} ({file_size_mb:.2f} MB)")
|
| 94 |
try:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
filename =
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
st.error(f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# ============ Display CIF Preview ============
|
| 102 |
if cif_text:
|
| 103 |
|
| 104 |
-
# ============ Input Properties ============
|
| 105 |
-
st.header("Input Properties")
|
| 106 |
-
st.info("Enter property values (VF, VSA, GSA, PLD, LCD) for the sample.")
|
| 107 |
-
|
| 108 |
-
# Default values for sample file
|
| 109 |
if st.session_state.use_sample and sample_file_exists:
|
| 110 |
default_values = {
|
| 111 |
'VF': 0.837,
|
|
@@ -122,125 +366,132 @@ if cif_text:
|
|
| 122 |
'PLD': 0.0,
|
| 123 |
'LCD': 0.0
|
| 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 |
-
st.error(f"β Request failed: {e}")
|
| 172 |
-
else:
|
| 173 |
-
if resp.status_code != 200:
|
| 174 |
-
st.error(f"β Private Space returned status {resp.status_code}")
|
| 175 |
-
|
| 176 |
-
try:
|
| 177 |
-
error_data = resp.json()
|
| 178 |
-
st.json(error_data)
|
| 179 |
-
except Exception:
|
| 180 |
-
st.code(resp.text)
|
| 181 |
-
|
| 182 |
-
if resp.status_code == 403:
|
| 183 |
-
st.warning("""
|
| 184 |
-
**403 Forbidden - Possible causes:**
|
| 185 |
-
- Token may not have correct permissions (needs Write access)
|
| 186 |
-
- Token may be expired
|
| 187 |
-
- Private space may not be running
|
| 188 |
-
""")
|
| 189 |
-
elif resp.status_code == 404:
|
| 190 |
-
st.warning(f"""
|
| 191 |
-
**404 Not Found - Check API URL:**
|
| 192 |
-
Current: `{PRIVATE_SPACE_API}`
|
| 193 |
-
Should be: `https://ytbai-mof-hydrogen-storage-capacity-prediction-model.hf.space/predict`
|
| 194 |
-
""")
|
| 195 |
else:
|
| 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 |
else:
|
| 237 |
-
st.
|
| 238 |
st.json(data)
|
| 239 |
-
else:
|
| 240 |
-
st.error("β Private Space returned failure.")
|
| 241 |
-
st.json(data)
|
| 242 |
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py -- Public Gateway (Full UI Replica)
|
| 2 |
import os
|
| 3 |
+
import base64
|
| 4 |
+
from contextlib import contextmanager
|
| 5 |
+
|
| 6 |
import requests
|
| 7 |
import streamlit as st
|
| 8 |
import json
|
| 9 |
import pandas as pd
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
+
st.set_page_config(page_title="Gateway Demo for Reviewers", layout="wide")
|
| 13 |
|
| 14 |
# ============ Configuration ============
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 20 |
st.info("Required Secrets:\n- HF_TOKEN: Your HuggingFace write token\n- PRIVATE_SPACE_API: https://ytbai-mof-hydrogen-storage-capacity-prediction-model.hf.space/predict")
|
| 21 |
st.stop()
|
| 22 |
|
| 23 |
+
# ============ UI Styling (mirroring index.html) ============
|
| 24 |
+
logo_path = "src/logo.png"
|
| 25 |
+
logo_element = "<span class='logo-placeholder'>βοΈ</span>"
|
| 26 |
+
if os.path.exists(logo_path):
|
| 27 |
+
with open(logo_path, "rb") as logo_file:
|
| 28 |
+
encoded_logo = base64.b64encode(logo_file.read()).decode()
|
| 29 |
+
logo_element = f"<img src='data:image/png;base64,{encoded_logo}' alt='MOF Logo' />"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
custom_css = """
|
| 32 |
+
<style>
|
| 33 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');
|
| 34 |
+
|
| 35 |
+
html, body, [class*="css"] {
|
| 36 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.stApp {
|
| 40 |
+
background: radial-gradient(circle at top, rgba(33, 150, 243, 0.15), transparent 45%) #0a0e27;
|
| 41 |
+
color: #f7fafc;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.stApp [data-testid="stAppViewContainer"] > .main {
|
| 45 |
+
padding: 0 1.5rem 4rem 1.5rem;
|
| 46 |
+
max-width: 1200px;
|
| 47 |
+
margin: 0 auto;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.hero {
|
| 51 |
+
text-align: center;
|
| 52 |
+
color: white;
|
| 53 |
+
margin-bottom: 40px;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
.hero h1 {
|
| 57 |
+
font-size: 3.2em;
|
| 58 |
+
font-weight: 800;
|
| 59 |
+
margin-bottom: 15px;
|
| 60 |
+
text-shadow: 0 4px 20px rgba(0,0,0,0.3);
|
| 61 |
+
letter-spacing: -1px;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.hero h1 img,
|
| 65 |
+
.hero h1 .logo-placeholder {
|
| 66 |
+
height: 1em;
|
| 67 |
+
width: auto;
|
| 68 |
+
vertical-align: middle;
|
| 69 |
+
margin-right: 15px;
|
| 70 |
+
display: inline-block;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.hero .tagline {
|
| 74 |
+
font-size: 1.1em;
|
| 75 |
+
opacity: 0.95;
|
| 76 |
+
font-weight: 300;
|
| 77 |
+
text-shadow: 0 2px 10px rgba(0,0,0,0.2);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.hero .badge {
|
| 81 |
+
display: inline-block;
|
| 82 |
+
background: rgba(255,255,255,0.2);
|
| 83 |
+
backdrop-filter: blur(10px);
|
| 84 |
+
padding: 8px 20px;
|
| 85 |
+
border-radius: 50px;
|
| 86 |
+
margin-top: 20px;
|
| 87 |
+
font-size: 0.9em;
|
| 88 |
+
border: 1px solid rgba(255,255,255,0.3);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
.glass-card {
|
| 92 |
+
background: rgba(255,255,255,0.95);
|
| 93 |
+
border-radius: 24px;
|
| 94 |
+
box-shadow: 0 20px 50px rgba(0,0,0,0.35);
|
| 95 |
+
padding: 35px;
|
| 96 |
+
margin-bottom: 30px;
|
| 97 |
+
border: 1px solid rgba(255,255,255,0.4);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.section-header {
|
| 101 |
+
display: flex;
|
| 102 |
+
align-items: center;
|
| 103 |
+
gap: 15px;
|
| 104 |
+
margin-bottom: 25px;
|
| 105 |
+
padding-bottom: 15px;
|
| 106 |
+
border-bottom: 2px solid #e2e8f0;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
.section-icon {
|
| 110 |
+
width: 50px;
|
| 111 |
+
height: 50px;
|
| 112 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
| 113 |
+
border-radius: 14px;
|
| 114 |
+
display: flex;
|
| 115 |
+
align-items: center;
|
| 116 |
+
justify-content: center;
|
| 117 |
+
font-size: 1.5em;
|
| 118 |
+
color: white;
|
| 119 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.5);
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
.section-title h2 {
|
| 123 |
+
font-size: 1.6em;
|
| 124 |
+
font-weight: 700;
|
| 125 |
+
color: #2d3748;
|
| 126 |
+
margin: 0;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.section-title p {
|
| 130 |
+
color: #718096;
|
| 131 |
+
font-size: 0.95em;
|
| 132 |
+
margin: 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.alert {
|
| 136 |
+
padding: 18px 24px;
|
| 137 |
+
border-radius: 14px;
|
| 138 |
+
margin-bottom: 20px;
|
| 139 |
+
font-weight: 500;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.alert-info {
|
| 143 |
+
background: linear-gradient(135deg, #e6fffa, #b2f5ea);
|
| 144 |
+
border-left: 5px solid #38b2ac;
|
| 145 |
+
color: #234e52;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
.alert-warning {
|
| 149 |
+
background: linear-gradient(135deg, #fefcbf, #faf089);
|
| 150 |
+
border-left: 5px solid #d69e2e;
|
| 151 |
+
color: #744210;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.alert-success {
|
| 155 |
+
background: linear-gradient(135deg, #c6f6d5, #9ae6b4);
|
| 156 |
+
border-left: 5px solid #38a169;
|
| 157 |
+
color: #22543d;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.alert-error {
|
| 161 |
+
background: linear-gradient(135deg, #fed7d7, #fc8181);
|
| 162 |
+
border-left: 5px solid #e53e3e;
|
| 163 |
+
color: #742a2a;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
div.stButton > button {
|
| 167 |
+
width: 100%;
|
| 168 |
+
padding: 18px 40px;
|
| 169 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
| 170 |
+
color: white;
|
| 171 |
+
border: none;
|
| 172 |
+
border-radius: 14px;
|
| 173 |
+
font-size: 1.1em;
|
| 174 |
+
font-weight: 700;
|
| 175 |
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
|
| 176 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
div.stButton > button:hover {
|
| 180 |
+
transform: translateY(-3px);
|
| 181 |
+
box-shadow: 0 12px 30px rgba(102, 126, 234, 0.5);
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
div.stButton > button:disabled {
|
| 185 |
+
background: linear-gradient(135deg, #cbd5e0, #a0aec0);
|
| 186 |
+
box-shadow: none;
|
| 187 |
+
transform: none;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
div[data-testid="stFileUploader"] {
|
| 191 |
+
background: #f7fafc;
|
| 192 |
+
border: 3px dashed #cbd5e0;
|
| 193 |
+
border-radius: 20px;
|
| 194 |
+
padding: 40px 30px;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.info-box {
|
| 198 |
+
background: linear-gradient(135deg, #f7fafc, #edf2f7);
|
| 199 |
+
padding: 30px;
|
| 200 |
+
border-radius: 16px;
|
| 201 |
+
margin-top: 30px;
|
| 202 |
+
border: 2px solid #e2e8f0;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.info-box h4 {
|
| 206 |
+
margin-top: 0;
|
| 207 |
+
color: #2d3748;
|
| 208 |
+
font-weight: 700;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.info-box ul {
|
| 212 |
+
list-style: none;
|
| 213 |
+
padding-left: 0;
|
| 214 |
+
margin: 0;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
.info-box li {
|
| 218 |
+
padding: 10px 0;
|
| 219 |
+
color: #4a5568;
|
| 220 |
+
border-bottom: 1px solid #e2e8f0;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
.info-box li:last-child {
|
| 224 |
+
border-bottom: none;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.footer {
|
| 228 |
+
text-align: center;
|
| 229 |
+
padding: 30px 20px 60px;
|
| 230 |
+
color: rgba(255,255,255,0.85);
|
| 231 |
+
font-size: 0.9em;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
.footer a {
|
| 235 |
+
color: rgba(255,255,255,0.95);
|
| 236 |
+
text-decoration: none;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
.footer a:hover {
|
| 240 |
+
color: #93c5fd;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
.footer .contact,
|
| 244 |
+
.footer .attribution {
|
| 245 |
+
opacity: 0.8;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
@media (max-width: 768px) {
|
| 249 |
+
.hero h1 {
|
| 250 |
+
font-size: 2.4em;
|
| 251 |
+
}
|
| 252 |
+
.glass-card {
|
| 253 |
+
padding: 25px;
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
</style>
|
| 257 |
+
"""
|
| 258 |
+
|
| 259 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 260 |
|
| 261 |
+
hero_html = f"""
|
| 262 |
+
<div class="hero">
|
| 263 |
+
<h1>{logo_element}MOF Hβ Storage Predictor</h1>
|
| 264 |
+
<p class="tagline">Advanced machine learning for hydrogen storage capacity prediction</p>
|
| 265 |
+
<div class="badge">77 K / 100 bar β 160 K / 5 bar</div>
|
| 266 |
+
</div>
|
| 267 |
+
"""
|
| 268 |
+
st.markdown(hero_html, unsafe_allow_html=True)
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
@contextmanager
|
| 272 |
+
def glass_card(icon: str, title: str, subtitle: str):
|
| 273 |
+
st.markdown(
|
| 274 |
+
f"""
|
| 275 |
+
<div class="glass-card">
|
| 276 |
+
<div class="section-header">
|
| 277 |
+
<div class="section-icon">{icon}</div>
|
| 278 |
+
<div class="section-title">
|
| 279 |
+
<h2>{title}</h2>
|
| 280 |
+
<p>{subtitle}</p>
|
| 281 |
+
</div>
|
| 282 |
+
</div>
|
| 283 |
+
""",
|
| 284 |
+
unsafe_allow_html=True,
|
| 285 |
+
)
|
| 286 |
+
try:
|
| 287 |
+
yield
|
| 288 |
+
finally:
|
| 289 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def render_alert(message: str, level: str = "info"):
|
| 293 |
+
st.markdown(f"<div class='alert alert-{level}'>{message}</div>", unsafe_allow_html=True)
|
| 294 |
+
|
| 295 |
+
# ============ Sample Source Selection ============
|
| 296 |
sample_file_path = "src/NU-1501-Al.cif"
|
| 297 |
sample_file_exists = os.path.exists(sample_file_path)
|
| 298 |
|
|
|
|
| 299 |
if 'use_sample' not in st.session_state:
|
| 300 |
st.session_state.use_sample = False
|
| 301 |
|
| 302 |
+
cif_text = None
|
| 303 |
+
filename = None
|
| 304 |
|
| 305 |
+
with glass_card("π", "Choose Input Method", "Select the provided sample structure or upload your own CIF file"):
|
| 306 |
+
render_alert("π Use the sample NU-1501-Al structure or upload a custom MOF CIF (max 5 MB).", "info")
|
| 307 |
use_sample = st.checkbox(
|
| 308 |
+
"Use sample file (NU-1501-Al.cif)",
|
| 309 |
value=st.session_state.use_sample,
|
| 310 |
key="sample_checkbox"
|
| 311 |
)
|
| 312 |
st.session_state.use_sample = use_sample
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
+
if use_sample:
|
| 315 |
+
if sample_file_exists:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
try:
|
| 317 |
+
with open(sample_file_path, 'r', encoding='utf-8') as f:
|
| 318 |
+
cif_text = f.read()
|
| 319 |
+
filename = "NU-1501-Al"
|
| 320 |
+
st.success("β
Loaded sample file: NU-1501-Al.cif")
|
| 321 |
except Exception as e:
|
| 322 |
+
st.error(f"Error loading sample file: {str(e)}")
|
| 323 |
+
cif_text = None
|
| 324 |
+
filename = None
|
| 325 |
+
else:
|
| 326 |
+
st.error("β Sample file not found. Please upload 'src/NU-1501-Al.cif' to this workspace.")
|
| 327 |
+
|
| 328 |
+
if not st.session_state.use_sample:
|
| 329 |
+
with glass_card("π€", "Upload CIF File", "Drop your CIF file here or click to browse (Maximum 5 MB)"):
|
| 330 |
+
file_upload = st.file_uploader(
|
| 331 |
+
"Select a CIF file to upload",
|
| 332 |
+
type="cif",
|
| 333 |
+
accept_multiple_files=False,
|
| 334 |
+
key="file_uploader"
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
if file_upload is not None:
|
| 338 |
+
file_size_mb = file_upload.size / (1024 * 1024)
|
| 339 |
+
if file_size_mb > 5:
|
| 340 |
+
st.error(f"β The uploaded file '{file_upload.name}' is too large ({file_size_mb:.2f} MB). Please upload a file smaller than 5 MB.")
|
| 341 |
+
else:
|
| 342 |
+
st.success(f"β
Successfully uploaded file: {file_upload.name} ({file_size_mb:.2f} MB)")
|
| 343 |
+
try:
|
| 344 |
+
raw = file_upload.read()
|
| 345 |
+
cif_text = raw.decode("utf-8", errors='replace')
|
| 346 |
+
filename = os.path.splitext(file_upload.name)[0]
|
| 347 |
+
except Exception as e:
|
| 348 |
+
st.error(f"Error reading file: {str(e)}")
|
| 349 |
|
| 350 |
# ============ Display CIF Preview ============
|
| 351 |
if cif_text:
|
| 352 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
if st.session_state.use_sample and sample_file_exists:
|
| 354 |
default_values = {
|
| 355 |
'VF': 0.837,
|
|
|
|
| 366 |
'PLD': 0.0,
|
| 367 |
'LCD': 0.0
|
| 368 |
}
|
| 369 |
+
|
| 370 |
+
with glass_card("βοΈ", "Structural Properties", f"Configure MOF characteristics for {filename}"):
|
| 371 |
+
render_alert("π‘ All properties must be filled. Empty or zero values may result in inaccurate predictions.", "info")
|
| 372 |
+
|
| 373 |
+
col1, col2 = st.columns(2)
|
| 374 |
+
vf = col1.number_input("VF (Void Fraction)", value=default_values['VF'], key="vf")
|
| 375 |
+
vsa = col2.number_input("VSA (Volume Specific Area, mΒ²/cmΒ³)", value=default_values['VSA'], key="vsa")
|
| 376 |
+
gsa = col1.number_input("GSA (Gravimetric Surface Area, mΒ²/g)", value=default_values['GSA'], key="gsa")
|
| 377 |
+
pld = col2.number_input("PLD (Pore Limiting Diameter, Γ
)", value=default_values['PLD'], key="pld")
|
| 378 |
+
lcd = col1.number_input("LCD (Largest Cavity Diameter, Γ
)", value=default_values['LCD'], key="lcd")
|
| 379 |
+
|
| 380 |
+
with glass_card("π", "Model Prediction", "Run hydrogen storage predictions for your structure"):
|
| 381 |
+
render_alert("β° <strong>First Run Notice:</strong> The computation service sleeps after 48h of inactivity. Initial wake-up may take 1-2 minutes.", "warning")
|
| 382 |
+
|
| 383 |
+
if st.button("π Run Predictions"):
|
| 384 |
+
with st.spinner("Sending request to private space..."):
|
| 385 |
+
# Prepare payload with CIF and properties
|
| 386 |
+
payload = {
|
| 387 |
+
"cif": cif_text,
|
| 388 |
+
"properties": {
|
| 389 |
+
"VF": vf if vf != 0.0 else None,
|
| 390 |
+
"VSA": vsa if vsa != 0.0 else None,
|
| 391 |
+
"GSA": gsa if gsa != 0.0 else None,
|
| 392 |
+
"PLD": pld if pld != 0.0 else None,
|
| 393 |
+
"LCD": lcd if lcd != 0.0 else None
|
| 394 |
+
}
|
| 395 |
}
|
| 396 |
+
|
| 397 |
+
headers = {
|
| 398 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 399 |
+
"Content-Type": "application/json",
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
try:
|
| 403 |
+
resp = requests.post(
|
| 404 |
+
PRIVATE_SPACE_API,
|
| 405 |
+
headers=headers,
|
| 406 |
+
json=payload,
|
| 407 |
+
timeout=180
|
| 408 |
+
)
|
| 409 |
+
except requests.exceptions.Timeout:
|
| 410 |
+
st.error("β±οΈ Request timeout. The model may be taking too long to respond.")
|
| 411 |
+
except requests.exceptions.ConnectionError as e:
|
| 412 |
+
st.error(f"π Connection failed: {e}")
|
| 413 |
+
except Exception as e:
|
| 414 |
+
st.error(f"β Request failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
else:
|
| 416 |
+
if resp.status_code != 200:
|
| 417 |
+
st.error(f"β Private Space returned status {resp.status_code}")
|
| 418 |
+
|
| 419 |
+
try:
|
| 420 |
+
error_data = resp.json()
|
| 421 |
+
st.json(error_data)
|
| 422 |
+
except Exception:
|
| 423 |
+
st.code(resp.text)
|
| 424 |
+
|
| 425 |
+
if resp.status_code == 403:
|
| 426 |
+
st.warning("""
|
| 427 |
+
**403 Forbidden - Possible causes:**
|
| 428 |
+
- Token may not have correct permissions (needs Write access)
|
| 429 |
+
- Token may be expired
|
| 430 |
+
- Private space may not be running
|
| 431 |
+
""")
|
| 432 |
+
elif resp.status_code == 404:
|
| 433 |
+
st.warning(f"""
|
| 434 |
+
**404 Not Found - Check API URL:**
|
| 435 |
+
Current: `{PRIVATE_SPACE_API}`
|
| 436 |
+
Should be: `https://ytbai-mof-hydrogen-storage-capacity-prediction-model.hf.space/predict`
|
| 437 |
+
""")
|
| 438 |
+
else:
|
| 439 |
+
data = resp.json()
|
| 440 |
+
if data.get("success"):
|
| 441 |
+
preds = data.get("predictions", [])
|
| 442 |
+
if isinstance(preds, list) and len(preds) > 0:
|
| 443 |
+
try:
|
| 444 |
+
df = pd.DataFrame(preds)
|
| 445 |
+
|
| 446 |
+
if 'sample_name' in df.columns:
|
| 447 |
+
df['sample_name'] = filename
|
| 448 |
+
else:
|
| 449 |
+
df.insert(0, 'sample_name', filename)
|
| 450 |
+
|
| 451 |
+
with glass_card("π", f"Prediction Results for {filename}", "Hydrogen storage capacity analysis"):
|
| 452 |
+
st.dataframe(df, use_container_width=True)
|
| 453 |
+
|
| 454 |
+
st.markdown("""
|
| 455 |
+
<div class="info-box">
|
| 456 |
+
<h4>π Property Definitions</h4>
|
| 457 |
+
<ul>
|
| 458 |
+
<li><strong>UG_at_PS</strong> β Usable Gravimetric capacity at 77 K (100 β 5 bar) in wt.%</li>
|
| 459 |
+
<li><strong>UV_at_PS</strong> β Usable Volumetric capacity at 77 K (100 β 5 bar) in g Hβ/L</li>
|
| 460 |
+
<li><strong>UG_at_TPS</strong> β Usable Gravimetric capacity (100 bar / 77 K β 5 bar / 160 K) in wt.%</li>
|
| 461 |
+
<li><strong>UV_at_TPS</strong> β Usable Volumetric capacity (100 bar / 77 K β 5 bar / 160 K) in g Hβ/L</li>
|
| 462 |
+
</ul>
|
| 463 |
+
</div>
|
| 464 |
+
""", unsafe_allow_html=True)
|
| 465 |
+
|
| 466 |
+
csv = df.to_csv(index=False)
|
| 467 |
+
st.download_button(
|
| 468 |
+
label="β¬οΈ Export Results to CSV",
|
| 469 |
+
data=csv,
|
| 470 |
+
file_name=f"mof_predictions_{filename}.csv",
|
| 471 |
+
mime="text/csv"
|
| 472 |
+
)
|
| 473 |
+
except Exception as e:
|
| 474 |
+
st.warning(f"Could not format as table: {e}")
|
| 475 |
+
st.json(preds)
|
| 476 |
+
else:
|
| 477 |
+
st.warning("β οΈ No predictions returned.")
|
| 478 |
+
st.json(data)
|
| 479 |
else:
|
| 480 |
+
st.error("β Private Space returned failure.")
|
| 481 |
st.json(data)
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
|
| 484 |
+
footer_html = """
|
| 485 |
+
<footer class="footer">
|
| 486 |
+
<div class="copyright">
|
| 487 |
+
<p>© 2025 MOF Hβ Storage Predictor. All rights reserved.</p>
|
| 488 |
+
</div>
|
| 489 |
+
<div class="contact">
|
| 490 |
+
<p>Contact: mofdb@outlook.com</p>
|
| 491 |
+
</div>
|
| 492 |
+
<div class="attribution">
|
| 493 |
+
<p><a href="https://www.flaticon.com/free-icons/structure" title="structure icons" target="_blank" rel="noopener noreferrer">Structure icons created by Vectorslab - Flaticon</a></p>
|
| 494 |
+
</div>
|
| 495 |
+
</footer>
|
| 496 |
+
"""
|
| 497 |
+
st.markdown(footer_html, unsafe_allow_html=True)
|