larxius commited on
Commit
19bb33f
·
1 Parent(s): ec7c945

fix(lab): attach + validation + neutral branding

Browse files
Files changed (2) hide show
  1. app.py +22 -0
  2. templates/doctor/lab_analysis.html +44 -4
app.py CHANGED
@@ -560,12 +560,34 @@ def create_app():
560
  except Exception as e:
561
  return jsonify({'success': False, 'error': str(e)}), 500
562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563
  @app.route('/api/predict_lab_report', methods=['POST'])
564
  def predict_lab_report():
565
  try:
566
  data = request.json or {}
567
  from lab_prediction import predict_lab_disease, FEATURES
568
  values = {k: (data.get(k) if data.get(k) != '' else None) for k in FEATURES}
 
 
 
569
  for k in list(values):
570
  v = values[k]
571
  if k != 'Gender' and v is not None:
 
560
  except Exception as e:
561
  return jsonify({'success': False, 'error': str(e)}), 500
562
 
563
+ @app.route('/api/lab_upload', methods=['POST'])
564
+ def lab_upload():
565
+ """Store an optional lab report file (record only, no OCR)."""
566
+ try:
567
+ if 'file' not in request.files:
568
+ return jsonify({'success': False, 'error': 'No file uploaded.'})
569
+ f = request.files['file']
570
+ if not f.filename:
571
+ return jsonify({'success': False, 'error': 'No file selected.'})
572
+ from utils.firebase_storage import save_upload
573
+ ref, _name = save_upload(f, "lab_reports", app.config['UPLOAD_FOLDER'])
574
+ if ref.startswith('http'):
575
+ url = ref
576
+ else:
577
+ url = '/' + ref.replace('\\', '/')
578
+ return jsonify({'success': True, 'url': url})
579
+ except Exception as e:
580
+ return jsonify({'success': False, 'error': str(e)}), 500
581
+
582
  @app.route('/api/predict_lab_report', methods=['POST'])
583
  def predict_lab_report():
584
  try:
585
  data = request.json or {}
586
  from lab_prediction import predict_lab_disease, FEATURES
587
  values = {k: (data.get(k) if data.get(k) != '' else None) for k in FEATURES}
588
+ missing = [k for k, v in values.items() if v is None]
589
+ if missing:
590
+ return jsonify({'success': False, 'error': 'Missing values: ' + ', '.join(missing)})
591
  for k in list(values):
592
  v = values[k]
593
  if k != 'Gender' and v is not None:
templates/doctor/lab_analysis.html CHANGED
@@ -4,11 +4,24 @@
4
  <div class="row mb-4">
5
  <div class="col-12">
6
  <h2>Lab Report Analysis</h2>
7
- <p class="text-muted">Enter lab values below, then predict disease (Groq AI).</p>
8
  </div>
9
  </div>
10
  <div class="row justify-content-center">
11
  <div class="col-12 col-xl-10">
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  <form id="labForm">
13
  <div class="card shadow-sm mb-4">
14
  <div class="card-body p-4">
@@ -43,7 +56,7 @@
43
  <div class="alert alert-info mb-3">
44
  <div class="small text-muted text-uppercase fw-bold">Predicted Disease</div>
45
  <h4 id="lab-disease" class="mb-1"></h4>
46
- <small class="text-muted">Confidence: <span id="lab-confidence"></span>% (<span id="lab-model"></span>)</small>
47
  </div>
48
  <h6>Top predictions</h6>
49
  <ul id="lab-top3" class="list-group mb-0"></ul>
@@ -56,9 +69,37 @@
56
  <script>
57
  const LAB_FIELDS = ["Gender","Age","Hemoglobin","RBC","WBC","AST","ALT","Cholesterol","Spirometry","Creatinine","Glucose","Lipase","Troponin"];
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  document.getElementById('btn-predict-lab').addEventListener('click', function () {
60
  const payload = {};
61
- LAB_FIELDS.forEach(f => { payload[f] = document.getElementById(f).value; });
 
 
 
 
 
 
 
 
 
62
  const btn = this; btn.disabled = true; btn.innerText = 'Predicting...';
63
  fetch('/api/predict_lab_report', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload) })
64
  .then(r => r.json())
@@ -68,7 +109,6 @@ document.getElementById('btn-predict-lab').addEventListener('click', function ()
68
  document.getElementById('lab-result').style.display = 'block';
69
  document.getElementById('lab-disease').innerText = data.disease;
70
  document.getElementById('lab-confidence').innerText = data.confidence;
71
- document.getElementById('lab-model').innerText = data.accuracy || '';
72
  const ul = document.getElementById('lab-top3'); ul.innerHTML = '';
73
  (data.top3 || []).forEach(t => {
74
  const li = document.createElement('li');
 
4
  <div class="row mb-4">
5
  <div class="col-12">
6
  <h2>Lab Report Analysis</h2>
7
+ <p class="text-muted">Attach the report for record (optional), enter lab values, then predict disease.</p>
8
  </div>
9
  </div>
10
  <div class="row justify-content-center">
11
  <div class="col-12 col-xl-10">
12
+ <div class="card shadow-sm mb-4">
13
+ <div class="card-body p-4">
14
+ <h5 class="card-title mb-3"><i data-lucide="file-image" class="me-2"></i>1. Attach Report Image (optional, for record)</h5>
15
+ <div class="d-flex gap-2 align-items-center flex-wrap">
16
+ <input type="file" id="lab-file" class="form-control" accept=".jpg,.jpeg,.png,image/*,.pdf" style="max-width: 400px;">
17
+ <button type="button" class="btn btn-secondary" id="btn-upload-lab">
18
+ <i data-lucide="upload" class="me-2"></i>Upload
19
+ </button>
20
+ <span class="text-muted small" id="upload-status"></span>
21
+ </div>
22
+ </div>
23
+ </div>
24
+
25
  <form id="labForm">
26
  <div class="card shadow-sm mb-4">
27
  <div class="card-body p-4">
 
56
  <div class="alert alert-info mb-3">
57
  <div class="small text-muted text-uppercase fw-bold">Predicted Disease</div>
58
  <h4 id="lab-disease" class="mb-1"></h4>
59
+ <small class="text-muted">Confidence: <span id="lab-confidence"></span>% · AI Analysis</small>
60
  </div>
61
  <h6>Top predictions</h6>
62
  <ul id="lab-top3" class="list-group mb-0"></ul>
 
69
  <script>
70
  const LAB_FIELDS = ["Gender","Age","Hemoglobin","RBC","WBC","AST","ALT","Cholesterol","Spirometry","Creatinine","Glucose","Lipase","Troponin"];
71
 
72
+ document.getElementById('btn-upload-lab').addEventListener('click', function () {
73
+ const fileInput = document.getElementById('lab-file');
74
+ const status = document.getElementById('upload-status');
75
+ if (!fileInput.files.length) { alert('Select a file first.'); return; }
76
+ const btn = this; btn.disabled = true; status.innerText = 'Uploading...';
77
+ const fd = new FormData(); fd.append('file', fileInput.files[0]);
78
+ fetch('/api/lab_upload', { method: 'POST', body: fd })
79
+ .then(r => r.json())
80
+ .then(data => {
81
+ btn.disabled = false;
82
+ if (data.success) {
83
+ status.innerHTML = 'Saved. <a href="' + data.url + '" target="_blank">View file</a>';
84
+ } else {
85
+ status.innerText = 'Upload failed: ' + data.error;
86
+ }
87
+ })
88
+ .catch(e => { btn.disabled = false; status.innerText = 'Error: ' + e; });
89
+ });
90
+
91
  document.getElementById('btn-predict-lab').addEventListener('click', function () {
92
  const payload = {};
93
+ const missing = [];
94
+ LAB_FIELDS.forEach(f => {
95
+ const v = document.getElementById(f).value.trim();
96
+ payload[f] = v;
97
+ if (!v) missing.push(f);
98
+ });
99
+ if (missing.length) {
100
+ alert('Please fill all lab values before predicting.\nMissing: ' + missing.join(', '));
101
+ return;
102
+ }
103
  const btn = this; btn.disabled = true; btn.innerText = 'Predicting...';
104
  fetch('/api/predict_lab_report', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload) })
105
  .then(r => r.json())
 
109
  document.getElementById('lab-result').style.display = 'block';
110
  document.getElementById('lab-disease').innerText = data.disease;
111
  document.getElementById('lab-confidence').innerText = data.confidence;
 
112
  const ul = document.getElementById('lab-top3'); ul.innerHTML = '';
113
  (data.top3 || []).forEach(t => {
114
  const li = document.createElement('li');