Update image_pipeline.py
Browse files- image_pipeline.py +17 -19
image_pipeline.py
CHANGED
@@ -1,23 +1,21 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
"""
|
11 |
-
return pipeline("image-to-text", model=IMAGE_MODEL_NAME)
|
12 |
|
13 |
-
def
|
14 |
"""
|
15 |
-
|
|
|
16 |
"""
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
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]
|
|