devjas1 commited on
Commit
dd49e6b
·
1 Parent(s): 614423c

(REFACTOR:core): <pdularize monolithic app script

Browse files

This commit introduces a major architectural refactor, breaking down the monolithic 'app.py' script into a more maintainable and organized structure. The core logic, UI components, state management, and configuration are now separate into distinct modules.

This new structure improves readability and separation of concerns, laying the groundwork for future development.

NEW COMPONENTS:
'config.py': centralized location for all application constants and model configurations
- 'core_logic.py': new home for core data processing, model loading, and ML inference functions.
- 'modules/callbacks.py': contains all Streamlit widget callbacks and state management functions
-'modules/ui_components.py': contains all high-level UI rendering functions
-'static/style.css': externalizes all custom CSS for better mgmt
-'CODEBASE_INVENTORY.md': adds the software audit and better refactor plan

CODEBASE_INVENTORY.md CHANGED
@@ -480,6 +480,56 @@ The platform successfully bridges academic research and practical application, p
480
 
481
  <div style="text-align: center">⁂</div>
482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  [^1_1]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml/tree/main
484
  [^1_2]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml/tree/main/datasets
485
  [^1_3]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml
 
480
 
481
  <div style="text-align: center">⁂</div>
482
 
483
+ ### EXTRA
484
+
485
+ ```text
486
+ 1. Setup & Configuration (Lines 1-105)
487
+ Imports: Standard libraries (os, sys, time), data science (numpy, torch, matplotlib), and Streamlit.
488
+ Local Imports: Pulls from your existing utils and models directories.
489
+ Constants: Global, hardcoded configuration variables.
490
+ KEEP_KEYS: Defines which session state keys persist on reset.
491
+ TARGET_LEN: A static preprocessing value.
492
+ SAMPLE_DATA_DIR, MODEL_WEIGHTS_DIR: Path configurations.
493
+ MODEL_CONFIG: A dictionary defining model paths, classes, and metadata.
494
+ LABEL_MAP: A dictionary for mapping class indices to human-readable names.
495
+ Page Setup:
496
+ st.set_page_config(): Sets the browser tab title, icon, and layout.
497
+ st.markdown(<style>...): A large, embedded multi-line string containing all the custom CSS for the application.
498
+ 2. Core Logic & Data Processing (Lines 108-250)
499
+ Model Handling:
500
+ load_state_dict(): Cached function to load model weights from a file.
501
+ load_model(): Cached resource to initialize a model class and load its weights.
502
+ run_inference(): The main ML prediction function. It takes resampled data, loads the appropriate model, runs inference, and returns the results.
503
+ Data I/O & Preprocessing:
504
+ label_file(): Extracts the ground truth label from a filename.
505
+ get_sample_files(): Lists the available .txt files in the sample data directory.
506
+ parse_spectrum_data(): The crucial function for reading, validating, and parsing raw text input into numerical numpy arrays.
507
+ Visualization:
508
+ create_spectrum_plot(): Generates the "Raw vs. Resampled" matplotlib plot and returns it as an image.
509
+ Helpers:
510
+ cleanup_memory(): A utility for garbage collection.
511
+ get_confidence_description(): Maps a logit margin to a human-readable confidence level.
512
+ 3. State Management & Callbacks (Lines 253-335)
513
+ Initialization:
514
+ init_session_state(): The cornerstone of the app's state, defining all the default values in st.session_state.
515
+ Widget Callbacks:
516
+ on_sample_change(): Triggered when the user selects a sample file.
517
+ on_input_mode_change(): Triggered by the main st.radio widget.
518
+ on_model_change(): Triggered when the user selects a new model.
519
+ Reset/Clear Functions:
520
+ reset_results(): A soft reset that only clears inference artifacts.
521
+ reset_ephemeral_state(): The "master reset" that clears almost all session state and forces a file uploader refresh.
522
+ clear_batch_results(): A focused function to clear only the results in col2.
523
+ 4. UI Rendering Components (Lines 338-End)
524
+ Generic Components:
525
+ render_kv_grid(): A reusable helper to display a dictionary in a neat grid.
526
+ render_model_meta(): Renders the model's accuracy and F1 score in the sidebar.
527
+ Main Application Layout (main()):
528
+ Sidebar: Contains the header, model selector (st.selectbox), model metadata, and the "About" expander.
529
+ Column 1 (Input): Contains the main st.radio for mode selection and the conditional logic to display the single file uploader, batch uploader, or sample selector. It also holds the "Run Analysis" and "Reset All" buttons.
530
+ Column 2 (Results): Contains all the logic for displaying either the batch results or the detailed, tabbed results for a single file (Details, Technical, Explanation).
531
+ ```
532
+
533
  [^1_1]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml/tree/main
534
  [^1_2]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml/tree/main/datasets
535
  [^1_3]: https://huggingface.co/spaces/dev-jas/polymer-aging-ml
config.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import os
3
+ from models.figure2_cnn import Figure2CNN
4
+ from models.resnet_cnn import ResNet1D
5
+
6
+ KEEP_KEYS = {
7
+ # ==global UI context we want to keep after "Reset"==
8
+ "model_select", # sidebar model key
9
+ "input_mode", # radio for Upload|Sample
10
+ "uploader_version", # version counter for file uploader
11
+ "input_registry", # radio controlling Upload vs Sample
12
+ }
13
+
14
+ TARGET_LEN = 500
15
+ SAMPLE_DATA_DIR = Path("sample_data")
16
+
17
+ MODEL_WEIGHTS_DIR = (
18
+ os.getenv("WEIGHTS_DIR")
19
+ or ("model_weights" if os.path.isdir("model_weights") else "outputs")
20
+ )
21
+
22
+ # Model configuration
23
+ MODEL_CONFIG = {
24
+ "Figure2CNN (Baseline)": {
25
+ "class": Figure2CNN,
26
+ "path": f"{MODEL_WEIGHTS_DIR}/figure2_model.pth",
27
+ "emoji": "",
28
+ "description": "Baseline CNN with standard filters",
29
+ "accuracy": "94.80%",
30
+ "f1": "94.30%"
31
+ },
32
+ "ResNet1D (Advanced)": {
33
+ "class": ResNet1D,
34
+ "path": f"{MODEL_WEIGHTS_DIR}/resnet_model.pth",
35
+ "emoji": "",
36
+ "description": "Residual CNN with deeper feature learning",
37
+ "accuracy": "96.20%",
38
+ "f1": "95.90%"
39
+ }
40
+ }
41
+
42
+ # ==Label mapping==
43
+ LABEL_MAP = {0: "Stable (Unweathered)", 1: "Weathered (Degraded)"}
core_logic.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # --- New Imports ---
4
+ from config import MODEL_CONFIG, TARGET_LEN
5
+ import time
6
+ import gc
7
+ import torch
8
+ import torch.nn.functional as F
9
+ import numpy as np
10
+ import streamlit as st
11
+ from pathlib import Path
12
+ from config import SAMPLE_DATA_DIR
13
+
14
+
15
+ def label_file(filename: str) -> int:
16
+ """Extract label from filename based on naming convention"""
17
+ name = Path(filename).name.lower()
18
+ if name.startswith("sta"):
19
+ return 0
20
+ elif name.startswith("wea"):
21
+ return 1
22
+ else:
23
+ # Return None for unknown patterns instead of raising error
24
+ return -1 # Default value for unknown patterns
25
+
26
+
27
+ @st.cache_data
28
+ def load_state_dict(_mtime, model_path):
29
+ """Load state dict with mtime in cache key to detect file changes"""
30
+ try:
31
+ return torch.load(model_path, map_location="cpu")
32
+ except (FileNotFoundError, RuntimeError) as e:
33
+ st.warning(f"Error loading state dict: {e}")
34
+ return None
35
+
36
+
37
+ @st.cache_resource
38
+ def load_model(model_name):
39
+ """Load and cache the specified model with error handling"""
40
+ try:
41
+ config = MODEL_CONFIG[model_name]
42
+ model_class = config["class"]
43
+ model_path = config["path"]
44
+
45
+ # Initialize model
46
+ model = model_class(input_length=TARGET_LEN)
47
+
48
+ # Check if model file exists
49
+ if not os.path.exists(model_path):
50
+ st.warning(f"⚠️ Model weights not found: {model_path}")
51
+ st.info("Using randomly initialized model for demonstration purposes.")
52
+ return model, False
53
+
54
+ # Get mtime for cache invalidation
55
+ mtime = os.path.getmtime(model_path)
56
+
57
+ # Load weights
58
+ state_dict = load_state_dict(mtime, model_path)
59
+ if state_dict:
60
+ model.load_state_dict(state_dict, strict=True)
61
+ if model is None:
62
+ raise ValueError(
63
+ "Model is not loaded. Please check the model configuration or weights."
64
+ )
65
+ if model is None:
66
+ raise ValueError(
67
+ "Model is not loaded. Please check the model configuration or weights."
68
+ )
69
+ if model is None:
70
+ raise ValueError(
71
+ "Model is not loaded. Please check the model configuration or weights."
72
+ )
73
+ model.eval()
74
+ return model, True
75
+ else:
76
+ return model, False
77
+
78
+ except (FileNotFoundError, KeyError, RuntimeError) as e:
79
+ st.error(f"❌ Error loading model {model_name}: {str(e)}")
80
+ return None, False
81
+
82
+
83
+ def cleanup_memory():
84
+ """Clean up memory after inference"""
85
+ gc.collect()
86
+ if torch.cuda.is_available():
87
+ torch.cuda.empty_cache()
88
+
89
+
90
+ @st.cache_data
91
+ def run_inference(y_resampled, model_choice, _cache_key=None):
92
+ """Run model inference and cache results"""
93
+ model, model_loaded = load_model(model_choice)
94
+ if not model_loaded:
95
+ return None, None, None, None, None
96
+
97
+ input_tensor = (
98
+ torch.tensor(y_resampled, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
99
+ )
100
+ start_time = time.time()
101
+ model.eval()
102
+ with torch.no_grad():
103
+ if model is None:
104
+ raise ValueError(
105
+ "Model is not loaded. Please check the model configuration or weights."
106
+ )
107
+ logits = model(input_tensor)
108
+ prediction = torch.argmax(logits, dim=1).item()
109
+ logits_list = logits.detach().numpy().tolist()[0]
110
+ probs = F.softmax(logits.detach(), dim=1).cpu().numpy().flatten()
111
+ inference_time = time.time() - start_time
112
+ cleanup_memory()
113
+ return prediction, logits_list, probs, inference_time, logits
114
+
115
+
116
+ @st.cache_data
117
+ def get_sample_files():
118
+ """Get list of sample files if available"""
119
+ sample_dir = Path(SAMPLE_DATA_DIR)
120
+ if sample_dir.exists():
121
+ return sorted(list(sample_dir.glob("*.txt")))
122
+ return []
123
+
124
+
125
+ def parse_spectrum_data(raw_text):
126
+ """Parse spectrum data from text with robust error handling and validation"""
127
+ x_vals, y_vals = [], []
128
+
129
+ for line in raw_text.splitlines():
130
+ line = line.strip()
131
+ if not line or line.startswith("#"): # Skip empty lines and comments
132
+ continue
133
+
134
+ try:
135
+ # Handle different separators
136
+ parts = line.replace(",", " ").split()
137
+ numbers = [
138
+ p
139
+ for p in parts
140
+ if p.replace(".", "", 1)
141
+ .replace("-", "", 1)
142
+ .replace("+", "", 1)
143
+ .isdigit()
144
+ ]
145
+
146
+ if len(numbers) >= 2:
147
+ x, y = float(numbers[0]), float(numbers[1])
148
+ x_vals.append(x)
149
+ y_vals.append(y)
150
+
151
+ except ValueError:
152
+ # Skip problematic lines but don't fail completely
153
+ continue
154
+
155
+ if len(x_vals) < 10: # Minimum reasonable spectrum length
156
+ raise ValueError(
157
+ f"Insufficient data points: {len(x_vals)}. Need at least 10 points."
158
+ )
159
+
160
+ x = np.array(x_vals)
161
+ y = np.array(y_vals)
162
+
163
+ # Check for NaNs
164
+ if np.any(np.isnan(x)) or np.any(np.isnan(y)):
165
+ raise ValueError("Input data contains NaN values")
166
+
167
+ # Check monotonic increasing x
168
+ if not np.all(np.diff(x) > 0):
169
+ raise ValueError("Wavenumbers must be strictly increasing")
170
+
171
+ # Check reasonable range for Raman spectroscopy
172
+ if min(x) < 0 or max(x) > 10000 or (max(x) - min(x)) < 100:
173
+ raise ValueError(
174
+ f"Invalid wavenumber range: {min(x)} - {max(x)}. Expected ~400-4000 cm⁻¹ with span >100"
175
+ )
176
+
177
+ return x, y
modules/__init__.py ADDED
File without changes
modules/callbacks.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pathlib import Path
3
+ from utils.results_manager import ResultsManager
4
+ from utils.errors import ErrorHandler
5
+ from config import SAMPLE_DATA_DIR
6
+
7
+
8
+ def init_session_state():
9
+ """Keep a persistent session state"""
10
+ defaults = {
11
+ "status_message": "Ready to analyze polymer spectra 🔬",
12
+ "status_type": "info",
13
+ "input_text": None,
14
+ "filename": None,
15
+ "input_source": None, # "upload", "batch" or "sample"
16
+ "sample_select": "-- Select Sample --",
17
+ "input_mode": "Upload File", # controls which pane is visible
18
+ "inference_run_once": False,
19
+ "x_raw": None,
20
+ "y_raw": None,
21
+ "y_resampled": None,
22
+ "log_messages": [],
23
+ "uploader_version": 0,
24
+ "current_upload_key": "upload_txt_0",
25
+ "active_tab": "Details",
26
+ "batch_mode": False,
27
+ }
28
+
29
+ if "uploader_key" not in st.session_state:
30
+ st.session_state.uploader_key = 0
31
+
32
+ for k, v in defaults.items():
33
+ st.session_state.setdefault(k, v)
34
+
35
+ for key, default_value in defaults.items():
36
+ if key not in st.session_state:
37
+ st.session_state[key] = default_value
38
+
39
+ # ==Initialize results table==
40
+ ResultsManager.init_results_table()
41
+
42
+
43
+ def log_message(msg: str):
44
+ """Append a timestamped line to the in-app log, creating the buffer if needed."""
45
+ ErrorHandler.log_info(msg)
46
+
47
+
48
+ def on_sample_change():
49
+ """Read selected sample once and persist as text."""
50
+ sel = st.session_state.get("sample_select", "-- Select Sample --")
51
+ if sel == "-- Select Sample --":
52
+ return
53
+ try:
54
+ text = Path(SAMPLE_DATA_DIR / sel).read_text(encoding="utf-8")
55
+ st.session_state["input_text"] = text
56
+ st.session_state["filename"] = sel
57
+ st.session_state["input_source"] = "sample"
58
+ # 🔧 Clear previous results so right column resets immediately
59
+ reset_results("New sample selected")
60
+ st.session_state["status_message"] = f"📁 Sample '{sel}' ready for analysis"
61
+ st.session_state["status_type"] = "success"
62
+ except (FileNotFoundError, IOError) as e:
63
+ st.session_state["status_message"] = f"❌ Error loading sample: {e}"
64
+ st.session_state["status_type"] = "error"
65
+
66
+
67
+ def on_input_mode_change():
68
+ """Reset sample when switching to Upload"""
69
+ if st.session_state["input_mode"] == "Upload File":
70
+ st.session_state["sample_select"] = "-- Select Sample --"
71
+ st.session_state["batch_mode"] = False # Reset batch mode
72
+ elif st.session_state["input_mode"] == "Sample Data":
73
+ st.session_state["batch_mode"] = False # Reset batch mode
74
+ # 🔧 Reset when switching modes to prevent stale right-column visuals
75
+ reset_results("Switched input mode")
76
+
77
+
78
+ def reset_ephemeral_state():
79
+ """Comprehensive reset for the entire app state."""
80
+ # Define keys that should NOT be cleared by a full reset
81
+ keep_keys = {"model_select", "input_mode"}
82
+
83
+ for k in list(st.session_state.keys()):
84
+ if k not in keep_keys:
85
+ st.session_state.pop(k, None)
86
+
87
+ # Re-initialize the core state after clearing
88
+ init_session_state()
89
+
90
+ # CRITICAL: Bump the uploader version to force a widget reset
91
+ st.session_state["uploader_version"] += 1
92
+ st.session_state["current_upload_key"] = (
93
+ f"upload_txt_{st.session_state['uploader_version']}"
94
+ )
95
+
96
+
97
+ def on_model_change():
98
+ """Force the right column back to init state when the model changes"""
99
+ reset_results("Model changed")
100
+
101
+
102
+ def reset_results(reason: str = ""):
103
+ """Clear previous inference artifacts so the right column returns to initial state."""
104
+ st.session_state["inference_run_once"] = False
105
+ st.session_state["x_raw"] = None
106
+ st.session_state["y_raw"] = None
107
+ st.session_state["y_resampled"] = None
108
+ # ||== Clear batch results when resetting ==||
109
+ if "batch_results" in st.session_state:
110
+ del st.session_state["batch_results"]
111
+ # ||== Clear logs between runs ==||
112
+ st.session_state["log_messages"] = []
113
+ # ||== Always reset the status box ==||
114
+ st.session_state["status_message"] = (
115
+ f"ℹ️ {reason}" if reason else "Ready to analyze polymer spectra 🔬"
116
+ )
117
+ st.session_state["status_type"] = "info"
118
+
119
+
120
+ def clear_batch_results():
121
+ """Callback to clear only the batch results and the results log table."""
122
+ if "batch_results" in st.session_state:
123
+ del st.session_state["batch_results"]
124
+ # Also clear the persistent table from the ResultsManager utility
125
+ ResultsManager.clear_results()
126
+
127
+
128
+ # --- END: BUG 2 FIX (Callback Function) ---
129
+
130
+
131
+ def reset_all():
132
+ # Increment the key to force the file uploader to re-render
133
+ st.session_state.uploader_key += 1
134
+
135
+
136
+ def trigger_run():
137
+ """Set a flag so we can detect button press reliably across reruns"""
138
+ st.session_state["run_requested"] = True
modules/ui_components.py ADDED
@@ -0,0 +1,934 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import streamlit as st
4
+ import hashlib
5
+ import io
6
+ from PIL import Image
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+ from typing import Union
10
+ import time
11
+ from config import MODEL_CONFIG, TARGET_LEN, LABEL_MAP
12
+ from modules.callbacks import (
13
+ on_model_change,
14
+ on_input_mode_change,
15
+ on_sample_change,
16
+ reset_ephemeral_state,
17
+ log_message,
18
+ clear_batch_results,
19
+ )
20
+ from core_logic import (
21
+ get_sample_files,
22
+ load_model,
23
+ run_inference,
24
+ parse_spectrum_data,
25
+ label_file,
26
+ )
27
+ from modules.callbacks import reset_results
28
+ from utils.results_manager import ResultsManager
29
+ from utils.confidence import calculate_softmax_confidence
30
+ from utils.multifile import process_multiple_files, display_batch_results
31
+ from utils.preprocessing import resample_spectrum
32
+
33
+
34
+ def load_css(file_path):
35
+ with open(file_path, encoding="utf-8") as f:
36
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
37
+
38
+
39
+ @st.cache_data
40
+ def create_spectrum_plot(x_raw, y_raw, x_resampled, y_resampled, _cache_key=None):
41
+ """Create spectrum visualization plot"""
42
+ fig, ax = plt.subplots(1, 2, figsize=(13, 5), dpi=100)
43
+
44
+ # == Raw spectrum ==
45
+ ax[0].plot(x_raw, y_raw, label="Raw", color="dimgray", linewidth=1)
46
+ ax[0].set_title("Raw Input Spectrum")
47
+ ax[0].set_xlabel("Wavenumber (cm⁻¹)")
48
+ ax[0].set_ylabel("Intensity")
49
+ ax[0].grid(True, alpha=0.3)
50
+ ax[0].legend()
51
+
52
+ # == Resampled spectrum ==
53
+ ax[1].plot(
54
+ x_resampled, y_resampled, label="Resampled", color="steelblue", linewidth=1
55
+ )
56
+ ax[1].set_title(f"Resampled ({len(y_resampled)} points)")
57
+ ax[1].set_xlabel("Wavenumber (cm⁻¹)")
58
+ ax[1].set_ylabel("Intensity")
59
+ ax[1].grid(True, alpha=0.3)
60
+ ax[1].legend()
61
+
62
+ fig.tight_layout()
63
+ # == Convert to image ==
64
+ buf = io.BytesIO()
65
+ plt.savefig(buf, format="png", bbox_inches="tight", dpi=100)
66
+ buf.seek(0)
67
+ plt.close(fig) # Prevent memory leaks
68
+
69
+ return Image.open(buf)
70
+
71
+
72
+ def render_confidence_progress(
73
+ probs: np.ndarray,
74
+ labels: list[str] = ["Stable", "Weathered"],
75
+ highlight_idx: Union[int, None] = None,
76
+ side_by_side: bool = True,
77
+ ):
78
+ """Render Streamlit native progress bars with scientific formatting."""
79
+ p = np.asarray(probs, dtype=float)
80
+ p = np.clip(p, 0.0, 1.0)
81
+
82
+ if side_by_side:
83
+ cols = st.columns(len(labels))
84
+ for i, (lbl, val, col) in enumerate(zip(labels, p, cols)):
85
+ with col:
86
+ is_highlighted = highlight_idx is not None and i == highlight_idx
87
+ label_text = f"**{lbl}**" if is_highlighted else lbl
88
+ st.markdown(f"{label_text}: {val*100:.1f}%")
89
+ st.progress(int(round(val * 100)))
90
+ else:
91
+ # Vertical layout for better readability
92
+ for i, (lbl, val) in enumerate(zip(labels, p)):
93
+ is_highlighted = highlight_idx is not None and i == highlight_idx
94
+
95
+ # Create a container for each probability
96
+ with st.container():
97
+ col1, col2 = st.columns([3, 1])
98
+ with col1:
99
+ if is_highlighted:
100
+ st.markdown(f"**{lbl}** ← Predicted")
101
+ else:
102
+ st.markdown(f"{lbl}")
103
+ with col2:
104
+ st.metric(label="", value=f"{val*100:.1f}%", delta=None)
105
+
106
+ # Progress bar with conditional styling
107
+ if is_highlighted:
108
+ st.progress(int(round(val * 100)))
109
+ st.caption("🎯 **Model Prediction**")
110
+ else:
111
+ st.progress(int(round(val * 100)))
112
+
113
+ if i < len(labels) - 1: # Add spacing between items
114
+ st.markdown("")
115
+
116
+
117
+ def render_kv_grid(d: dict = {}, ncols: int = 2):
118
+ if d is None:
119
+ d = {}
120
+ if not d:
121
+ return
122
+ items = list(d.items())
123
+ cols = st.columns(ncols)
124
+ for i, (k, v) in enumerate(items):
125
+ with cols[i % ncols]:
126
+ st.caption(f"**{k}:** {v}")
127
+
128
+
129
+ def render_model_meta(model_choice: str):
130
+ info = MODEL_CONFIG.get(model_choice, {})
131
+ emoji = info.get("emoji", "")
132
+ desc = info.get("description", "").strip()
133
+ acc = info.get("accuracy", "-")
134
+ f1 = info.get("f1", "-")
135
+
136
+ st.caption(f"{emoji} **Model Snapshot** - {model_choice}")
137
+ cols = st.columns(2)
138
+ with cols[0]:
139
+ st.metric("Accuracy", acc)
140
+ with cols[1]:
141
+ st.metric("F1 Score", f1)
142
+ if desc:
143
+ st.caption(desc)
144
+
145
+
146
+ def get_confidence_description(logit_margin):
147
+ """Get human-readable confidence description"""
148
+ if logit_margin > 1000:
149
+ return "VERY HIGH", "🟢"
150
+ elif logit_margin > 250:
151
+ return "HIGH", "🟡"
152
+ elif logit_margin > 100:
153
+ return "MODERATE", "🟠"
154
+ else:
155
+ return "LOW", "🔴"
156
+
157
+
158
+ def render_sidebar():
159
+ with st.sidebar:
160
+ # Header
161
+ st.header("AI-Driven Polymer Classification")
162
+ st.caption(
163
+ "Predict polymer degradation (Stable vs Weathered) from Raman spectra using validated CNN models. — v0.1"
164
+ )
165
+ model_labels = [
166
+ f"{MODEL_CONFIG[name]['emoji']} {name}" for name in MODEL_CONFIG.keys()
167
+ ]
168
+ selected_label = st.selectbox(
169
+ "Choose AI Model",
170
+ model_labels,
171
+ key="model_select",
172
+ on_change=on_model_change,
173
+ )
174
+ model_choice = selected_label.split(" ", 1)[1]
175
+
176
+ # ===Compact metadata directly under dropdown===
177
+ render_model_meta(model_choice)
178
+
179
+ # ===Collapsed info to reduce clutter===
180
+ with st.expander("About This App", icon=":material/info:", expanded=False):
181
+ st.markdown(
182
+ """
183
+ AI-Driven Polymer Aging Prediction and Classification
184
+
185
+ **Purpose**: Classify polymer degradation using AI
186
+ **Input**: Raman spectroscopy `.txt` files
187
+ **Models**: CNN architectures for binary classification
188
+ **Next**: More trained CNNs in evaluation pipeline
189
+
190
+
191
+ **Contributors**
192
+ Dr. Sanmukh Kuppannagari (Mentor)
193
+ Dr. Metin Karailyan (Mentor)
194
+ Jaser Hasan (Author)
195
+
196
+
197
+ **Links**
198
+ [Live HF Space](https://huggingface.co/spaces/dev-jas/polymer-aging-ml)
199
+ [GitHub Repository](https://github.com/KLab-AI3/ml-polymer-recycling)
200
+
201
+
202
+ **Citation Figure2CNN (baseline)**
203
+ Neo et al., 2023, *Resour. Conserv. Recycl.*, 188, 106718.
204
+ [https://doi.org/10.1016/j.resconrec.2022.106718](https://doi.org/10.1016/j.resconrec.2022.106718)
205
+ """,
206
+ )
207
+
208
+
209
+ # col1 goes here
210
+
211
+ # In modules/ui_components.py
212
+
213
+
214
+ def render_input_column():
215
+ st.markdown("##### Data Input")
216
+
217
+ mode = st.radio(
218
+ "Input mode",
219
+ ["Upload File", "Batch Upload", "Sample Data"],
220
+ key="input_mode",
221
+ horizontal=True,
222
+ on_change=on_input_mode_change,
223
+ )
224
+
225
+ # == Input Mode Logic ==
226
+ # ... (The if/elif/else block for Upload, Batch, and Sample modes remains exactly the same) ...
227
+ # ==Upload tab==
228
+ if mode == "Upload File":
229
+ upload_key = st.session_state["current_upload_key"]
230
+ up = st.file_uploader(
231
+ "Upload Raman spectrum (.txt)",
232
+ type="txt",
233
+ help="Upload a text file with wavenumber and intensity columns",
234
+ key=upload_key, # ← versioned key
235
+ )
236
+
237
+ # ==Process change immediately (no on_change; simpler & reliable)==
238
+ if up is not None:
239
+ raw = up.read()
240
+ text = raw.decode("utf-8") if isinstance(raw, bytes) else raw
241
+ # == only reparse if its a different file|source ==
242
+ if (
243
+ st.session_state.get("filename") != getattr(up, "name", None)
244
+ or st.session_state.get("input_source") != "upload"
245
+ ):
246
+ st.session_state["input_text"] = text
247
+ st.session_state["filename"] = getattr(up, "name", None)
248
+ st.session_state["input_source"] = "upload"
249
+ # Ensure single file mode
250
+ st.session_state["batch_mode"] = False
251
+ st.session_state["status_message"] = (
252
+ f"File '{st.session_state['filename']}' ready for analysis"
253
+ )
254
+ st.session_state["status_type"] = "success"
255
+ reset_results("New file uploaded")
256
+
257
+ # ==Batch Upload tab==
258
+ elif mode == "Batch Upload":
259
+ st.session_state["batch_mode"] = True
260
+ # --- START: BUG 1 & 3 FIX ---
261
+ # Use a versioned key to ensure the file uploader resets properly.
262
+ batch_upload_key = f"batch_upload_{st.session_state['uploader_version']}"
263
+ uploaded_files = st.file_uploader(
264
+ "Upload multiple Raman spectrum files (.txt)",
265
+ type="txt",
266
+ accept_multiple_files=True,
267
+ help="Upload one or more text files with wavenumber and intensity columns.",
268
+ key=batch_upload_key,
269
+ )
270
+ # --- END: BUG 1 & 3 FIX ---
271
+
272
+ if uploaded_files:
273
+ # --- START: Bug 1 Fix ---
274
+ # Use a dictionary to keep only unique files based on name and size
275
+ unique_files = {(file.name, file.size): file for file in uploaded_files}
276
+ unique_file_list = list(unique_files.values())
277
+
278
+ num_uploaded = len(uploaded_files)
279
+ num_unique = len(unique_file_list)
280
+
281
+ # Optionally, inform the user that duplicates were removed
282
+ if num_uploaded > num_unique:
283
+ st.info(
284
+ f"ℹ️ {num_uploaded - num_unique} duplicate file(s) were removed."
285
+ )
286
+
287
+ # Use the unique list
288
+ st.session_state["batch_files"] = unique_file_list
289
+ st.session_state["status_message"] = (
290
+ f"{num_unique} ready for batch analysis"
291
+ )
292
+ st.session_state["status_type"] = "success"
293
+ # --- END: Bug 1 Fix ---
294
+ else:
295
+ st.session_state["batch_files"] = []
296
+ # This check prevents resetting the status if files are already staged
297
+ if not st.session_state.get("batch_files"):
298
+ st.session_state["status_message"] = (
299
+ "No files selected for batch processing"
300
+ )
301
+ st.session_state["status_type"] = "info"
302
+
303
+ # ==Sample tab==
304
+ elif mode == "Sample Data":
305
+ st.session_state["batch_mode"] = False
306
+ sample_files = get_sample_files()
307
+ if sample_files:
308
+ options = ["-- Select Sample --"] + [p.name for p in sample_files]
309
+ sel = st.selectbox(
310
+ "Choose sample spectrum:",
311
+ options,
312
+ key="sample_select",
313
+ on_change=on_sample_change,
314
+ )
315
+ if sel != "-- Select Sample --":
316
+ st.session_state["status_message"] = (
317
+ f"📁 Sample '{sel}' ready for analysis"
318
+ )
319
+ st.session_state["status_type"] = "success"
320
+ else:
321
+ st.info("No sample data available")
322
+ # == Status box (displays the message) ==
323
+ msg = st.session_state.get("status_message", "Ready")
324
+ typ = st.session_state.get("status_type", "info")
325
+ if typ == "success":
326
+ st.success(msg)
327
+ elif typ == "error":
328
+ st.error(msg)
329
+ else:
330
+ st.info(msg)
331
+
332
+ # --- DE-NESTED LOGIC STARTS HERE ---
333
+ # This code now runs on EVERY execution, guaranteeing the buttons will appear.
334
+
335
+ # Safely get model choice from session state
336
+ model_choice = st.session_state.get("model_select", " ").split(" ", 1)[1]
337
+ model = load_model(model_choice)
338
+
339
+ # Determine if the app is ready for inference
340
+ is_batch_ready = st.session_state.get("batch_mode", False) and st.session_state.get(
341
+ "batch_files"
342
+ )
343
+ is_single_ready = not st.session_state.get(
344
+ "batch_mode", False
345
+ ) and st.session_state.get("input_text")
346
+ inference_ready = (is_batch_ready or is_single_ready) and model is not None
347
+ # Store for other modules to access
348
+ st.session_state["inference_ready"] = inference_ready
349
+
350
+ # Render buttons
351
+ with st.form("analysis_form", clear_on_submit=False):
352
+ submitted = st.form_submit_button(
353
+ "Run Analysis", type="primary", disabled=not inference_ready
354
+ )
355
+ st.button(
356
+ "Reset All",
357
+ on_click=reset_ephemeral_state,
358
+ help="Clear all uploaded files and results.",
359
+ )
360
+
361
+ # Handle form submission
362
+ if submitted and inference_ready:
363
+ if st.session_state.get("batch_mode"):
364
+ batch_files = st.session_state.get("batch_files", [])
365
+ with st.spinner(f"Processing {len(batch_files)} files ..."):
366
+ st.session_state["batch_results"] = process_multiple_files(
367
+ uploaded_files=batch_files,
368
+ model_choice=model_choice,
369
+ load_model_func=load_model,
370
+ run_inference_func=run_inference,
371
+ label_file_func=label_file,
372
+ )
373
+ else:
374
+ try:
375
+ x_raw, y_raw = parse_spectrum_data(st.session_state["input_text"])
376
+ x_resampled, y_resampled = resample_spectrum(x_raw, y_raw, TARGET_LEN)
377
+ st.session_state.update(
378
+ {
379
+ "x_raw": x_raw,
380
+ "y_raw": y_raw,
381
+ "x_resampled": x_resampled,
382
+ "y_resampled": y_resampled,
383
+ "inference_run_once": True,
384
+ }
385
+ )
386
+ except (ValueError, TypeError) as e:
387
+ st.error(f"Error processing spectrum data: {e}")
388
+
389
+
390
+ # col2 goes here
391
+
392
+
393
+ def render_results_column():
394
+ # Check if we're in batch more or have batch results
395
+ is_batch_mode = st.session_state.get("batch_mode", False)
396
+ has_batch_results = "batch_results" in st.session_state
397
+
398
+ if is_batch_mode and has_batch_results:
399
+ # Display batch results
400
+ st.markdown("##### Batch Analysis Results")
401
+ batch_results = st.session_state["batch_results"]
402
+ display_batch_results(batch_results)
403
+
404
+ # Add session results table
405
+ st.markdown("---")
406
+
407
+ st.button(
408
+ "Clear Results",
409
+ on_click=clear_batch_results,
410
+ help="Clear all uploaded files and results.",
411
+ )
412
+
413
+ ResultsManager.display_results_table()
414
+
415
+ elif st.session_state.get("inference_run_once", False) and not is_batch_mode:
416
+ st.markdown("##### Analysis Results")
417
+
418
+ # Get data from session state
419
+ x_raw = st.session_state.get("x_raw")
420
+ y_raw = st.session_state.get("y_raw")
421
+ x_resampled = st.session_state.get("x_resampled") # ← NEW
422
+ y_resampled = st.session_state.get("y_resampled")
423
+ filename = st.session_state.get("filename", "Unknown")
424
+
425
+ if all(v is not None for v in [x_raw, y_raw, y_resampled]):
426
+ # ===Run inference===
427
+ if y_resampled is None:
428
+ raise ValueError(
429
+ "y_resampled is None. Ensure spectrum data is properly resampled before proceeding."
430
+ )
431
+ cache_key = hashlib.md5(
432
+ f"{y_resampled.tobytes()}{st.session_state.get('model_select', 'Unknown').split(' ', 1)[1]}".encode()
433
+ ).hexdigest()
434
+ prediction, logits_list, probs, inference_time, logits = run_inference(
435
+ y_resampled,
436
+ (
437
+ st.session_state.get("model_select", "").split(" ", 1)[1]
438
+ if "model_select" in st.session_state
439
+ else None
440
+ ),
441
+ _cache_key=cache_key,
442
+ )
443
+ if prediction is None:
444
+ st.error(
445
+ "❌ Inference failed: Model not loaded. Please check that weights are available."
446
+ )
447
+ st.stop() # prevents the rest of the code in this block from executing
448
+
449
+ log_message(
450
+ f"Inference completed in {inference_time:.2f}s, prediction: {prediction}"
451
+ )
452
+
453
+ # ===Get ground truth===
454
+ true_label_idx = label_file(filename)
455
+ true_label_str = (
456
+ LABEL_MAP.get(true_label_idx, "Unknown")
457
+ if true_label_idx is not None
458
+ else "Unknown"
459
+ )
460
+ # ===Get prediction===
461
+ predicted_class = LABEL_MAP.get(int(prediction), f"Class {int(prediction)}")
462
+
463
+ # Enhanced confidence calculation
464
+ if logits is not None:
465
+ # Use new softmax-based confidence
466
+ probs_np, max_confidence, confidence_level, confidence_emoji = (
467
+ calculate_softmax_confidence(logits)
468
+ )
469
+ confidence_desc = confidence_level
470
+ else:
471
+ # Fallback to legace method
472
+ logit_margin = abs(
473
+ (logits_list[0] - logits_list[1])
474
+ if logits_list is not None and len(logits_list) >= 2
475
+ else 0
476
+ )
477
+ confidence_desc, confidence_emoji = get_confidence_description(
478
+ logit_margin
479
+ )
480
+ max_confidence = logit_margin / 10.0 # Normalize for display
481
+ probs_np = np.array([])
482
+
483
+ # Store result in results manager for single file too
484
+ ResultsManager.add_results(
485
+ filename=filename,
486
+ model_name=(
487
+ st.session_state.get("model_select", "").split(" ", 1)[1]
488
+ if "model_select" in st.session_state
489
+ else "Unknown"
490
+ ),
491
+ prediction=int(prediction),
492
+ predicted_class=predicted_class,
493
+ confidence=max_confidence,
494
+ logits=logits_list if logits_list else [],
495
+ ground_truth=true_label_idx if true_label_idx >= 0 else None,
496
+ processing_time=inference_time if inference_time is not None else 0.0,
497
+ metadata={
498
+ "confidence_level": confidence_desc,
499
+ "confidence_emoji": confidence_emoji,
500
+ },
501
+ )
502
+
503
+ # ===Precompute Stats===
504
+ model_choice = (
505
+ st.session_state.get("model_select", "").split(" ", 1)[1]
506
+ if "model_select" in st.session_state
507
+ else None
508
+ )
509
+ if not model_choice:
510
+ st.error(
511
+ "⚠️ Model choice is not defined. Please select a model from the sidebar."
512
+ )
513
+ st.stop()
514
+ model_path = MODEL_CONFIG[model_choice]["path"]
515
+ mtime = os.path.getmtime(model_path) if os.path.exists(model_path) else None
516
+ file_hash = (
517
+ hashlib.md5(open(model_path, "rb").read()).hexdigest()
518
+ if os.path.exists(model_path)
519
+ else "N/A"
520
+ )
521
+ # Removed unused variable 'input_tensor'
522
+
523
+ start_render = time.time()
524
+
525
+ active_tab = st.selectbox(
526
+ "View Results",
527
+ ["Details", "Technical", "Explanation"],
528
+ key="active_tab", # reuse the key you were managing manually
529
+ )
530
+
531
+ if active_tab == "Details":
532
+ st.markdown('<div class="expander-results">', unsafe_allow_html=True)
533
+ # Use a dynamic and informative title for the expander
534
+ with st.expander(f"Results for {filename}", expanded=True):
535
+
536
+ # --- START: STREAMLINED METRICS ---
537
+ # A single, powerful row for the most important results.
538
+ key_metric_cols = st.columns(3)
539
+
540
+ # Metric 1: The Prediction
541
+ key_metric_cols[0].metric("Prediction", predicted_class)
542
+
543
+ # Metric 2: The Confidence (with level in tooltip)
544
+ confidence_icon = (
545
+ "🟢"
546
+ if max_confidence >= 0.8
547
+ else "🟡" if max_confidence >= 0.6 else "🔴"
548
+ )
549
+ key_metric_cols[1].metric(
550
+ "Confidence",
551
+ f"{confidence_icon} {max_confidence:.1%}",
552
+ help=f"Confidence Level: {confidence_desc}",
553
+ )
554
+
555
+ # Metric 3: Ground Truth + Correctness (Combined)
556
+ if true_label_idx is not None:
557
+ is_correct = predicted_class == true_label_str
558
+ delta_text = "✅ Correct" if is_correct else "❌ Incorrect"
559
+ # Use delta_color="normal" to let the icon provide the visual cue
560
+ key_metric_cols[2].metric(
561
+ "Ground Truth",
562
+ true_label_str,
563
+ delta=delta_text,
564
+ delta_color="normal",
565
+ )
566
+ else:
567
+ key_metric_cols[2].metric("Ground Truth", "N/A")
568
+
569
+ st.divider()
570
+ # --- END: STREAMLINED METRICS ---
571
+
572
+ # --- START: CONSOLIDATED CONFIDENCE ANALYSIS ---
573
+ st.markdown("##### Probability Breakdown")
574
+
575
+ # This custom bullet bar logic remains as it is highly specific and valuable
576
+ def create_bullet_bar(probability, width=20, predicted=False):
577
+ filled_count = int(probability * width)
578
+ bar = "▤" * filled_count + "▢" * (width - filled_count)
579
+ percentage = f"{probability:.1%}"
580
+ pred_marker = "↩ Predicted" if predicted else ""
581
+ return f"{bar} {percentage} {pred_marker}"
582
+
583
+ if probs is not None:
584
+ stable_prob, weathered_prob = probs[0], probs[1]
585
+ else:
586
+ st.error(
587
+ "❌ Probability values are missing. Please check the inference process."
588
+ )
589
+ # Default values to prevent further errors
590
+ stable_prob, weathered_prob = 0.0, 0.0
591
+ is_stable_predicted, is_weathered_predicted = (
592
+ int(prediction) == 0
593
+ ), (int(prediction) == 1)
594
+
595
+ st.markdown(
596
+ f"""
597
+ <div style="font-family: 'Fira Code', monospace;">
598
+ Stable (Unweathered)<br>
599
+ {create_bullet_bar(stable_prob, predicted=is_stable_predicted)}<br><br>
600
+ Weathered (Degraded)<br>
601
+ {create_bullet_bar(weathered_prob, predicted=is_weathered_predicted)}
602
+ </div>
603
+ """,
604
+ unsafe_allow_html=True,
605
+ )
606
+ # --- END: CONSOLIDATED CONFIDENCE ANALYSIS ---
607
+
608
+ st.divider()
609
+
610
+ # --- START: CLEAN METADATA FOOTER ---
611
+ # Secondary info is now a clean, single-line caption
612
+ st.caption(
613
+ f"Analyzed with **{st.session_state.get('model_select', 'Unknown')}** in **{inference_time:.2f}s**."
614
+ )
615
+ # --- END: CLEAN METADATA FOOTER ---
616
+
617
+ st.markdown("</div>", unsafe_allow_html=True)
618
+
619
+ elif active_tab == "Technical":
620
+ with st.container():
621
+ st.markdown("Technical Diagnostics")
622
+
623
+ # Model performance metrics
624
+ with st.container(border=True):
625
+ st.markdown("##### **Model Performance**")
626
+ tech_col1, tech_col2 = st.columns(2)
627
+
628
+ with tech_col1:
629
+ st.metric("Inference Time", f"{inference_time:.3f}s")
630
+ st.metric(
631
+ "Input Length",
632
+ f"{len(x_raw) if x_raw is not None else 0} points",
633
+ )
634
+ st.metric("Resampled Length", f"{TARGET_LEN} points")
635
+
636
+ with tech_col2:
637
+ st.metric(
638
+ "Model Loaded",
639
+ (
640
+ "✅ Yes"
641
+ if st.session_state.get("model_loaded", False)
642
+ else "❌ No"
643
+ ),
644
+ )
645
+ st.metric("Device", "CPU")
646
+ st.metric("Confidence Score", f"{max_confidence:.3f}")
647
+
648
+ # Raw logits display
649
+ with st.container(border=True):
650
+ st.markdown("##### **Raw Model Outputs (Logits)**")
651
+ logits_df = {
652
+ "Class": (
653
+ [
654
+ LABEL_MAP.get(i, f"Class {i}")
655
+ for i in range(len(logits_list))
656
+ ]
657
+ if logits_list is not None
658
+ else []
659
+ ),
660
+ "Logit Value": (
661
+ [f"{score:.4f}" for score in logits_list]
662
+ if logits_list is not None
663
+ else []
664
+ ),
665
+ "Probability": (
666
+ [f"{prob:.4f}" for prob in probs_np]
667
+ if logits_list is not None and len(probs_np) > 0
668
+ else []
669
+ ),
670
+ }
671
+
672
+ # Display as a simple table format
673
+ for i, (cls, logit, prob) in enumerate(
674
+ zip(
675
+ logits_df["Class"],
676
+ logits_df["Logit Value"],
677
+ logits_df["Probability"],
678
+ )
679
+ ):
680
+ col1, col2, col3 = st.columns([2, 1, 1])
681
+ with col1:
682
+ if i == prediction:
683
+ st.markdown(f"**{cls}** ← Predicted")
684
+ else:
685
+ st.markdown(cls)
686
+ with col2:
687
+ st.caption(f"Logit: {logit}")
688
+ with col3:
689
+ st.caption(f"Prob: {prob}")
690
+
691
+ # Spectrum statistics in organized sections
692
+ with st.container(border=True):
693
+ st.markdown("##### **Spectrum Analysis**")
694
+ spec_cols = st.columns(2)
695
+
696
+ with spec_cols[0]:
697
+ st.markdown("**Original Spectrum:**")
698
+ render_kv_grid(
699
+ {
700
+ "Length": f"{len(x_raw) if x_raw is not None else 0} points",
701
+ "Range": (
702
+ f"{min(x_raw):.1f} - {max(x_raw):.1f} cm⁻¹"
703
+ if x_raw is not None
704
+ else "N/A"
705
+ ),
706
+ "Min Intensity": (
707
+ f"{min(y_raw):.2e}"
708
+ if y_raw is not None
709
+ else "N/A"
710
+ ),
711
+ "Max Intensity": (
712
+ f"{max(y_raw):.2e}"
713
+ if y_raw is not None
714
+ else "N/A"
715
+ ),
716
+ },
717
+ ncols=1,
718
+ )
719
+
720
+ with spec_cols[1]:
721
+ st.markdown("**Processed Spectrum:**")
722
+ render_kv_grid(
723
+ {
724
+ "Length": f"{TARGET_LEN} points",
725
+ "Resampling": "Linear interpolation",
726
+ "Normalization": "None",
727
+ "Input Shape": f"(1, 1, {TARGET_LEN})",
728
+ },
729
+ ncols=1,
730
+ )
731
+
732
+ # Model information
733
+ with st.container(border=True):
734
+ st.markdown("##### **Model Information**")
735
+ model_info_cols = st.columns(2)
736
+
737
+ with model_info_cols[0]:
738
+ render_kv_grid(
739
+ {
740
+ "Architecture": model_choice,
741
+ "Path": MODEL_CONFIG[model_choice]["path"],
742
+ "Weights Modified": (
743
+ time.strftime(
744
+ "%Y-%m-%d %H:%M:%S", time.localtime(mtime)
745
+ )
746
+ if mtime
747
+ else "N/A"
748
+ ),
749
+ },
750
+ ncols=1,
751
+ )
752
+
753
+ with model_info_cols[1]:
754
+ if os.path.exists(model_path):
755
+ file_hash = hashlib.md5(
756
+ open(model_path, "rb").read()
757
+ ).hexdigest()
758
+ render_kv_grid(
759
+ {
760
+ "Weights Hash": f"{file_hash[:16]}...",
761
+ "Output Shape": f"(1, {len(LABEL_MAP)})",
762
+ "Activation": "Softmax",
763
+ },
764
+ ncols=1,
765
+ )
766
+
767
+ # Debug logs (collapsed by default)
768
+ with st.expander("📋 Debug Logs", expanded=False):
769
+ log_content = "\n".join(
770
+ st.session_state.get("log_messages", [])
771
+ )
772
+ if log_content.strip():
773
+ st.code(log_content, language="text")
774
+ else:
775
+ st.caption("No debug logs available")
776
+
777
+ elif active_tab == "Explanation":
778
+ with st.container():
779
+ st.markdown("### 🔍 Methodology & Interpretation")
780
+
781
+ # Process explanation
782
+ st.markdown("Analysis Pipeline")
783
+ process_steps = [
784
+ "📁 **Data Upload**: Raman spectrum file loaded and validated",
785
+ "🔍 **Preprocessing**: Spectrum parsed and resampled to 500 data points using linear interpolation",
786
+ "🧠 **AI Inference**: Convolutional Neural Network analyzes spectral patterns and molecular signatures",
787
+ "📊 **Classification**: Binary prediction with confidence scoring using softmax probabilities",
788
+ "✅ **Validation**: Ground truth comparison (when available from filename)",
789
+ ]
790
+
791
+ for step in process_steps:
792
+ st.markdown(step)
793
+
794
+ st.markdown("---")
795
+
796
+ # Model interpretation
797
+ st.markdown("#### Scientific Interpretation")
798
+
799
+ interp_col1, interp_col2 = st.columns(2)
800
+
801
+ with interp_col1:
802
+ st.markdown("**Stable (Unweathered) Polymers:**")
803
+ st.info(
804
+ """
805
+ - Well-preserved molecular structure
806
+ - Minimal oxidative degradation
807
+ - Characteristic Raman peaks intact
808
+ - Suitable for recycling applications
809
+ """
810
+ )
811
+
812
+ with interp_col2:
813
+ st.markdown("**Weathered (Degraded) Polymers:**")
814
+ st.warning(
815
+ """
816
+ - Oxidized molecular bonds
817
+ - Surface degradation present
818
+ - Altered spectral signatures
819
+ - May require additional processing
820
+ """
821
+ )
822
+
823
+ st.markdown("---")
824
+
825
+ # Applications
826
+ st.markdown("#### Research Applications")
827
+
828
+ applications = [
829
+ "🔬 **Material Science**: Polymer degradation studies",
830
+ "♻️ **Recycling Research**: Viability assessment for circular economy",
831
+ "🌱 **Environmental Science**: Microplastic weathering analysis",
832
+ "🏭 **Quality Control**: Manufacturing process monitoring",
833
+ "📈 **Longevity Studies**: Material aging prediction",
834
+ ]
835
+
836
+ for app in applications:
837
+ st.markdown(app)
838
+
839
+ # Technical details
840
+ # MODIFIED: Wrap the expander in a div with the 'expander-advanced' class
841
+ st.markdown(
842
+ '<div class="expander-advanced">', unsafe_allow_html=True
843
+ )
844
+ with st.expander("🔧 Technical Details", expanded=False):
845
+ st.markdown(
846
+ """
847
+ **Model Architecture:**
848
+ - Convolutional layers for feature extraction
849
+ - Residual connections for gradient flow
850
+ - Fully connected layers for classification
851
+ - Softmax activation for probability distribution
852
+
853
+ **Performance Metrics:**
854
+ - Accuracy: 94.8-96.2% on validation set
855
+ - F1-Score: 94.3-95.9% across classes
856
+ - Robust to spectral noise and baseline variations
857
+
858
+ **Data Processing:**
859
+ - Input: Raman spectra (any length)
860
+ - Resampling: Linear interpolation to 500 points
861
+ - Normalization: None (preserves intensity relationships)
862
+ """
863
+ )
864
+ st.markdown(
865
+ "</div>", unsafe_allow_html=True
866
+ ) # Close the wrapper div
867
+
868
+ render_time = time.time() - start_render
869
+ log_message(
870
+ f"col2 rendered in {render_time:.2f}s, active tab: {active_tab}"
871
+ )
872
+
873
+ with st.expander("Spectrum Preprocessing Results", expanded=False):
874
+ st.caption("<br>Spectral Analysis", unsafe_allow_html=True)
875
+
876
+ # Add some context about the preprocessing
877
+ st.markdown(
878
+ """
879
+ **Preprocessing Overview:**
880
+ - **Original Spectrum**: Raw Raman data as uploaded
881
+ - **Resampled Spectrum**: Data interpolated to 500 points for model input
882
+ - **Purpose**: Ensures consistent input dimensions for neural network
883
+ """
884
+ )
885
+
886
+ # Create and display plot
887
+ cache_key = hashlib.md5(
888
+ f"{(x_raw.tobytes() if x_raw is not None else b'')}"
889
+ f"{(y_raw.tobytes() if y_raw is not None else b'')}"
890
+ f"{(x_resampled.tobytes() if x_resampled is not None else b'')}"
891
+ f"{(y_resampled.tobytes() if y_resampled is not None else b'')}".encode()
892
+ ).hexdigest()
893
+ spectrum_plot = create_spectrum_plot(
894
+ x_raw, y_raw, x_resampled, y_resampled, _cache_key=cache_key
895
+ )
896
+ st.image(
897
+ spectrum_plot,
898
+ caption="Raman Spectrum: Raw vs Processed",
899
+ use_container_width=True,
900
+ )
901
+
902
+ else:
903
+ st.error("❌ Missing spectrum data. Please upload a file and run analysis.")
904
+ else:
905
+ # ===Getting Started===
906
+ st.markdown(
907
+ """
908
+ ##### How to Get Started
909
+
910
+ 1. **Select an AI Model:** Use the dropdown menu in the sidebar to choose a model.
911
+ 2. **Provide Your Data:** Select one of the three input modes:
912
+ - **Upload File:** Analyze a single spectrum.
913
+ - **Batch Upload:** Process multiple files at once.
914
+ - **Sample Data:** Explore functionality with pre-loaded examples.
915
+ 3. **Run Analysis:** Click the "Run Analysis" button to generate the classification results.
916
+
917
+ ---
918
+
919
+ ##### Supported Data Format
920
+
921
+ - **File Type:** Plain text (`.txt`)
922
+ - **Content:** Must contain two columns: `wavenumber` and `intensity`.
923
+ - **Separators:** Values can be separated by spaces or commas.
924
+ - **Preprocessing:** Your spectrum will be automatically resampled to 500 data points to match the model's input requirements.
925
+
926
+ ---
927
+
928
+ ##### Example Applications
929
+ - 🔬 Research on polymer degradation
930
+ - ♻️ Recycling feasibility assessment
931
+ - 🌱 Sustainability impact studies
932
+ - 🏭 Quality control in manufacturing
933
+ """
934
+ )
static/style.css ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* THEME-AWARE CUSTOM CSS
2
+
3
+ This CSS block has been refactored to use Streamlit's internal theme
4
+ variables. This ensures that all custom components will automatically adapt
5
+ to both light and dark themes selected by the user in the settings menu.
6
+ */
7
+ /* ====== Font Imports (Optional but Recommended) ====== */
8
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&family=Fira+Code:wght@400&display=swap');
9
+
10
+ /* ====== Base & Typography ====== */
11
+ .stApp,
12
+ section[data-testid="stSidebar"],
13
+ div[data-testid="stMetricValue"],
14
+ div[data-testid="stMetricLabel"] {
15
+ font-family: 'Inter', sans-serif;
16
+ /* Uses the main text color from the current theme (light or dark) */
17
+ color: var(--text-color);
18
+ }
19
+
20
+ .kv-val {
21
+ font-family: 'Fira Code', monospace;
22
+ }
23
+
24
+ /* ====== Custom Containers: Tabs & Info Boxes ====== */
25
+ div[data-testid="stTabs"]>div[role="tablist"]+div {
26
+ min-height: 400px;
27
+ /* Uses the secondary background color, which is different in light and dark modes */
28
+ background-color: var(--secondary-background-color);
29
+ /* Border color uses a semi-transparent version of the text color for a subtle effect that works on any background */
30
+ border: 10px solid rgba(128, 128, 128, 0.2);
31
+ border-radius: 10px;
32
+ padding: 24px;
33
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
34
+ }
35
+
36
+ .info-box {
37
+ font-size: 0.9rem;
38
+ padding: 12px 16px;
39
+ border: 1px solid rgba(128, 128, 128, 0.2);
40
+ border-radius: 10px;
41
+ background-color: var(--secondary-background-color);
42
+ }
43
+
44
+ /* ====== Key-Value Pair Styling ====== */
45
+ .kv-row {
46
+ display: flex;
47
+ justify-content: space-between;
48
+ gap: 16px;
49
+ padding: 8px 0;
50
+ border-bottom: 1px solid rgba(128, 128, 128, 0.2);
51
+ }
52
+
53
+ .kv-row:last-child {
54
+ border-bottom: none;
55
+ }
56
+
57
+ .kv-key {
58
+ opacity: 0.7;
59
+ font-size: 0.9rem;
60
+ white-space: nowrap;
61
+ }
62
+
63
+ .kv-val {
64
+ font-size: 0.9rem;
65
+ overflow-wrap: break-word;
66
+ text-align: right;
67
+ }
68
+
69
+ /* ====== Custom Expander Styling ====== */
70
+ div.stExpander>details>summary::-webkit-details-marker,
71
+ div.stExpander>details>summary::marker,
72
+ div[data-testid="stExpander"] summary svg {
73
+ display: none !important;
74
+ }
75
+
76
+ div.stExpander>details>summary::after {
77
+ content: 'DETAILS';
78
+ font-size: 0.75rem;
79
+ font-weight: 600;
80
+ letter-spacing: 0.5px;
81
+ padding: 4px 12px;
82
+ border-radius: 999px;
83
+ /* The primary color is set in config.toml and adapted by Streamlit */
84
+ background-color: var(--primary);
85
+ /* Text on the primary color needs high contrast. White works well for our chosen purple. */
86
+
87
+ transition: background-color 0.2s ease-in-out;
88
+ }
89
+
90
+ div.stExpander>details>summary:hover::after {
91
+ /* Using a fixed darker shade on hover. A more advanced solution could use color-mix() in CSS. */
92
+ filter: brightness(90%);
93
+ }
94
+
95
+ /* Specialized Expander Labels */
96
+ .expander-results div[data-testid="stExpander"] summary::after {
97
+ content: "RESULTS";
98
+ background-color: #16A34A;
99
+ /* Green is universal for success */
100
+
101
+ }
102
+
103
+ div[data-testid="stExpander"] details {
104
+ content: "RESULTS";
105
+ background-color: var(--primary);
106
+ border-radius: 10px;
107
+ padding: 10px
108
+ }
109
+
110
+ .expander-advanced div[data-testid="stExpander"] summary::after {
111
+ content: "ADVANCED";
112
+ background-color: #D97706;
113
+ /* Amber is universal for warning/technical */
114
+
115
+ }
116
+
117
+ [data-testid="stExpanderDetails"] {
118
+ padding: 16px 4px 4px 4px;
119
+ background-color: transparent;
120
+ border-top: 1px solid rgba(128, 128, 128, 0.2);
121
+ margin-top: 12px;
122
+ }
123
+
124
+ /* ====== Sidebar & Metrics ====== */
125
+ section[data-testid="stSidebar"]>div:first-child {
126
+ background-color: var(--secondary-background-color);
127
+ border-right: 1px solid rgba(128, 128, 128, 0.2);
128
+ }
129
+
130
+ div[data-testid="stMetricValue"] {
131
+ font-size: 1.1rem !important;
132
+ font-weight: 500;
133
+ }
134
+
135
+ div[data-testid="stMetricLabel"] {
136
+ font-size: 0.85rem !important;
137
+ opacity: 0.8;
138
+ }
139
+
140
+ /* ====== Interactivity & Accessibility ====== */
141
+ :focus-visible {
142
+ /* The focus outline now uses the theme's primary color */
143
+ outline: 2px solid var(--primary);
144
+ outline-offset: 2px;
145
+ border-radius: 8px;
146
+ }
147
+
148
+ </style>