Spaces:
Sleeping
Sleeping
File size: 33,585 Bytes
62a4041 22d9362 62a4041 723ebe4 62a4041 723ebe4 182c9ce d0109c7 723ebe4 40a522b 723ebe4 4dcddf5 723ebe4 62a4041 723ebe4 62a4041 d0109c7 723ebe4 62a4041 40a522b 182c9ce d0109c7 40a522b 62a4041 723ebe4 40a522b 723ebe4 40a522b 62a4041 d7065c4 c8f5637 40a522b c8f5637 40a522b 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 40a522b 723ebe4 62a4041 723ebe4 62a4041 723ebe4 40a522b 62a4041 40a522b 723ebe4 62a4041 182c9ce 723ebe4 62a4041 723ebe4 182c9ce 723ebe4 182c9ce 723ebe4 182c9ce 723ebe4 22d9362 62a4041 723ebe4 320b946 40a522b d7065c4 40a522b d7065c4 40a522b 62a4041 40a522b d7065c4 40a522b d7065c4 62a4041 4dcddf5 62a4041 d0109c7 40a522b 22d9362 723ebe4 62a4041 b028c2c d0109c7 b028c2c 723ebe4 e2ae453 723ebe4 e2ae453 723ebe4 e2ae453 723ebe4 e2ae453 b028c2c e2ae453 b028c2c e2ae453 d0109c7 e2ae453 b028c2c e2ae453 b54a4ec e2ae453 723ebe4 62a4041 d7065c4 d0109c7 723ebe4 40a522b d0109c7 40a522b 723ebe4 c8f5637 d0109c7 723ebe4 d0109c7 40a522b 723ebe4 40a522b 723ebe4 62a4041 40a522b 723ebe4 40a522b 723ebe4 40a522b 723ebe4 40a522b 723ebe4 40a522b 723ebe4 40a522b 62a4041 40a522b d0109c7 40a522b d0109c7 40a522b d0109c7 40a522b 182c9ce 40a522b 62a4041 40a522b 723ebe4 182c9ce 723ebe4 182c9ce 62a4041 114376b 723ebe4 40a522b 723ebe4 62a4041 723ebe4 62a4041 723ebe4 22d9362 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 723ebe4 22d9362 62a4041 723ebe4 c8f5637 723ebe4 c8f5637 723ebe4 40a522b 62a4041 723ebe4 40a522b 723ebe4 62a4041 723ebe4 40a522b 62a4041 40a522b 182c9ce 723ebe4 62a4041 723ebe4 40a522b 723ebe4 62a4041 723ebe4 62a4041 723ebe4 62a4041 |
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
from models.resnet_cnn import ResNet1D
from models.figure2_cnn import Figure2CNN
import hashlib
import gc
import time
import io
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import torch
import torch.nn.functional as F
import streamlit as st
import os
import sys
from pathlib import Path
# Ensure 'utils' directory is in the Python path
utils_path = Path(__file__).resolve().parent / "utils"
if utils_path.is_dir() and str(utils_path) not in sys.path:
sys.path.append(str(utils_path))
matplotlib.use("Agg") # ensure headless rendering in Spaces
# Import local modules
from utils.preprocessing import resample_spectrum
KEEP_KEYS = {
# === global UI context we want to keep after "Reset" ===
"model_select", # sidebar model key
"input_mode", # radio for Upload|Sample
"uploader_version", # version counter for file uploader
"input_registry", # radio controlling Upload vs Sample
}
# Configuration
st.set_page_config(
page_title="ML Polymer Classification",
page_icon="π¬",
layout="wide",
initial_sidebar_state="expanded"
)
# Stabilize tab panel height on HF Spaces to prevent visible column jitter.
# This sets a minimum height for the content area under the tab headers.
st.markdown("""
<style>
/* Tabs content area: the sibling after the tablist */
div[data-testid="stTabs"] > div[role="tablist"] + div { min-height: 420px;}
</style>
""", unsafe_allow_html=True)
# Constants
TARGET_LEN = 500
SAMPLE_DATA_DIR = Path("sample_data")
# Prefer env var, else 'model_weights' if present; else canonical 'outputs'
MODEL_WEIGHTS_DIR = (
os.getenv("WEIGHTS_DIR")
or ("model_weights" if os.path.isdir("model_weights") else "outputs")
)
# Model configuration
MODEL_CONFIG = {
"Figure2CNN (Baseline)": {
"class": Figure2CNN,
"path": f"{MODEL_WEIGHTS_DIR}/figure2_model.pth",
"emoji": "π¬",
"description": "Baseline CNN with standard filters",
"accuracy": "94.80%",
"f1": "94.30%"
},
"ResNet1D (Advanced)": {
"class": ResNet1D,
"path": f"{MODEL_WEIGHTS_DIR}/resnet_model.pth",
"emoji": "π§ ",
"description": "Residual CNN with deeper feature learning",
"accuracy": "96.20%",
"f1": "95.90%"
}
}
# Label mapping
LABEL_MAP = {0: "Stable (Unweathered)", 1: "Weathered (Degraded)"}
# === UTILITY FUNCTIONS ===
def init_session_state():
defaults = {
"status_message": "Ready to analyze polymer spectra π¬",
"status_type": "info",
"input_text": None,
"filename": None,
"input_source": None, # "upload" or "sample"
"sample_select": "-- Select Sample --",
"input_mode": "Upload File", # controls which pane is visible
"inference_run_once": False,
"x_raw": None, "y_raw": None, "y_resampled": None,
"log_messages": [],
"uploader_version": 0,
"current_upload_key": "upload_txt_0",
}
for k, v in defaults.items():
st.session_state.setdefault(k, v)
for key, default_value in defaults.items():
if key not in st.session_state:
st.session_state[key] = default_value
def label_file(filename: str) -> int:
"""Extract label from filename based on naming convention"""
name = Path(filename).name.lower()
if name.startswith("sta"):
return 0
elif name.startswith("wea"):
return 1
else:
# Return None for unknown patterns instead of raising error
return -1 # Default value for unknown patterns
@st.cache_data
def load_state_dict(_mtime, model_path):
"""Load state dict with mtime in cache key to detect file changes"""
try:
return torch.load(model_path, map_location="cpu", weights_only=True)
except (FileNotFoundError, RuntimeError) as e:
st.warning(f"Error loading state dict: {e}")
return None
@st.cache_resource
def load_model(model_name):
"""Load and cache the specified model with error handling"""
try:
config = MODEL_CONFIG[model_name]
model_class = config["class"]
model_path = config["path"]
# Initialize model
model = model_class(input_length=TARGET_LEN)
# Check if model file exists
if not os.path.exists(model_path):
st.warning(f"β οΈ Model weights not found: {model_path}")
st.info("Using randomly initialized model for demonstration purposes.")
return model, False
# Get mtime for cache invalidation
mtime = os.path.getmtime(model_path)
# Load weights
state_dict = load_state_dict(mtime, model_path)
if state_dict:
model.load_state_dict(state_dict, strict=True)
if model is None:
raise ValueError(
"Model is not loaded. Please check the model configuration or weights.")
model.eval()
return model, True
else:
return model, False
except (FileNotFoundError, KeyError) as e:
st.error(f"β Error loading model {model_name}: {str(e)}")
return None, False
def cleanup_memory():
"""Clean up memory after inference"""
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
@st.cache_data
def get_sample_files():
"""Get list of sample files if available"""
sample_dir = Path(SAMPLE_DATA_DIR)
if sample_dir.exists():
return sorted(list(sample_dir.glob("*.txt")))
return []
def parse_spectrum_data(raw_text):
"""Parse spectrum data from text with robust error handling and validation"""
x_vals, y_vals = [], []
for line in raw_text.splitlines():
line = line.strip()
if not line or line.startswith('#'): # Skip empty lines and comments
continue
try:
# Handle different separators
parts = line.replace(",", " ").split()
numbers = [p for p in parts if p.replace('.', '', 1).replace(
'-', '', 1).replace('+', '', 1).isdigit()]
if len(numbers) >= 2:
x, y = float(numbers[0]), float(numbers[1])
x_vals.append(x)
y_vals.append(y)
except ValueError:
# Skip problematic lines but don't fail completely
continue
if len(x_vals) < 10: # Minimum reasonable spectrum length
raise ValueError(
f"Insufficient data points: {len(x_vals)}. Need at least 10 points.")
x = np.array(x_vals)
y = np.array(y_vals)
# Check for NaNs
if np.any(np.isnan(x)) or np.any(np.isnan(y)):
raise ValueError("Input data contains NaN values")
# Check monotonic increasing x
if not np.all(np.diff(x) > 0):
raise ValueError("Wavenumbers must be strictly increasing")
# Check reasonable range for Raman spectroscopy
if min(x) < 0 or max(x) > 10000 or (max(x) - min(x)) < 100:
raise ValueError(
f"Invalid wavenumber range: {min(x)} - {max(x)}. Expected ~400-4000 cmβ»ΒΉ with span >100")
return x, y
def create_spectrum_plot(x_raw, y_raw, x_resampled, y_resampled):
"""Create spectrum visualization plot"""
fig, ax = plt.subplots(1, 2, figsize=(13, 5), dpi=100)
# == Raw spectrum ==
ax[0].plot(x_raw, y_raw, label="Raw", color="dimgray", linewidth=1)
ax[0].set_title("Raw Input Spectrum")
ax[0].set_xlabel("Wavenumber (cmβ»ΒΉ)")
ax[0].set_ylabel("Intensity")
ax[0].grid(True, alpha=0.3)
ax[0].legend()
# == Resampled spectrum ==
ax[1].plot(x_resampled, y_resampled, label="Resampled", color="steelblue", linewidth=1)
ax[1].set_title(f"Resampled ({len(y_resampled)} points)")
ax[1].set_xlabel("Wavenumber (cmβ»ΒΉ)")
ax[1].set_ylabel("Intensity")
ax[1].grid(True, alpha=0.3)
ax[1].legend()
plt.tight_layout()
# == Convert to image ==
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
buf.seek(0)
plt.close(fig) # Prevent memory leaks
return Image.open(buf)
def render_confidence_bar(probabilities, class_labels):
bar = lambda p: "β" * int(p * 20)
for label, prob in zip(class_labels, probabilities):
st.write(f"**{label}**: {bar(prob)} {prob*100:.1f}%")
def get_confidence_description(logit_margin):
"""Get human-readable confidence description"""
if logit_margin > 1000:
return "VERY HIGH", "π’"
elif logit_margin > 250:
return "HIGH", "π‘"
elif logit_margin > 100:
return "MODERATE", "π "
else:
return "LOW", "π΄"
def log_message(msg: str):
"""Append a timestamped line to the in-app log, creating the buffer if needed."""
if "log_messages" not in st.session_state or st.session_state["log_messages"] is None:
st.session_state["log_messages"] = []
st.session_state["log_messages"].append(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}"
)
def trigger_run():
"""Set a flag so we can detect button press reliably across reruns"""
st.session_state['run_requested'] = True
def on_sample_change():
"""Read selected sample once and persist as text."""
sel = st.session_state.get("sample_select", "-- Select Sample --")
if sel == "-- Select Sample --":
return
try:
text = (Path(SAMPLE_DATA_DIR / sel).read_text(encoding="utf-8"))
st.session_state["input_text"] = text
st.session_state["filename"] = sel
st.session_state["input_source"] = "sample"
# π§ Clear previous results so right column resets immediately
reset_results("New sample selected")
st.session_state["status_message"] = f"π Sample '{sel}' ready for analysis"
st.session_state["status_type"] = "success"
except (FileNotFoundError, IOError) as e:
st.session_state["status_message"] = f"β Error loading sample: {e}"
st.session_state["status_type"] = "error"
def on_input_mode_change():
"""Reset sample when switching to Upload"""
if st.session_state["input_mode"] == "Upload File":
st.session_state["sample_select"] = "-- Select Sample --"
# π§ Reset when switching modes to prevent stale right-column visuals
reset_results("Switched input mode")
def on_model_change():
"""Force the right column back to init state when the model changes"""
reset_results("Model changed")
def reset_results(reason: str = ""):
"""Clear previous inference artifacts so the right column returns to initial state."""
st.session_state["inference_run_once"] = False
st.session_state["x_raw"] = None
st.session_state["y_raw"] = None
st.session_state["y_resampled"] = None
# ||== Clear logs between runs ==||
st.session_state["log_messages"] = []
# ||== Always reset the status box ==||
st.session_state["status_message"] = (
f"βΉοΈ {reason}"
if reason else "Ready to analyze polymer spectra π¬"
)
st.session_state["status_type"] = "info"
def reset_ephemeral_state():
# === remove everything except KEPT global UI context ===
for k in list(st.session_state.keys()):
if k not in KEEP_KEYS:
st.session_state.pop(k, None)
# == bump the uploader version β new widget instance with empty value ==
st.session_state["uploader_version"] += 1
st.session_state["current_upload_key"] = f"upload_txt_{st.session_state['uploader_version']}"
# == reseed other emphemeral state ==
st.session_state["input_text"] = None
st.session_state["filename"] = None
st.session_state["input_source"] = None
st.session_state["sample_select"] = "-- Select Sample --"
# == return the UI to a clean state ==
st.session_state["inference_run_once"] = False
st.session_state["x_raw"] = None
st.session_state["y_raw"] = None
st.session_state["y_resampled"] = None
st.session_state["log_messages"] = []
st.session_state["status_message"] = "Ready to analyze polymer spectra π¬"
st.session_state["status_type"] = "info"
st.rerun()
def plot_confidence_bar(probabilities: list[float], class_labels: list[str]) -> None:
"""Renders a horizontal bar chart of prediction confidences per class."""
fig, ax = plt.subplots(figsize=(4, 1.5))
bars = ax.barh(class_labels, probabilities, color=[
"green" if i == np.argmax(probabilities) else "gray"
for i in range(len(probabilities))
])
ax.set_xlabel("Confidence")
ax.set_title("Prediction Confidence")
ax.xaxis.set_ticks([0, 0.5, 1.0])
ax.set_xlim(0, 1.0)
for i, (label, prob) in enumerate(zip(class_labels, probabilities)):
ax.text(prob + 0.01, i, f"{prob*100:.1f}%", va='center', fontsize=8)
st.pyplot(fig)
# Main app
def main():
init_session_state()
# Header
st.title("π¬ AI-Driven Polymer Classification")
st.markdown(
"**Predict polymer degradation states using Raman spectroscopy and deep learning**")
st.info(
"**Prototype Notice:** v0.1 Raman-only. "
"Multi-model CNN evaluation in progress. "
"FTIR support planned.",
icon="β‘"
)
# Sidebar
with st.sidebar:
st.header("βΉοΈ About This App")
st.sidebar.markdown("""
AI-Driven Polymer Aging Prediction and Classification
π― **Purpose**: Classify polymer degradation using AI
π **Input**: Raman spectroscopy `.txt` files
π§ **Models**: CNN architectures for binary classification
πΎ **Current**: Figure2CNN (baseline)
π **Next**: More trained CNNs in evaluation pipeline
---
**Team**
Dr. Sanmukh Kuppannagari (Mentor)
Dr. Metin Karailyan (Mentor)
π¨βπ» Jaser Hasan (Author)
---
**Links**
π [Live HF Space](https://huggingface.co/spaces/dev-jas/polymer-aging-ml)
π [GitHub Repository](https://github.com/KLab-AI3/ml-polymer-recycling)
---
**Model Credit**
Baseline model inspired by *Figure 2 CNN* from:
> Neo, E.R.K., Low, J.S.C., Goodship, V., Debattista, K. (2023).
> *Deep learning for chemometric analysis of plastic spectral data from infrared and Raman databases*.
> _Resources, Conservation & Recycling_, **188**, 106718.
[https://doi.org/10.1016/j.resconrec.2022.106718](https://doi.org/10.1016/j.resconrec.2022.106718)
""")
st.markdown("---")
# Model selection
st.subheader("π§ Model Selection")
model_labels = [
f"{MODEL_CONFIG[name]['emoji']} {name}" for name in MODEL_CONFIG.keys()]
selected_label = st.selectbox("Choose AI model:", model_labels,
key="model_select", on_change=on_model_change)
model_choice = selected_label.split(" ", 1)[1]
# Model info
config = MODEL_CONFIG[model_choice]
st.markdown(f"""
**π {config['emoji']} Model Details**
*{config['description']}*
- **Accuracy**: `{config['accuracy']}`
- **F1 Score**: `{config['f1']}`
""")
# Main content area
col1, col2 = st.columns([1, 1.5], gap="large")
with col1:
st.subheader("π Data Input")
mode = st.radio(
"Input mode",
["Upload File", "Sample Data"],
key="input_mode",
horizontal=True,
on_change=on_input_mode_change
)
# ---- Upload tab ----
if mode == "Upload File":
upload_key = st.session_state["current_upload_key"]
up = st.file_uploader(
"Upload Raman spectrum (.txt)",
type="txt",
help="Upload a text file with wavenumber and intensity columns",
key=upload_key, # β versioned key
)
# == process change immediately (no on_change; simpler & reliable) ==
if up is not None:
raw = up.read()
text = raw.decode("utf-8") if isinstance(raw, bytes) else raw
# == only reparse if its a different file|source ==
if st.session_state.get("filename") != getattr(up, "name", None) or st.session_state.get("input_source") != "upload":
st.session_state["input_text"] = text
st.session_state["filename"] = getattr(up, "name", "uploaded.txt")
st.session_state["input_source"] = "upload"
# == clear right column immediately ==
reset_results("New file selected")
st.session_state["status_message"] = f"π File '{st.session_state['filename']}' ready for analysis"
st.session_state["status_type"] = "success"
if up:
st.success(f"β
Loaded: {up.name}")
# ---- Sample tab ----
else:
sample_files = get_sample_files()
if sample_files:
options = ["-- Select Sample --"] + \
[p.name for p in sample_files]
sel = st.selectbox(
"Choose sample spectrum:",
options,
key="sample_select",
on_change=on_sample_change, # <-- critical
)
if sel != "-- Select Sample --":
st.success(f"β
Loaded sample: {sel}")
else:
st.info("No sample data available")
# ---- Status box ----
st.subheader("π¦ Status")
msg = st.session_state.get("status_message", "Ready")
typ = st.session_state.get("status_type", "info")
if typ == "success":
st.success(msg)
elif typ == "error":
st.error(msg)
else:
st.info(msg)
# ---- Model load ----
model, model_loaded = load_model(model_choice)
if not model_loaded:
st.warning("β οΈ Model weights not available - using demo mode")
# Ready to run if we have text and a model
inference_ready = bool(st.session_state.get(
"input_text")) and (model is not None)
# === Run Analysis (form submit batches state) ===
with st.form("analysis_form", clear_on_submit=False):
submitted = st.form_submit_button(
"βΆοΈ Run Analysis",
type="primary",
disabled=not inference_ready,
)
if st.button("Reset", help="Clear current file(s), plots, and results"):
reset_ephemeral_state()
if submitted and inference_ready:
# parse β preprocess β predict β render
# Handles the submission of the analysis form and performs spectrum data processing
try:
raw_text = st.session_state["input_text"]
filename = st.session_state.get("filename") or "unknown.txt"
# Parse
with st.spinner("Parsing spectrum data..."):
x_raw, y_raw = parse_spectrum_data(raw_text)
# Resample
with st.spinner("Resampling spectrum..."):
# ===Resample Unpack===
r1, r2 = resample_spectrum(x_raw, y_raw, TARGET_LEN)
def _is_strictly_increasing(a):
try:
a = np.asarray(a)
return a.ndim == 1 and a.size >= 2 and np.all(np.diff(a) > 0)
except Exception:
return False
if _is_strictly_increasing(r1) and not _is_strictly_increasing(r2):
x_resampled, y_resampled = np.asarray(r1), np.asarray(r2)
elif _is_strictly_increasing(r2) and not _is_strictly_increasing(r1):
x_resampled, y_resampled = np.asarray(r2), np.asarray(r1)
else:
# == Ambigous; assume (x, y) and log
x_resampled, y_resampled = np.asarray(r1), np.asarray(r2)
log_message("Resample outputs ambigous; assumed (x, y).")
# ===Persists for plotting + inference===
st.session_state["x_raw"] = x_raw
st.session_state["y_raw"] = y_raw
st.session_state["x_resampled"] = x_resampled # β-- NEW
st.session_state["y_resampled"] = y_resampled
# Persist results (drives right column)
st.session_state["x_raw"] = x_raw
st.session_state["y_raw"] = y_raw
st.session_state["y_resampled"] = y_resampled
st.session_state["inference_run_once"] = True
st.session_state["status_message"] = f"π Analysis completed for: {filename}"
st.session_state["status_type"] = "success"
st.rerun()
except (ValueError, TypeError) as e:
st.error(f"β Analysis failed: {e}")
st.session_state["status_message"] = f"β Error: {e}"
st.session_state["status_type"] = "error"
# Results column
with col2:
if st.session_state.get("inference_run_once", False):
st.subheader("π Analysis Results")
# Get data from session state
x_raw = st.session_state.get('x_raw')
y_raw = st.session_state.get('y_raw')
x_resampled = st.session_state.get('x_resampled') # β NEW
y_resampled = st.session_state.get('y_resampled')
filename = st.session_state.get('filename', 'Unknown')
if all(v is not None for v in [x_raw, y_raw, y_resampled]):
# Create and display plot
try:
spectrum_plot = create_spectrum_plot(x_raw, y_raw, x_resampled, y_resampled)
st.image(
spectrum_plot, caption="Spectrum Preprocessing Results", use_container_width=True)
except (ValueError, RuntimeError, TypeError) as e:
st.warning(f"Could not generate plot: {e}")
log_message(f"Plot generation error: {e}")
# Run inference
try:
with st.spinner("Running AI inference..."):
start_time = time.time()
# Prepare input tensor
input_tensor = torch.tensor(
y_resampled, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
# Run inference
model.eval()
with torch.no_grad():
if model is None:
raise ValueError(
"Model is not loaded. Please check the model configuration or weights.")
logits = model(input_tensor)
prediction = torch.argmax(logits, dim=1).item()
logits_list = logits.detach().numpy().tolist()[0]
probs = F.softmax(logits.detach(), dim=1).cpu().numpy().flatten()
inference_time = time.time() - start_time
log_message(
f"Inference completed in {inference_time:.2f}s, prediction: {prediction}")
# Clean up memory
cleanup_memory()
# Get ground truth if available
true_label_idx = label_file(filename)
true_label_str = LABEL_MAP.get(
true_label_idx, "Unknown") if true_label_idx is not None else "Unknown"
# Get prediction
predicted_class = LABEL_MAP.get(
int(prediction), f"Class {int(prediction)}")
# Calculate confidence metrics
logit_margin = abs(
logits_list[0] - logits_list[1]) if len(logits_list) >= 2 else 0
confidence_desc, confidence_emoji = get_confidence_description(
logit_margin)
# Display results
st.markdown("### π― Prediction Results")
# Main prediction
st.markdown(f"""
**π¬ Sample**: `{filename}`
**π§ Model**: `{model_choice}`
**β±οΈ Processing Time**: `{inference_time:.2f}s`
""")
# Prediction box
if predicted_class == "Stable (Unweathered)":
st.success(f"π’ **Prediction**: {predicted_class}")
else:
st.warning(f"π‘ **Prediction**: {predicted_class}")
# Confidence
st.markdown(
f"**{confidence_emoji} Confidence**: {confidence_desc} (margin: {logit_margin:.1f})")
# Ground truth comparison
if true_label_idx is not None:
if predicted_class == true_label_str:
st.success(
f"β
**Ground Truth**: {true_label_str} - **Correct!**")
else:
st.error(
f"β **Ground Truth**: {true_label_str} - **Incorrect**")
else:
st.info(
"βΉοΈ **Ground Truth**: Unknown (filename doesn't follow naming convention)")
# ===display confidence results===
class_labels = ["Stable", "Weathered"]
plot_confidence_bar(probabilities=probs.tolist(), class_labels=class_labels)
# ===Detailed results tabs===
tab1, tab2, tab3 = st.tabs(
["π Details", "π¬ Technical", "π Explanation"])
with tab1:
st.markdown("**Model Output (Logits)**")
for i, score in enumerate(logits_list):
label = LABEL_MAP.get(i, f"Class {i}")
st.metric(label, f"{score:.2f}")
st.markdown("**Spectrum Statistics**")
st.json({
"Original Length": len(x_raw) if x_raw is not None else 0,
"Resampled Length": TARGET_LEN,
"Wavenumber Range": f"{min(x_raw):.1f} - {max(x_raw):.1f} cmβ»ΒΉ" if x_raw is not None else "N/A",
"Intensity Range": f"{min(y_raw):.1f} - {max(y_raw):.1f}" if y_raw is not None else "N/A",
"Model Confidence": confidence_desc
})
with tab2:
st.markdown("**Technical Information**")
model_path = MODEL_CONFIG[model_choice]["path"]
mtime = os.path.getmtime(model_path) if os.path.exists(
model_path) else "N/A"
file_hash = hashlib.md5(open(model_path, 'rb').read(
)).hexdigest() if os.path.exists(model_path) else "N/A"
st.json({
"Model Architecture": model_choice,
"Model Path": model_path,
"Weights Last Modified": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime)) if mtime != "N/A" else "N/A",
"Weights Hash": file_hash,
"Input Shape": list(input_tensor.shape),
"Output Shape": list(logits.shape),
"Inference Time": f"{inference_time:.3f}s",
"Device": "CPU",
"Model Loaded": model_loaded
})
if not model_loaded:
st.warning(
"β οΈ Demo mode: Using randomly initialized weights")
# Debug log
st.markdown("**Debug Log**")
st.text_area("Logs", "\n".join(
st.session_state.get("log_messages", [])), height=200)
try:
resampler_mod = getattr(resample_spectrum, "__module__", "unknown")
resampler_doc = getattr(resample_spectrum, "__doc__", None)
resampler_doc = resampler_doc.splitlines()[0] if isinstance(resampler_doc, str) and resampler_doc else "no doc"
y_rs = st.session_state.get("y_resampled", None)
diag = {}
if y_rs is not None:
arr = np.asarray(y_rs)
diag = {
"y_resampled_len": int(arr.size),
"y_resampled_min": float(np.min(arr)) if arr.size else None,
"y_resampled_max": float(np.max(arr)) if arr.size else None,
"y_resampled_ptp": float(np.ptp(arr)) if arr.size else None,
"y_resampled_unique": int(np.unique(arr).size) if arr.size else None,
"y_resampled_all_equal": bool(np.ptp(arr) == 0.0) if arr.size else None,
}
st.markdown("**Resampler Info")
st.json({
"module": resampler_mod,
"doc": resampler_doc,
**({"y_resampled_stats": diag} if diag else {})
})
except Exception as _e:
st.warning(f"Diagnostics skipped: {_e}")
with tab3:
st.markdown("""
**π Analysis Process**
1. **Data Upload**: Raman spectrum file loaded
2. **Preprocessing**: Data parsed and resampled to 500 points
3. **AI Inference**: CNN model analyzes spectral patterns
4. **Classification**: Binary prediction with confidence scores
**π§ Model Interpretation**
The AI model identifies spectral features indicative of:
- **Stable polymers**: Well-preserved molecular structure
- **Weathered polymers**: Degraded/oxidized molecular bonds
**π― Applications**
- Material longevity assessment
- Recycling viability evaluation
- Quality control in manufacturing
- Environmental impact studies
""")
except (ValueError, RuntimeError) as e:
st.error(f"β Inference failed: {str(e)}")
log_message(f"Inference error: {str(e)}")
else:
st.error(
"β Missing spectrum data. Please upload a file and run analysis.")
else:
# Welcome message
st.markdown("""
### π Welcome to AI Polymer Classification
**Get started by:**
1. π§ Select an AI model in the sidebar
2. π Upload a Raman spectrum file or choose a sample
3. βΆοΈ Click "Run Analysis" to get predictions
**Supported formats:**
- Text files (.txt) with wavenumber and intensity columns
- Space or comma-separated values
- Any length (automatically resampled to 500 points)
**Example applications:**
- π¬ Research on polymer degradation
- β»οΈ Recycling feasibility assessment
- π± Sustainability impact studies
- π Quality control in manufacturing
""")
# Run the application
main()
|