File size: 1,136 Bytes
e06fe15
 
1a2364b
 
 
 
 
 
 
 
 
 
e06fe15
 
 
 
 
1a2364b
e06fe15
 
1a2364b
e06fe15
 
 
 
 
 
 
1a2364b
e06fe15
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from PIL import Image
import torch
from transformers import BlipForConditionalGeneration, AutoProcessor

# Load processor and model from Hugging Face Hub
processor = AutoProcessor.from_pretrained("daliavanilla/BLIP-Radiology-model")
model = BlipForConditionalGeneration.from_pretrained("daliavanilla/BLIP-Radiology-model")

# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Define the prediction function
def generate_caption(image):
    # Process the image
    image = Image.fromarray(image)
    inputs = processor(images=image, return_tensors="pt").to(device)

    # Generate caption
    generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
    generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]

    return generated_caption

# Define the Gradio interface
interface = gr.Interface(
    fn=generate_caption,
    inputs=gr.Image(type="numpy"),  # Ensure the image type is correctly handled by PIL
    outputs=gr.Textbox(),
    live=True
)

# Launch the Gradio interface
interface.launch()