gregorlied commited on
Commit
314581a
·
verified ·
1 Parent(s): 1d4e68a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -26
app.py CHANGED
@@ -13,12 +13,12 @@ hf_login(token=os.getenv("HF_TOKEN"))
13
 
14
  model_name = "gregorlied/Llama-3.2-1B-Instruct-Medical-Report-Summarization-FP32"
15
 
16
- # model = AutoModelForCausalLM.from_pretrained(
17
- # model_name,
18
- # device_map="auto",
19
- # attn_implementation='eager',
20
- # trust_remote_code=True,
21
- # )
22
 
23
  class Person(BaseModel):
24
  life_style: str
@@ -48,6 +48,8 @@ grammar_compiler = xgr.GrammarCompiler(tokenizer_info)
48
  compiled_grammar = grammar_compiler.compile_json_schema(Person)
49
  xgr_logits_processor = xgr.contrib.hf.LogitsProcessor(compiled_grammar)
50
 
 
 
51
  prompt = """You are a text extraction system for clinical reports.
52
  Please extract relevant clinical information from the report.
53
 
@@ -205,26 +207,21 @@ def summarize(
205
  enable_thinking=False, # only relevant for qwen
206
  )
207
 
208
- """
209
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
210
- generated_ids = model.generate(
211
- input_ids=model_inputs["input_ids"],
212
- attention_mask = model_inputs["attention_mask"],
213
- # num_beams=8,
214
- # top_p=0.9,
215
- # do_sample=True,
216
- # temperature=0.6,
217
- # min_new_tokens=50,
218
- max_new_tokens=2048,
219
- logits_processor=[xgr_logits_processor]
220
- )
221
- generated_ids = [
222
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
223
- ]
224
 
225
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
226
- """
227
- response = ['{"life_style": "N/A", "family_history": "N/A", "social_history": "N/A", "medical_surgical_history": "N/A", "signs_symptoms": "Fever; Chest pain; Cough; Progressive dyspnea; Tachypnea; Tachycardia; Decreased breath sounds in both lung bases; Crackles on the left", "comorbidities": "N/A", "diagnostic_techniques_procedures": "Chest X-ray; Echocardiography; Thoracentesis; Laboratory tests; Pleural fluid analysis; Urinary pneumococcal antigen test; Pleural fluid culture", "diagnosis": "Pneumonia; Pericardial effusion; S. pneumoniae infection", "laboratory_values": "White blood cell count: 11.78 \\u00d7 10^9 cells/L (84.3% neutrophils, 4.3% lymphocytes, 9.1% monocytes); Platelet count: 512 \\u00d7 10^9/L; Serum C-reactive protein: 31.27 mg/dL; Serum creatinine: 0.94 mg/dL; Serum sodium: 133 mEq/L; Serum potassium: 3.72 mEq/L; Pleural fluid pH: 7.16; Pleural fluid glucose: 4.5 mg/dL; Pleural fluid proteins: 49.1 g/L; Pleural fluid LDH: 1,385 U/L", "pathology": "N/A", "pharmacological_therapy": "Amoxicillin-clavulanate (2.2 g/8 h, i.v.); Levofloxacin (500 mg twice a day); Ibuprofen (800 mg/day)", "interventional_therapy": "Pericardiocentesis; Thoracentesis", "patient_outcome_assessment": "Nearly complete resolution of alterations on chest X-ray and CT scan", "age": "57 year", "gender": "Male"}']
228
 
229
  try:
230
  data = ast.literal_eval(response[0])
@@ -281,7 +278,7 @@ with gr.Blocks() as demo:
281
  lines=15,
282
  max_lines=15,
283
  placeholder="Paste your clinical report here...",
284
- value="A 57-year-old male presented with fever (38.9°C), chest pain, cough, and progressive dyspnea. The patient exhibited tachypnea (34 breaths/min) and tachycardia (134 bpm). Auscultation revealed decreased breath sounds in both lung bases, with crackles on the left. A chest X-ray revealed bilateral pleural opacities and enlargement of the cardiac silhouette ( A). Echocardiography showed moderate pericardial effusion affecting the entire cardiac silhouette. Pericardiocentesis yielded 250 mL of exudative fluid. A CT scan of the chest showed pneumonia in the left lower lobe, bilateral pleural effusion, and moderate pericardial effusion ( B). Thoracentesis was performed and yielded 1,050 mL of exudative fluid. Laboratory tests yielded the following data: white blood cell count, 11.78 × 109 cells/L (84.3% neutrophils, 4.3% lymphocytes, and 9.1% monocytes); platelet count, 512 × 109/L; serum C-reactive protein, 31.27 mg/dL; serum creatinine, 0.94 mg/dL; serum sodium, 133 mEq/L; and serum potassium, 3.72 mEq/L. Examination of the pleural fluid showed a pH of 7.16, a glucose level of 4.5 mg/dL, proteins at 49.1 g/L, and an LDH content of 1,385 U/L. A urinary pneumococcal antigen test was positive. Pleural fluid culture was positive for S. pneumoniae. The patient was treated for four weeks with amoxicillin-clavulanate (2.2 g/8 h, i.v.) plus levofloxacin (500 mg twice a day), together with a nonsteroidal anti-inflammatory drug (ibuprofen, 800 mg/day), after which there was nearly complete resolution of the alterations seen on the chest X-ray and CT scan.",
285
  )
286
 
287
  with gr.Accordion("Advanced Settings", open=False):
 
13
 
14
  model_name = "gregorlied/Llama-3.2-1B-Instruct-Medical-Report-Summarization-FP32"
15
 
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ model_name,
18
+ device_map="auto",
19
+ attn_implementation='eager',
20
+ trust_remote_code=True,
21
+ )
22
 
23
  class Person(BaseModel):
24
  life_style: str
 
48
  compiled_grammar = grammar_compiler.compile_json_schema(Person)
49
  xgr_logits_processor = xgr.contrib.hf.LogitsProcessor(compiled_grammar)
50
 
51
+ default_value = "A 57-year-old male presented with fever (38.9°C), chest pain, cough, and progressive dyspnea. The patient exhibited tachypnea (34 breaths/min) and tachycardia (134 bpm). Auscultation revealed decreased breath sounds in both lung bases, with crackles on the left. A chest X-ray revealed bilateral pleural opacities and enlargement of the cardiac silhouette ( A). Echocardiography showed moderate pericardial effusion affecting the entire cardiac silhouette. Pericardiocentesis yielded 250 mL of exudative fluid. A CT scan of the chest showed pneumonia in the left lower lobe, bilateral pleural effusion, and moderate pericardial effusion ( B). Thoracentesis was performed and yielded 1,050 mL of exudative fluid. Laboratory tests yielded the following data: white blood cell count, 11.78 × 109 cells/L (84.3% neutrophils, 4.3% lymphocytes, and 9.1% monocytes); platelet count, 512 × 109/L; serum C-reactive protein, 31.27 mg/dL; serum creatinine, 0.94 mg/dL; serum sodium, 133 mEq/L; and serum potassium, 3.72 mEq/L. Examination of the pleural fluid showed a pH of 7.16, a glucose level of 4.5 mg/dL, proteins at 49.1 g/L, and an LDH content of 1,385 U/L. A urinary pneumococcal antigen test was positive. Pleural fluid culture was positive for S. pneumoniae. The patient was treated for four weeks with amoxicillin-clavulanate (2.2 g/8 h, i.v.) plus levofloxacin (500 mg twice a day), together with a nonsteroidal anti-inflammatory drug (ibuprofen, 800 mg/day), after which there was nearly complete resolution of the alterations seen on the chest X-ray and CT scan."
52
+
53
  prompt = """You are a text extraction system for clinical reports.
54
  Please extract relevant clinical information from the report.
55
 
 
207
  enable_thinking=False, # only relevant for qwen
208
  )
209
 
210
+ if text == default_value:
211
+ response = ['{"life_style": "N/A", "family_history": "N/A", "social_history": "N/A", "medical_surgical_history": "N/A", "signs_symptoms": "Fever; Chest pain; Cough; Progressive dyspnea; Tachypnea; Tachycardia; Decreased breath sounds in both lung bases; Crackles on the left", "comorbidities": "N/A", "diagnostic_techniques_procedures": "Chest X-ray; Echocardiography; Thoracentesis; Laboratory tests; Pleural fluid analysis; Urinary pneumococcal antigen test; Pleural fluid culture", "diagnosis": "Pneumonia; Pericardial effusion; S. pneumoniae infection", "laboratory_values": "White blood cell count: 11.78 \\u00d7 10^9 cells/L (84.3% neutrophils, 4.3% lymphocytes, 9.1% monocytes); Platelet count: 512 \\u00d7 10^9/L; Serum C-reactive protein: 31.27 mg/dL; Serum creatinine: 0.94 mg/dL; Serum sodium: 133 mEq/L; Serum potassium: 3.72 mEq/L; Pleural fluid pH: 7.16; Pleural fluid glucose: 4.5 mg/dL; Pleural fluid proteins: 49.1 g/L; Pleural fluid LDH: 1,385 U/L", "pathology": "N/A", "pharmacological_therapy": "Amoxicillin-clavulanate (2.2 g/8 h, i.v.); Levofloxacin (500 mg twice a day); Ibuprofen (800 mg/day)", "interventional_therapy": "Pericardiocentesis; Thoracentesis", "patient_outcome_assessment": "Nearly complete resolution of alterations on chest X-ray and CT scan", "age": "57 year", "gender": "Male"}']
212
+ else:
213
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
214
+ generated_ids = model.generate(
215
+ input_ids=model_inputs["input_ids"],
216
+ attention_mask = model_inputs["attention_mask"],
217
+ max_new_tokens=2048,
218
+ logits_processor=[xgr_logits_processor]
219
+ )
220
+ generated_ids = [
221
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
222
+ ]
 
 
 
223
 
224
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True
 
 
225
 
226
  try:
227
  data = ast.literal_eval(response[0])
 
278
  lines=15,
279
  max_lines=15,
280
  placeholder="Paste your clinical report here...",
281
+ value=default_value,
282
  )
283
 
284
  with gr.Accordion("Advanced Settings", open=False):