scdrand23 commited on
Commit
0c20836
·
1 Parent(s): 8ffbfbc

major change, used biomed_llama 7b

Browse files
Files changed (1) hide show
  1. app.py +60 -21
app.py CHANGED
@@ -17,6 +17,7 @@ import sys
17
  from pathlib import Path
18
  from huggingface_hub import login
19
  # from dotenv import load_dotenv
 
20
 
21
  # For Hugging Face Spaces, secrets are automatically loaded as environment variables
22
  token = os.getenv("HF_TOKEN")
@@ -188,8 +189,32 @@ def initialize_model():
188
  )
189
  return model
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  model = initialize_model()
 
193
 
194
  def update_example_prompts(modality):
195
  if modality in MODALITY_PROMPTS:
@@ -211,39 +236,53 @@ def process_image(image_path, text_prompts, modality):
211
  if not modality:
212
  raise ValueError("Please select a modality")
213
 
 
214
  image = read_rgb(image_path)
215
  text_prompts = [prompt.strip() for prompt in text_prompts.split(',')]
216
-
217
- # Run inference
218
  pred_masks = interactive_infer_image(model, Image.fromarray(image), text_prompts)
219
 
220
  # Prepare outputs
221
  results = []
222
- p_values = []
223
-
 
224
  for i, prompt in enumerate(text_prompts):
225
- # Calculate p-value for the selected modality
226
- print("PROMPT: ", prompt, flush=True)
227
  p_value = check_mask_stats(image, pred_masks[i] * 255, modality, prompt)
228
- p_values.append(f"P-value for '{prompt}' ({modality}): {p_value:.4f}")
229
-
230
- # Overlay predictions on the image
231
  overlay_image = image.copy()
232
- overlay_image[pred_masks[i] > 0.5] = [255, 0, 0] # Highlight predictions in red
233
  results.append(overlay_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
- return results, "\n".join(p_values)
236
-
237
- except ValueError as ve:
238
- # Handle validation errors
239
- return None, f"⚠️ Input Error: {str(ve)}"
240
- except torch.cuda.OutOfMemoryError:
241
- # Handle CUDA out of memory errors
242
- return None, "⚠️ Error: GPU memory exceeded. Please try with a smaller image."
243
  except Exception as e:
244
- # Handle all other errors
245
- error_msg = f"��️ An error occurred: {str(e)}"
246
- print(f"Error details: {str(e)}", flush=True) # For logging
247
  return None, error_msg
248
 
249
  # Define Gradio interface
 
17
  from pathlib import Path
18
  from huggingface_hub import login
19
  # from dotenv import load_dotenv
20
+ from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig
21
 
22
  # For Hugging Face Spaces, secrets are automatically loaded as environment variables
23
  token = os.getenv("HF_TOKEN")
 
189
  )
190
  return model
191
 
192
+ def initialize_llm():
193
+ bnb_config = BitsAndBytesConfig(
194
+ load_in_4bit=True,
195
+ bnb_4bit_quant_type="nf4",
196
+ bnb_4bit_use_double_quant=True,
197
+ bnb_4bit_compute_dtype=torch.float16
198
+ )
199
+
200
+ model = AutoModel.from_pretrained(
201
+ "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
202
+ quantization_config=bnb_config,
203
+ device_map="auto",
204
+ torch_dtype=torch.float16,
205
+ trust_remote_code=True,
206
+ attn_implementation="flash_attention_2"
207
+ )
208
+
209
+ tokenizer = AutoTokenizer.from_pretrained(
210
+ "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
211
+ trust_remote_code=True
212
+ )
213
+
214
+ return model, tokenizer
215
 
216
  model = initialize_model()
217
+ llm_model, llm_tokenizer = initialize_llm()
218
 
219
  def update_example_prompts(modality):
220
  if modality in MODALITY_PROMPTS:
 
236
  if not modality:
237
  raise ValueError("Please select a modality")
238
 
239
+ # Original BiomedParse processing
240
  image = read_rgb(image_path)
241
  text_prompts = [prompt.strip() for prompt in text_prompts.split(',')]
 
 
242
  pred_masks = interactive_infer_image(model, Image.fromarray(image), text_prompts)
243
 
244
  # Prepare outputs
245
  results = []
246
+ analysis_results = []
247
+
248
+ # Process with BiomedParse
249
  for i, prompt in enumerate(text_prompts):
 
 
250
  p_value = check_mask_stats(image, pred_masks[i] * 255, modality, prompt)
251
+ analysis_results.append(f"P-value for '{prompt}' ({modality}): {p_value:.4f}")
252
+
 
253
  overlay_image = image.copy()
254
+ overlay_image[pred_masks[i] > 0.5] = [255, 0, 0]
255
  results.append(overlay_image)
256
+
257
+ # Process with LLM
258
+ pil_image = Image.fromarray(image)
259
+ question = 'Give the modality, organ, analysis, abnormalities (if any), treatment (if abnormalities are present)?'
260
+ msgs = [{'role': 'user', 'content': [pil_image, question]}]
261
+
262
+ llm_response = ""
263
+ for new_text in llm_model.chat(
264
+ image=pil_image,
265
+ msgs=msgs,
266
+ tokenizer=llm_tokenizer,
267
+ sampling=True,
268
+ temperature=0.95,
269
+ stream=True
270
+ ):
271
+ llm_response += new_text
272
+
273
+ # Combine both analyses
274
+ combined_analysis = "\n\n".join([
275
+ "BiomedParse Analysis:",
276
+ "\n".join(analysis_results),
277
+ "\nLLM Analysis:",
278
+ llm_response
279
+ ])
280
+
281
+ return results, combined_analysis
282
 
 
 
 
 
 
 
 
 
283
  except Exception as e:
284
+ error_msg = f"⚠️ An error occurred: {str(e)}"
285
+ print(f"Error details: {str(e)}", flush=True)
 
286
  return None, error_msg
287
 
288
  # Define Gradio interface