mgbam commited on
Commit
a055897
·
verified ·
1 Parent(s): 56b906a

Update image_pipeline.py

Browse files
Files changed (1) hide show
  1. image_pipeline.py +11 -10
image_pipeline.py CHANGED
@@ -1,16 +1,17 @@
1
- from transformers import AutoProcessor, AutoModelForVision2Seq
2
  from PIL import Image
3
- from config import HF_IMAGE_MODEL
 
4
 
5
- # Load Hugging Face image model and processor
6
- processor = AutoProcessor.from_pretrained(HF_IMAGE_MODEL)
7
- model = AutoModelForVision2Seq.from_pretrained(HF_IMAGE_MODEL)
8
 
9
  def analyze_medical_image(image_file):
10
  """
11
- Process and analyze a medical image to generate diagnostic insights.
12
  """
13
- image = Image.open(image_file).convert("RGB")
14
- inputs = processor(images=image, return_tensors="pt").to(model.device)
15
- outputs = model.generate(**inputs, max_length=256)
16
- return processor.batch_decode(outputs, skip_special_tokens=True)[0]
 
 
1
+ from huggingface_hub import InferenceApi
2
  from PIL import Image
3
+ import base64
4
+ from config import HF_IMAGE_MODEL, HF_TOKEN
5
 
6
+ # Initialize Hugging Face Inference API
7
+ inference = InferenceApi(repo_id=HF_IMAGE_MODEL, token=HF_TOKEN)
 
8
 
9
  def analyze_medical_image(image_file):
10
  """
11
+ Analyze a medical image using the Hugging Face Inference API.
12
  """
13
+ with open(image_file, "rb") as img:
14
+ base64_image = base64.b64encode(img.read()).decode("utf-8")
15
+
16
+ response = inference(inputs={"image": base64_image})
17
+ return response.get("generated_text", "No insights generated.")