BaranN1204 commited on
Commit
35ba4e5
Β·
verified Β·
1 Parent(s): b2389f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -44
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import re
4
  import json
@@ -75,22 +74,21 @@ def apply_output_filter(text):
75
  def load_documents():
76
  docs = []
77
  for path in glob.glob("data/knowledge_base/*.txt"):
78
- with open(path, "r", encoding="utf-8", errors="ignore") as f:
79
- text = f.read()
80
-
81
- docs.append({
82
- "source": path,
83
- "text": text
84
- })
85
-
86
  return docs
87
 
88
  documents = load_documents()
89
  embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
90
  generator = pipeline(
91
  "text-generation",
92
- model="google/flan-t5-small",
 
93
  )
 
94
  all_chunks = []
95
  metadata = []
96
 
@@ -142,10 +140,17 @@ Convert the following doctor-patient conversation into a structured SOAP note fo
142
 
143
  Use the transcript as the main source of truth.
144
  Use the retrieved context only as supporting information.
145
- Do not repeat the transcript.
146
  Do not output instructions.
147
  Return only the final structured answer.
148
 
 
 
 
 
 
 
 
149
  Transcript:
150
  {transcript}
151
 
@@ -157,53 +162,108 @@ Required output format:
157
  SOAP NOTE
158
 
159
  Subjective:
160
- - Chief complaint:
161
- - History of present illness:
162
- - Associated symptoms:
163
- - Relevant negatives:
164
- - Medication/risk factors:
165
 
166
  Objective:
167
- - Exam findings: Not provided
168
- - Diagnostic information: Not provided unless mentioned
169
 
170
  Assessment:
171
- - Most likely diagnosis:
172
- - Differential diagnosis:
173
 
174
  Plan:
175
- - Medications:
176
- - Tests/Workup:
177
- - Lifestyle recommendations:
178
- - Follow-up / return precautions:
179
 
180
  ICD SUGGESTIONS:
181
- - CODE - Diagnosis
 
182
 
183
  CLINICAL REASONING:
184
- - 2 to 4 concise clinical sentences
185
  """.strip()
186
 
187
  return prompt
188
 
189
- def generate_llm_output(prompt, max_new_tokens=250):
190
- full_prompt = (
191
- "You are a careful gastroenterology clinical documentation assistant. "
192
- "Always return only a structured SOAP note with ICD suggestions. "
193
- "Do not repeat the transcript. Do not repeat instructions.\n\n"
194
- + prompt
195
- )
196
-
197
- output = generator(
198
- full_prompt,
199
- max_new_tokens=max_new_tokens,
200
- do_sample=False
201
- )
202
-
203
- text = output[0]["generated_text"].strip()
 
 
 
 
 
 
 
 
 
204
 
205
- if "SOAP NOTE" in text:
206
- text = text[text.index("SOAP NOTE"):].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
  return apply_output_filter(text)
209
 
@@ -324,4 +384,4 @@ demo = gr.Interface(
324
  description="Hosted NexScribe demo with retrieval, SOAP note generation, request monitoring, and runtime observability."
325
  )
326
 
327
- demo.launch()
 
 
1
  import os
2
  import re
3
  import json
 
74
  def load_documents():
75
  docs = []
76
  for path in glob.glob("data/knowledge_base/*.txt"):
77
+ with open(path, "r", encoding="utf-8") as f:
78
+ docs.append({
79
+ "source": path,
80
+ "text": f.read()
81
+ })
 
 
 
82
  return docs
83
 
84
  documents = load_documents()
85
  embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
86
  generator = pipeline(
87
  "text-generation",
88
+ model="microsoft/Phi-4-mini-instruct",
89
+ device_map="auto"
90
  )
91
+
92
  all_chunks = []
93
  metadata = []
94
 
 
140
 
141
  Use the transcript as the main source of truth.
142
  Use the retrieved context only as supporting information.
143
+ Do not repeat the transcript verbatim.
144
  Do not output instructions.
145
  Return only the final structured answer.
146
 
147
+ Critical formatting rules:
148
+ - Every bullet must contain actual patient-specific content after the colon.
149
+ - Never leave a bullet blank.
150
+ - Never output placeholders such as "CODE - Diagnosis" or "2 to 4 concise clinical sentences".
151
+ - If a detail is missing, write "Not stated".
152
+ - Use concise clinical language.
153
+
154
  Transcript:
155
  {transcript}
156
 
 
162
  SOAP NOTE
163
 
164
  Subjective:
165
+ - Chief complaint: <fill with patient-specific content>
166
+ - History of present illness: <fill with patient-specific content>
167
+ - Associated symptoms: <fill with patient-specific content>
168
+ - Relevant negatives: <fill with patient-specific content>
169
+ - Medication/risk factors: <fill with patient-specific content>
170
 
171
  Objective:
172
+ - Exam findings: <fill with patient-specific content or "Not provided" if not mentioned>
173
+ - Diagnostic information: <fill with patient-specific content or "Not provided" if not mentioned>
174
 
175
  Assessment:
176
+ - Most likely diagnosis: <fill with patient-specific content>
177
+ - Differential diagnosis: <fill with patient-specific content>
178
 
179
  Plan:
180
+ - Medications: <fill with patient-specific content>
181
+ - Tests/Workup: <fill with patient-specific content>
182
+ - Lifestyle recommendations: <fill with patient-specific content>
183
+ - Follow-up / return precautions: <fill with patient-specific content>
184
 
185
  ICD SUGGESTIONS:
186
+ - <ICD code> - <diagnosis>
187
+ - <ICD code> - <diagnosis or Not stated>
188
 
189
  CLINICAL REASONING:
190
+ - Write 2 to 4 concise clinical sentences based on the transcript and context.
191
  """.strip()
192
 
193
  return prompt
194
 
195
+ def generate_llm_output(prompt, max_new_tokens=450):
196
+ def _run_generation(prompt_text, token_limit=max_new_tokens):
197
+ messages = [
198
+ {
199
+ "role": "system",
200
+ "content": (
201
+ "You are a careful gastroenterology clinical documentation assistant. "
202
+ "Always return only a structured SOAP note with ICD suggestions. "
203
+ "Do not repeat the transcript. Do not repeat instructions. "
204
+ "Fill every section with patient-specific content. "
205
+ "If information is missing, say 'Not stated'."
206
+ )
207
+ },
208
+ {
209
+ "role": "user",
210
+ "content": prompt_text
211
+ }
212
+ ]
213
+
214
+ formatted_prompt = generator.tokenizer.apply_chat_template(
215
+ messages,
216
+ tokenize=False,
217
+ add_generation_prompt=True
218
+ )
219
 
220
+ text = generator(
221
+ formatted_prompt,
222
+ max_new_tokens=token_limit,
223
+ do_sample=False,
224
+ return_full_text=False
225
+ )[0]["generated_text"].strip()
226
+
227
+ if "SOAP NOTE" in text:
228
+ text = text[text.index("SOAP NOTE"):].strip()
229
+
230
+ return text
231
+
232
+ def _looks_incomplete(text):
233
+ if not text or not text.strip():
234
+ return True
235
+
236
+ placeholder_patterns = [
237
+ "- Chief complaint:",
238
+ "- History of present illness:",
239
+ "- Associated symptoms:",
240
+ "- Relevant negatives:",
241
+ "- Medication/risk factors:",
242
+ "- Most likely diagnosis:",
243
+ "- Differential diagnosis:",
244
+ "- Medications:",
245
+ "- Tests/Workup:",
246
+ "- Lifestyle recommendations:",
247
+ "- Follow-up / return precautions:",
248
+ "- CODE - Diagnosis",
249
+ "- 2 to 4 concise clinical sentences"
250
+ ]
251
+
252
+ return any(pattern in text for pattern in placeholder_patterns)
253
+
254
+ text = _run_generation(prompt)
255
+
256
+ if _looks_incomplete(text):
257
+ repair_prompt = (
258
+ prompt
259
+ + "\n\nThe previous draft was incomplete because it left placeholders blank. "
260
+ + "Rewrite the SOAP note now and replace every placeholder with actual content. "
261
+ + "Every bullet must contain text after the colon. "
262
+ + "Do not output placeholder text such as 'CODE - Diagnosis' or '2 to 4 concise clinical sentences'."
263
+ )
264
+ repaired = _run_generation(repair_prompt, token_limit=max(max_new_tokens, 550))
265
+ if repaired and not _looks_incomplete(repaired):
266
+ text = repaired
267
 
268
  return apply_output_filter(text)
269
 
 
384
  description="Hosted NexScribe demo with retrieval, SOAP note generation, request monitoring, and runtime observability."
385
  )
386
 
387
+ demo.launch()