Update app.py
Browse files
app.py
CHANGED
@@ -45,9 +45,9 @@ MEDICAL_KEYWORDS = {
|
|
45 |
'allergies', 'summary', 'impression', 'findings', 'recommendations',
|
46 |
'conclusion', 'history', 'examination', 'progress', 'discharge'
|
47 |
}
|
48 |
-
TOKENIZER = "cl100k_base"
|
49 |
-
MAX_MODEL_LEN =
|
50 |
-
|
51 |
MEDICAL_SECTION_HEADER = "=== MEDICAL SECTION ==="
|
52 |
|
53 |
def sanitize_utf8(text: str) -> str:
|
@@ -226,7 +226,7 @@ def format_final_report(analysis_results: List[str], filename: str) -> str:
|
|
226 |
|
227 |
return "\n".join(report)
|
228 |
|
229 |
-
def split_content_by_tokens(content: str, max_tokens: int =
|
230 |
"""Split content into chunks that fit within token limits"""
|
231 |
paragraphs = re.split(r"\n\s*\n", content)
|
232 |
chunks = []
|
@@ -292,40 +292,49 @@ def analyze_complete_document(content: str, filename: str, agent: TxAgent) -> st
|
|
292 |
|
293 |
for i, chunk in enumerate(chunks):
|
294 |
try:
|
295 |
-
# Create
|
296 |
prompt = f"""
|
297 |
-
Analyze this
|
298 |
-
|
|
|
|
|
|
|
|
|
299 |
|
300 |
-
|
301 |
-
**Content:**
|
302 |
{chunk}
|
303 |
|
304 |
-
|
305 |
-
1. CRITICAL FINDINGS (urgent issues)
|
306 |
-
2. MISSED DIAGNOSES (with supporting evidence)
|
307 |
-
3. MEDICATION ISSUES (specific conflicts)
|
308 |
-
4. ASSESSMENT GAPS (missing evaluations)
|
309 |
-
5. FOLLOW-UP RECOMMENDATIONS (specific actions)
|
310 |
-
|
311 |
-
Be concise and evidence-based:
|
312 |
"""
|
313 |
-
#
|
314 |
prompt_tokens = count_tokens(prompt)
|
315 |
chunk_tokens = count_tokens(chunk)
|
316 |
|
317 |
-
if prompt_tokens + chunk_tokens > MAX_MODEL_LEN -
|
318 |
-
#
|
319 |
-
max_chunk_tokens = MAX_MODEL_LEN - prompt_tokens - 1024
|
320 |
adjusted_chunk = ""
|
321 |
tokens_used = 0
|
|
|
|
|
322 |
for para in re.split(r"\n\s*\n", chunk):
|
323 |
para_tokens = count_tokens(para)
|
324 |
-
if tokens_used + para_tokens <=
|
325 |
adjusted_chunk += "\n\n" + para
|
326 |
tokens_used += para_tokens
|
327 |
else:
|
328 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
chunk = adjusted_chunk.strip()
|
330 |
|
331 |
response = ""
|
@@ -333,7 +342,7 @@ Be concise and evidence-based:
|
|
333 |
message=prompt,
|
334 |
history=[],
|
335 |
temperature=0.1,
|
336 |
-
max_new_tokens=
|
337 |
max_token=MAX_MODEL_LEN,
|
338 |
call_agent=False,
|
339 |
conversation=[],
|
|
|
45 |
'allergies', 'summary', 'impression', 'findings', 'recommendations',
|
46 |
'conclusion', 'history', 'examination', 'progress', 'discharge'
|
47 |
}
|
48 |
+
TOKENIZER = "cl100k_base"
|
49 |
+
MAX_MODEL_LEN = 2048 # Matches your model's actual limit
|
50 |
+
TARGET_CHUNK_TOKENS = 1500 # Leaves room for prompt and response
|
51 |
MEDICAL_SECTION_HEADER = "=== MEDICAL SECTION ==="
|
52 |
|
53 |
def sanitize_utf8(text: str) -> str:
|
|
|
226 |
|
227 |
return "\n".join(report)
|
228 |
|
229 |
+
def split_content_by_tokens(content: str, max_tokens: int = TARGET_CHUNK_TOKENS) -> List[str]:
|
230 |
"""Split content into chunks that fit within token limits"""
|
231 |
paragraphs = re.split(r"\n\s*\n", content)
|
232 |
chunks = []
|
|
|
292 |
|
293 |
for i, chunk in enumerate(chunks):
|
294 |
try:
|
295 |
+
# Create minimal prompt to save tokens
|
296 |
prompt = f"""
|
297 |
+
Analyze this medical record section for:
|
298 |
+
1. Critical findings (urgent)
|
299 |
+
2. Missed diagnoses (with evidence)
|
300 |
+
3. Medication issues
|
301 |
+
4. Assessment gaps
|
302 |
+
5. Follow-up needs
|
303 |
|
304 |
+
Content:
|
|
|
305 |
{chunk}
|
306 |
|
307 |
+
Concise findings only:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
"""
|
309 |
+
# Verify we're within token limits
|
310 |
prompt_tokens = count_tokens(prompt)
|
311 |
chunk_tokens = count_tokens(chunk)
|
312 |
|
313 |
+
if prompt_tokens + chunk_tokens > MAX_MODEL_LEN - 512: # Leave room for response
|
314 |
+
# Find a natural truncation point
|
|
|
315 |
adjusted_chunk = ""
|
316 |
tokens_used = 0
|
317 |
+
max_content_tokens = MAX_MODEL_LEN - prompt_tokens - 512
|
318 |
+
|
319 |
for para in re.split(r"\n\s*\n", chunk):
|
320 |
para_tokens = count_tokens(para)
|
321 |
+
if tokens_used + para_tokens <= max_content_tokens:
|
322 |
adjusted_chunk += "\n\n" + para
|
323 |
tokens_used += para_tokens
|
324 |
else:
|
325 |
break
|
326 |
+
|
327 |
+
if not adjusted_chunk:
|
328 |
+
# If even one paragraph is too long, split sentences
|
329 |
+
sentences = re.split(r'(?<=[.!?])\s+', chunk)
|
330 |
+
for sent in sentences:
|
331 |
+
sent_tokens = count_tokens(sent)
|
332 |
+
if tokens_used + sent_tokens <= max_content_tokens:
|
333 |
+
adjusted_chunk += " " + sent
|
334 |
+
tokens_used += sent_tokens
|
335 |
+
else:
|
336 |
+
break
|
337 |
+
|
338 |
chunk = adjusted_chunk.strip()
|
339 |
|
340 |
response = ""
|
|
|
342 |
message=prompt,
|
343 |
history=[],
|
344 |
temperature=0.1,
|
345 |
+
max_new_tokens=512, # Keep responses concise
|
346 |
max_token=MAX_MODEL_LEN,
|
347 |
call_agent=False,
|
348 |
conversation=[],
|