Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load model and
|
| 7 |
model_name = "hackergeek/radiology-image-captioning"
|
| 8 |
-
processor = BlipProcessor.from_pretrained(model_name)
|
| 9 |
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def generate_caption(image):
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
-
if isinstance(image,
|
| 16 |
-
image = Image.open(
|
| 17 |
-
|
| 18 |
image = image.convert("RGB")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
return caption
|
| 24 |
|
| 25 |
-
#
|
| 26 |
title = "Radiology Image Captioning"
|
| 27 |
-
description =
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=generate_caption,
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from transformers import BlipForConditionalGeneration, AutoTokenizer
|
| 6 |
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
model_name = "hackergeek/radiology-image-captioning"
|
|
|
|
| 9 |
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# Manual preprocessing
|
| 13 |
+
preprocess = transforms.Compose([
|
| 14 |
+
transforms.Resize((384, 384)), # BLIP models usually expect 384x384
|
| 15 |
+
transforms.ToTensor(),
|
| 16 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 17 |
+
std=[0.229, 0.224, 0.225]),
|
| 18 |
+
])
|
| 19 |
|
| 20 |
def generate_caption(image):
|
| 21 |
"""
|
| 22 |
+
Generate radiology caption for a PIL image.
|
| 23 |
"""
|
| 24 |
+
if not isinstance(image, Image.Image):
|
| 25 |
+
image = Image.open(image).convert("RGB")
|
| 26 |
+
else:
|
| 27 |
image = image.convert("RGB")
|
| 28 |
+
|
| 29 |
+
pixel_values = preprocess(image).unsqueeze(0) # add batch dimension
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model.generate(pixel_values=pixel_values)
|
| 32 |
+
caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
return caption
|
| 34 |
|
| 35 |
+
# Gradio Interface
|
| 36 |
title = "Radiology Image Captioning"
|
| 37 |
+
description = (
|
| 38 |
+
"Upload a radiology image (X-ray, CT, MRI) and get an automatic caption "
|
| 39 |
+
"generated by the `hackergeek/radiology-image-captioning` model."
|
| 40 |
+
)
|
| 41 |
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=generate_caption,
|