renderfy commited on
Commit
1abbf79
·
verified ·
1 Parent(s): f7d17d1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -1,11 +1,11 @@
1
- # app.py — DiagStudio AI (Streamlit frontend, fixed)
2
- # Root-cause fix: prevent Streamlit from writing to '/.streamlit'
3
- # by defining a writable HOME and disabling usage stats before import.
4
 
5
  import os
6
- # ----- MUST be set BEFORE importing streamlit -----
7
  os.environ.setdefault("HOME", "/tmp")
8
- os.environ.setdefault("STREAMLIT_BROWSER_GATHER_USAGE_STATS", "false")
9
  os.environ.setdefault("STREAMLIT_SERVER_HEADLESS", "true")
10
 
11
  import io
@@ -15,7 +15,7 @@ import base64
15
  import requests
16
  import streamlit as st
17
 
18
- # ================= Theme (no extra files) =================
19
  try:
20
  st._config.set_option("theme.base", "dark")
21
  st._config.set_option("theme.primaryColor", "#ff7a1a")
@@ -26,11 +26,11 @@ except Exception:
26
  pass
27
 
28
  # ================= Env & API =================
29
- def _env(k):
30
  return (os.getenv(k) or "").strip().strip("'\"")
31
 
32
  API_BASE = (
33
- _env("CARDIAG_AI_API")
34
  or _env("AI_LIGHTBOX_API")
35
  or _env("LUXFIT_API")
36
  ).rstrip("/") if (
@@ -81,6 +81,7 @@ def _post_analyze(audio_file, rpm_file, sr_target, n_fft, hop, env_band, run_llm
81
  "extra_text": notes or "",
82
  }
83
 
 
84
  r = requests.post(f"{API_BASE}/analyze", headers=HEADERS, files=files, data=data, timeout=180)
85
  r.raise_for_status()
86
  return r.json()
@@ -106,7 +107,6 @@ def _post_analyze_zip(audio_file, rpm_file, sr_target, n_fft, hop, env_band, run
106
  return r.content
107
 
108
  def dataurl_to_bytes(data_url: str) -> bytes:
109
- # "data:image/png;base64,...."
110
  b64 = data_url.split(",", 1)[1]
111
  return base64.b64decode(b64)
112
 
@@ -156,15 +156,18 @@ if analyze_btn and audio_up is not None:
156
  res = st.session_state.get("last_result")
157
  if res:
158
  st.subheader("Result")
159
- col1, col2 = st.columns([1,1])
160
 
 
161
  llm = res.get("llm_report") or {}
162
- if isinstance(llm, str):
163
- try:
164
- llm = json.loads(llm)
165
- except Exception:
166
- llm = {"raw": llm}
167
-
 
 
 
168
  diag_list = llm.get("diagnosis", [])
169
  notes = llm.get("notes", "")
170
  next_tests = llm.get("next_tests", "")
@@ -174,10 +177,13 @@ if res:
174
  if diag_list:
175
  for item in diag_list:
176
  label = str(item.get("label", "Issue"))
177
- conf = _nice_conf(item.get("likelihood", 0))
178
  evidence = str(item.get("evidence", ""))
179
  st.text(label)
180
- st.progress(conf)
 
 
 
181
  if evidence:
182
  st.caption(evidence)
183
  else:
@@ -191,6 +197,7 @@ if res:
191
  st.markdown("**Suggested next checks**")
192
  st.write(next_tests)
193
 
 
194
  st.subheader("Visual checks")
195
  tabs = st.tabs([
196
  "Energy over time",
@@ -206,23 +213,18 @@ if res:
206
  with tabs[0]:
207
  if "stft" in imgs:
208
  st.image(imgs["stft"], caption="Energy distribution across time.")
209
-
210
  with tabs[1]:
211
  if "mel" in imgs:
212
  st.image(imgs["mel"], caption="Perceptually-weighted overview.")
213
-
214
  with tabs[2]:
215
  if "envelope_spectrum" in imgs:
216
  st.image(imgs["envelope_spectrum"], caption="Repetitive impact signature.")
217
-
218
  with tabs[3]:
219
  if "envelope_trace" in imgs:
220
  st.image(imgs["envelope_trace"], caption="Overall intensity trace.")
221
-
222
  with tabs[4]:
223
  if "cwt" in imgs:
224
  st.image(imgs["cwt"], caption="Fine details and short events.")
225
-
226
  with tabs[5]:
227
  if "order_map" in imgs:
228
  st.image(imgs["order_map"], caption="Patterns that track engine speed.")
 
1
+ # app.py — DiagStudio AI (Streamlit frontend, public HF Space)
2
+ # Kök dizine yazma girişimlerini engellemek için HOME ve headless/statistics env'lerini set ediyoruz.
3
+ # Streamlit konfig/env yöntemleri belgeli. # :contentReference[oaicite:6]{index=6}
4
 
5
  import os
6
+ # ---- set BEFORE importing streamlit ----
7
  os.environ.setdefault("HOME", "/tmp")
8
+ os.environ.setdefault("STREAMLIT_BROWSER_GATHER_USAGE_STATS", "false") # :contentReference[oaicite:7]{index=7}
9
  os.environ.setdefault("STREAMLIT_SERVER_HEADLESS", "true")
10
 
11
  import io
 
15
  import requests
16
  import streamlit as st
17
 
18
+ # ================= Theme =================
19
  try:
20
  st._config.set_option("theme.base", "dark")
21
  st._config.set_option("theme.primaryColor", "#ff7a1a")
 
26
  pass
27
 
28
  # ================= Env & API =================
29
+ def _env(k):
30
  return (os.getenv(k) or "").strip().strip("'\"")
31
 
32
  API_BASE = (
33
+ _env("CARDIAG_AI_API")
34
  or _env("AI_LIGHTBOX_API")
35
  or _env("LUXFIT_API")
36
  ).rstrip("/") if (
 
81
  "extra_text": notes or "",
82
  }
83
 
84
+ # multipart/form-data upload akışı FastAPI UploadFile'a uygun # :contentReference[oaicite:8]{index=8}
85
  r = requests.post(f"{API_BASE}/analyze", headers=HEADERS, files=files, data=data, timeout=180)
86
  r.raise_for_status()
87
  return r.json()
 
107
  return r.content
108
 
109
  def dataurl_to_bytes(data_url: str) -> bytes:
 
110
  b64 = data_url.split(",", 1)[1]
111
  return base64.b64decode(b64)
112
 
 
156
  res = st.session_state.get("last_result")
157
  if res:
158
  st.subheader("Result")
 
159
 
160
+ # ---- Model & usage meta (proof that GPT-4o Vision ran) ----
161
  llm = res.get("llm_report") or {}
162
+ model_name = llm.get("model", "unknown")
163
+ usage = llm.get("usage") or {}
164
+ with st.container():
165
+ st.caption(f"AI model: **{model_name}**")
166
+ if usage:
167
+ st.caption(f"Tokens — prompt: {usage.get('prompt_tokens')}, completion: {usage.get('completion_tokens')}")
168
+
169
+ # ---- Diagnosis summary ----
170
+ col1, col2 = st.columns([1,1])
171
  diag_list = llm.get("diagnosis", [])
172
  notes = llm.get("notes", "")
173
  next_tests = llm.get("next_tests", "")
 
177
  if diag_list:
178
  for item in diag_list:
179
  label = str(item.get("label", "Issue"))
180
+ conf = item.get("likelihood", 0)
181
  evidence = str(item.get("evidence", ""))
182
  st.text(label)
183
+ try:
184
+ st.progress(min(max(float(conf), 0.0), 1.0))
185
+ except Exception:
186
+ st.progress(0.0)
187
  if evidence:
188
  st.caption(evidence)
189
  else:
 
197
  st.markdown("**Suggested next checks**")
198
  st.write(next_tests)
199
 
200
+ # ---- Visuals ----
201
  st.subheader("Visual checks")
202
  tabs = st.tabs([
203
  "Energy over time",
 
213
  with tabs[0]:
214
  if "stft" in imgs:
215
  st.image(imgs["stft"], caption="Energy distribution across time.")
 
216
  with tabs[1]:
217
  if "mel" in imgs:
218
  st.image(imgs["mel"], caption="Perceptually-weighted overview.")
 
219
  with tabs[2]:
220
  if "envelope_spectrum" in imgs:
221
  st.image(imgs["envelope_spectrum"], caption="Repetitive impact signature.")
 
222
  with tabs[3]:
223
  if "envelope_trace" in imgs:
224
  st.image(imgs["envelope_trace"], caption="Overall intensity trace.")
 
225
  with tabs[4]:
226
  if "cwt" in imgs:
227
  st.image(imgs["cwt"], caption="Fine details and short events.")
 
228
  with tabs[5]:
229
  if "order_map" in imgs:
230
  st.image(imgs["order_map"], caption="Patterns that track engine speed.")