niol08 commited on
Commit
d88eb7e
·
verified ·
1 Parent(s): c6031ec

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +59 -62
src/streamlit_app.py CHANGED
@@ -93,19 +93,24 @@ for tab, sig in zip(tabs, ["ECG", "EMG", "VAG", "PCG"]):
93
  "_Note: Do not upload `.mp3`, `.flac`, or stereo files—they may fail to process properly._"
94
  )
95
 
 
 
 
96
  if f"uploaded_file_{sig}" not in st.session_state:
97
  st.session_state[f"uploaded_file_{sig}"] = None
98
- if f"file_processed_{sig}" not in st.session_state:
99
- st.session_state[f"file_processed_{sig}"] = False
100
-
 
 
101
  uploaded = st.file_uploader(
102
  f"Upload {sig} file",
103
  type=FILE_TYPES[sig],
104
- key=f"upload_{sig}"
105
- on_change=lambda: st.session_state.update({
106
- f"uploaded_file_{sig}": st.session_state[f"upload_{sig}"],
107
- f"file_processed_{sig}": False})
108
  )
 
 
109
  if uploaded is not None:
110
  st.session_state[f"uploaded_file_{sig}"] = uploaded
111
  st.success(f"✅ File uploaded: {uploaded.name} ({uploaded.size} bytes)")
@@ -118,17 +123,15 @@ for tab, sig in zip(tabs, ["ECG", "EMG", "VAG", "PCG"]):
118
  with col2:
119
  st.info(f"📏 **Size:** {uploaded.size} bytes")
120
  st.info(f"✅ **Status:** Ready for analysis")
121
-
122
- # Auto-enable the run button
123
- st.session_state[f"file_processed_{sig}"] = True
124
-
125
  elif st.session_state[f"uploaded_file_{sig}"] is not None:
126
- # Use previously uploaded file
127
  uploaded = st.session_state[f"uploaded_file_{sig}"]
128
- st.info(f"📁 Using file: {uploaded.name}")
129
  else:
130
  st.warning("⚠️ No file uploaded yet")
131
  st.info(f"💡 Supported formats: {', '.join(FILE_TYPES[sig])}")
 
 
132
  col1, col2 = st.columns([1, 1])
133
  with col1:
134
  if st.button(f"🔄 Refresh", key=f"refresh_{sig}"):
@@ -138,63 +141,57 @@ for tab, sig in zip(tabs, ["ECG", "EMG", "VAG", "PCG"]):
138
  if st.session_state[f"uploaded_file_{sig}"] is not None:
139
  if st.button(f"🗑️ Clear File", key=f"clear_{sig}"):
140
  st.session_state[f"uploaded_file_{sig}"] = None
141
- st.session_state[f"file_processed_{sig}"] = False
142
  st.rerun()
143
 
144
- # Update your analysis buttons to use session state
145
  if st.session_state[f"uploaded_file_{sig}"] is not None:
146
  uploaded = st.session_state[f"uploaded_file_{sig}"]
147
-
148
- if sig == "ECG" and uploaded and st.button("Run Diagnostic", key=f"run_{sig}", type="primary" ):
149
- label, human, conf, gnote = analyze_signal(
150
- uploaded, MODELS["ECG"], GEMINI_API_KEY, signal_type="ECG"
151
- )
152
- st.success(f"**{label} – {human}**\n\nConfidence: {conf:.2%}")
153
- if gnote:
154
- st.markdown("### 🧠 Gemini Insight")
155
- st.write(gnote)
156
- elif not GEMINI_API_KEY:
157
- st.info("Gemini key missing – no explanation.")
158
-
159
-
160
- elif sig == "PCG" and uploaded and st.button("Run Diagnostic", key=f"run_{sig}", type="primary"):
161
- label, human, conf, gnote = analyze_pcg_signal(
162
- uploaded, MODELS["PCG"], GEMINI_API_KEY
163
- )
164
- st.success(f"**{label}**\n\nConfidence: {conf:.2%}")
165
- if gnote:
166
- st.markdown("### 🧠 Gemini Insight")
167
- st.write(gnote)
168
- elif not GEMINI_API_KEY:
169
- st.info("Gemini key missing – no explanation.")
170
 
171
- elif sig == "EMG" and uploaded and st.button("Run Diagnostic", key=f"run_{sig}", type="primary"):
172
- human, conf, gnote = analyze_emg_signal(
173
  uploaded, MODELS["EMG"], GEMINI_API_KEY
174
- )
175
- st.success(f"**{human.upper()}**\n\nConfidence: {conf:.2%}")
176
- if gnote:
177
- st.markdown("### 🧠 Gemini Insight")
178
- st.write(gnote)
179
- elif not GEMINI_API_KEY:
180
- st.info("Gemini key missing – no explanation.")
181
-
182
- elif sig == "VAG" and uploaded and st.button("Run Diagnostic", key=f"run_{sig}", type="primary"):
183
 
184
- label, human, conf, gnote = predict_vag_from_features(
185
- uploaded, MODELS["VAG"], GEMINI_API_KEY
186
- )
187
- st.success(f"**{label}**\n\nConfidence: {conf:.2%}")
188
- if gnote:
189
- st.markdown("### 🧠 Gemini Insight")
190
- st.write(gnote)
191
- elif not GEMINI_API_KEY:
192
- st.info("Gemini key missing – no explanation.")
193
-
194
-
195
  else:
196
- if not uploaded:
197
- st.info("Upload a file to begin.")
198
 
199
 
200
  st.caption("© 2025 Biosignal Chatbot")
 
93
  "_Note: Do not upload `.mp3`, `.flac`, or stereo files—they may fail to process properly._"
94
  )
95
 
96
+ # Replace your file uploader section with this corrected version:
97
+
98
+ # Initialize session state
99
  if f"uploaded_file_{sig}" not in st.session_state:
100
  st.session_state[f"uploaded_file_{sig}"] = None
101
+
102
+ # File uploader with proper callback
103
+ def handle_file_upload():
104
+ st.session_state[f"uploaded_file_{sig}"] = st.session_state[f"upload_{sig}"]
105
+
106
  uploaded = st.file_uploader(
107
  f"Upload {sig} file",
108
  type=FILE_TYPES[sig],
109
+ key=f"upload_{sig}",
110
+ on_change=handle_file_upload
 
 
111
  )
112
+
113
+ # Check if file was uploaded
114
  if uploaded is not None:
115
  st.session_state[f"uploaded_file_{sig}"] = uploaded
116
  st.success(f"✅ File uploaded: {uploaded.name} ({uploaded.size} bytes)")
 
123
  with col2:
124
  st.info(f"📏 **Size:** {uploaded.size} bytes")
125
  st.info(f"✅ **Status:** Ready for analysis")
126
+
 
 
 
127
  elif st.session_state[f"uploaded_file_{sig}"] is not None:
 
128
  uploaded = st.session_state[f"uploaded_file_{sig}"]
129
+ st.info(f"📁 Using previously uploaded file: {uploaded.name}")
130
  else:
131
  st.warning("⚠️ No file uploaded yet")
132
  st.info(f"💡 Supported formats: {', '.join(FILE_TYPES[sig])}")
133
+
134
+ # Add control buttons
135
  col1, col2 = st.columns([1, 1])
136
  with col1:
137
  if st.button(f"🔄 Refresh", key=f"refresh_{sig}"):
 
141
  if st.session_state[f"uploaded_file_{sig}"] is not None:
142
  if st.button(f"🗑️ Clear File", key=f"clear_{sig}"):
143
  st.session_state[f"uploaded_file_{sig}"] = None
 
144
  st.rerun()
145
 
146
+ # Your existing analysis buttons (keep them the same, but check session state)
147
  if st.session_state[f"uploaded_file_{sig}"] is not None:
148
  uploaded = st.session_state[f"uploaded_file_{sig}"]
149
+
150
+ if sig == "ECG" and st.button("Run Diagnostic", key=f"run_{sig}"):
151
+ label, human, conf, gnote = analyze_signal(
152
+ uploaded, MODELS["ECG"], GEMINI_API_KEY, signal_type="ECG"
153
+ )
154
+ st.success(f"**{label} – {human}**\n\nConfidence: {conf:.2%}")
155
+ if gnote:
156
+ st.markdown("### 🧠 Gemini Insight")
157
+ st.write(gnote)
158
+ elif not GEMINI_API_KEY:
159
+ st.info("Gemini key missing – no explanation.")
160
+
161
+ elif sig == "PCG" and st.button("Run Diagnostic", key=f"run_{sig}"):
162
+ label, human, conf, gnote = analyze_pcg_signal(
163
+ uploaded, MODELS["PCG"], GEMINI_API_KEY
164
+ )
165
+ st.success(f"**{label}**\n\nConfidence: {conf:.2%}")
166
+ if gnote:
167
+ st.markdown("### 🧠 Gemini Insight")
168
+ st.write(gnote)
169
+ elif not GEMINI_API_KEY:
170
+ st.info("Gemini key missing – no explanation.")
 
171
 
172
+ elif sig == "EMG" and st.button("Run Diagnostic", key=f"run_{sig}"):
173
+ human, conf, gnote = analyze_emg_signal(
174
  uploaded, MODELS["EMG"], GEMINI_API_KEY
175
+ )
176
+ st.success(f"**{human.upper()}**\n\nConfidence: {conf:.2%}")
177
+ if gnote:
178
+ st.markdown("### 🧠 Gemini Insight")
179
+ st.write(gnote)
180
+ elif not GEMINI_API_KEY:
181
+ st.info("Gemini key missing – no explanation.")
 
 
182
 
183
+ elif sig == "VAG" and st.button("Run Diagnostic", key=f"run_{sig}"):
184
+ label, human, conf, gnote = predict_vag_from_features(
185
+ uploaded, MODELS["VAG"], GEMINI_API_KEY
186
+ )
187
+ st.success(f"**{label}**\n\nConfidence: {conf:.2%}")
188
+ if gnote:
189
+ st.markdown("### 🧠 Gemini Insight")
190
+ st.write(gnote)
191
+ elif not GEMINI_API_KEY:
192
+ st.info("Gemini key missing – no explanation.")
 
193
  else:
194
+ st.info("📤 Upload a file to begin analysis")
 
195
 
196
 
197
  st.caption("© 2025 Biosignal Chatbot")