gimmy256 commited on
Commit
2d639e3
·
verified ·
1 Parent(s): 981d4b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -8,6 +8,11 @@ import os, re, json
8
  import gradio as gr
9
  import google.generativeai as genai
10
 
 
 
 
 
 
11
  # Safe API configuration
12
  api_key = os.environ.get("GEMINI_API_KEY")
13
  if api_key:
@@ -37,6 +42,66 @@ SAMPLES = {
37
  "for 15 days",
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def prompt_correction(t):
41
  return f"""You are a medical transcript editor.
42
 
@@ -146,6 +211,12 @@ with gr.Blocks(
146
  theme=gr.themes.Soft(),
147
  ) as demo:
148
 
 
 
 
 
 
 
149
  gr.Markdown("""
150
  # ⚕ MedASR + Gemini Pipeline
151
  Phase 2 — Transcript → Clinical NLP (Correction, SOAP, Entities)
@@ -195,6 +266,11 @@ with gr.Blocks(
195
  inputs=transcript_box,
196
  outputs=[correction_out, soap_out, entities_out]
197
  )
 
 
 
 
 
198
 
199
  if __name__ == "__main__":
200
  demo.launch()
 
8
  import gradio as gr
9
  import google.generativeai as genai
10
 
11
+ import onnxruntime as ort
12
+ import numpy as np
13
+ import soundfile as sf
14
+
15
+
16
  # Safe API configuration
17
  api_key = os.environ.get("GEMINI_API_KEY")
18
  if api_key:
 
42
  "for 15 days",
43
  }
44
 
45
+
46
+ MODEL_PATH = "model_quantized.onnx" # adjust if different
47
+
48
+ session = None
49
+
50
+ def load_model():
51
+ global session
52
+ if session is None:
53
+ session = ort.InferenceSession(MODEL_PATH)
54
+ return session
55
+
56
+ def preprocess_audio(audio_path):
57
+ audio, sr = sf.read(audio_path)
58
+
59
+ if len(audio.shape) > 1:
60
+ audio = np.mean(audio, axis=1)
61
+
62
+ # Resample if needed
63
+ if sr != 16000:
64
+ from scipy.signal import resample
65
+ audio = resample(audio, int(len(audio) * 16000 / sr))
66
+
67
+ return audio.astype(np.float32)
68
+
69
+
70
+ def transcribe_audio(audio_path):
71
+ try:
72
+ session = load_model()
73
+ audio = preprocess_audio(audio_path)
74
+
75
+ inputs = {session.get_inputs()[0].name: audio}
76
+
77
+ outputs = session.run(None, inputs)
78
+
79
+ # This part depends on your model decoding
80
+ transcript = str(outputs[0])
81
+
82
+ return transcript
83
+
84
+ except Exception as e:
85
+ return f"ASR Error: {e}"
86
+
87
+
88
+ def run_full_pipeline(audio):
89
+ if not audio:
90
+ return "No audio", "", "", ""
91
+
92
+ transcript = transcribe_audio(audio)
93
+
94
+ if transcript.startswith("ASR Error"):
95
+ return transcript, "", "", ""
96
+
97
+ correction = run_correction(transcript)
98
+ soap = run_soap(transcript)
99
+ entities = run_entities(transcript)
100
+
101
+ return transcript, correction, soap, entities
102
+
103
+
104
+
105
  def prompt_correction(t):
106
  return f"""You are a medical transcript editor.
107
 
 
211
  theme=gr.themes.Soft(),
212
  ) as demo:
213
 
214
+
215
+ audio_input = gr.Audio(type="filepath", label="Upload Medical Audio")
216
+
217
+ run_audio_btn = gr.Button("🎤 Run Full Pipeline")
218
+ raw_transcript_out = gr.Textbox(label="Raw Transcript", lines=6)
219
+
220
  gr.Markdown("""
221
  # ⚕ MedASR + Gemini Pipeline
222
  Phase 2 — Transcript → Clinical NLP (Correction, SOAP, Entities)
 
266
  inputs=transcript_box,
267
  outputs=[correction_out, soap_out, entities_out]
268
  )
269
+ run_audio_btn.click(
270
+ fn=run_full_pipeline,
271
+ inputs=audio_input,
272
+ outputs=[raw_transcript_out, correction_out, soap_out, entities_out]
273
+ )
274
 
275
  if __name__ == "__main__":
276
  demo.launch()