File size: 1,622 Bytes
20013e4
 
1aad204
a660aaa
20013e4
1aad204
20013e4
 
 
a660aaa
1aad204
a660aaa
 
 
 
 
 
 
 
 
 
 
 
20013e4
 
 
 
 
 
359e714
20013e4
 
 
 
 
 
 
e369657
 
20013e4
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
import torch
import gradio as gr
from transformers import AutoProcessor, Blip2ForConditionalGeneration, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, PeftModel


# Load the processor
processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")

# Load the base model from the original repository
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
base_model = Blip2ForConditionalGeneration.from_pretrained(
    "ybelkada/blip2-opt-2.7b-fp16-sharded",
    device_map="auto",
    quantization_config=quantization_config
)

repo_id = "Prasi21/blip2-opt-2.7b-strep-throat-caption-adapters"

# Load the fine-tuned LoRA adapters from the Hugging Face Hub
model = PeftModel.from_pretrained(base_model, repo_id)


# Define the prediction function
def predict(image):
    # Preprocess the image
    inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)
    new_eos_token_id = 13
    with torch.no_grad():
        generated_ids = model.generate(**inputs, max_length=100,
                                      eos_token_id=new_eos_token_id)
    generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)
    return f"{generated_caption[0]}"

# Set up the Gradio interface
demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),  # Upload an image in PIL format
    outputs=gr.Textbox(),  # The output will be the generated caption
    title="Strep Throat Image Assessment",
    description="Upload an image of a throat and receive a medical assessment caption based on the model's output."
)

# Launch the Gradio app
demo.launch()