mgbam commited on
Commit
4753f3a
·
verified ·
1 Parent(s): ff77b73

Update image_pipeline.py

Browse files
Files changed (1) hide show
  1. image_pipeline.py +17 -19
image_pipeline.py CHANGED
@@ -1,23 +1,21 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
- from config import IMAGE_MODEL_NAME
4
 
5
- @st.cache_resource
6
- def load_image_model():
7
- """
8
- Loads an image captioning model recognized by the HF pipeline.
9
- Example: "nlpconnect/vit-gpt2-image-captioning" or "Salesforce/blip-image-captioning-base".
10
- """
11
- return pipeline("image-to-text", model=IMAGE_MODEL_NAME)
12
 
13
- def analyze_image(image_file, image_model):
14
  """
15
- Pass an uploaded image to the loaded pipeline for caption generation.
 
16
  """
17
- try:
18
- result = image_model(image_file)
19
- if isinstance(result, list) and len(result) > 0:
20
- return result[0].get("generated_text", "No caption.")
21
- return "No output from the model."
22
- except Exception as e:
23
- return f"Error analyzing image: {str(e)}"
 
1
+ from transformers import AutoProcessor, AutoModelForImageTextToText
2
+ from PIL import Image
3
+ import torch
4
 
5
+ from config import HF_IMAGE_MODEL
6
+
7
+ # Load the advanced vision-language model for medical images
8
+ processor = AutoProcessor.from_pretrained(HF_IMAGE_MODEL)
9
+ model = AutoModelForImageTextToText.from_pretrained(HF_IMAGE_MODEL)
 
 
10
 
11
+ def analyze_medical_image(image_file):
12
  """
13
+ Performs advanced medical image analysis.
14
+ Returns a text explanation or diagnostic insight from the model.
15
  """
16
+ image = Image.open(image_file).convert("RGB")
17
+ inputs = processor(images=image, return_tensors="pt").to(model.device)
18
+
19
+ # Inference
20
+ outputs = model.generate(**inputs, max_length=256)
21
+ return processor.batch_decode(outputs, skip_special_tokens=True)[0]