Spaces:
Runtime error
Runtime error
File size: 1,817 Bytes
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 47 48 49 |
import torch
import gradio as gr
from transformers import Blip2ForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
from peft import PeftModel, PeftConfig
# Load the PEFT model configuration and quantization settings
peft_model_id = "Prasi21/blip2-opt-2.7b-strep-throat-caption-adapters3"
config = PeftConfig.from_pretrained(peft_model_id)
config.base_model_name_or_path = "Prasi21/blip2-opt-2.7b-strep-throat-caption-adapters3"
# Enable 8-bit quantization for more efficient loading
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
# Load the base model with quantization
model = Blip2ForConditionalGeneration.from_pretrained(
config.base_model_name_or_path,
quantization_config=quantization_config,
device_map="auto"
)
# Load the fine-tuned PEFT model
model = PeftModel.from_pretrained(model, peft_model_id)
# Load the processor
processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
# 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()
|