File size: 1,231 Bytes
5a148a2
f3c6706
 
 
 
 
 
5a148a2
4c17e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4fa802
4c17e90
 
 
 
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
37
import gradio as gr
from huggingface_hub import login

import os
hf_token = os.getenv("HuggingFaceApiKey")
if hf_token:
    login(token=hf_token)

# Load Processor
from transformers import AutoProcessor
model_id  = "google/paligemma-3b-pt-224"
processor = AutoProcessor.from_pretrained(model_id)

from transformers import PaliGemmaForConditionalGeneration

model_id  = "dmusingu/PaliGemma-CXR"
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id)


def answer_question(image, question):
    # Process the image and question
    inputs = processor(images=image, text=question, return_tensors="pt", padding=True)
    # Perform the inference
    outputs  = model.generate(**inputs, max_new_tokens= 50)[0]
    outputs  = processor.decode(outputs[inputs["input_ids"].shape[1]:], skip_special_tokens = True)
    return outputs

# Define the Gradio interface
iface = gr.Interface(
    fn=answer_question,
    inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
    outputs=gr.Textbox(label="Answer"),
    title="PaliGemma-CXR: Report Generation, VQA, Object detection, Segmentation, Classification",
    description="Upload an image of a chest X-ray and ask a question and the model will answer."
)

iface.launch()