mnavaidd's picture
fix rouge score issue
a7ac376
import transformers
from transformers import BlipProcessor, BlipForImageTextRetrieval,BlipForConditionalGeneration, AutoProcessor
from transformers import AutoModelForCausalLM
import torch
import gradio as gr
from PIL import Image
import evaluate
model_path = "model"
processor = AutoProcessor.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
# Evaluation
bleu = evaluate.load("bleu")
rouge = evaluate.load('rouge')
references = [
["chest X-ray, which confirmed the position of guidewire, extending from the right internal jugular vein up to inferior vena cava",
"chest X-ray findings. Chest radiograph revealed an engorged pulmonary trunk with an abrupt cutoff of pulmonary vascularity in the distal portions bilaterally, indicative of the Westermark sign (arrows).",
"chest X-ray, PA, showing the position of the gun nails",
"chest x-ray showing right-sided pneumothorax.",
"chest X-ray on the day of admission showing diffuse bilateral haziness and air bronchogram. Post tapingchest X-ray shows multiple irregular pleural masses",
"chest X-ray performed after transplantation. Two drains inserted in right pleuralcavity andcatheter in rightcarotid internal vein are visible",
"chest X-ray ofcase 2 showing dextrocardia, the gastric bubble on the right side and the liver on the left side",
"chest X-ray showingcomplete resolution of symptoms at the end of anti-biotic therapy.",
"chest X-ray shows opacity at the lower part of the left hemithorax.",
"chest X-ray showing a 4.3cm right upper lobe lung mass.",
"chest x - ray showing a large right pleural effusion.",
"chest x - ray showing bilateral diffuse bilateral interstitial opacities",
"chest x - ray showing a large right pleural effusion.",
"chest X-ray of the 6 Y/O girl after the incident. Massive pleural effusion and air fluid level; foreign body in the rightchest at the anatomical site of the esophagus and radiological signs of perforation.",
"chest X-ray showing bilateral diffuse high density micro-nodular opacities.",
"chest X-ray revealed multiple bilateral round to oval, nodular opacities of homogeneous density, ranging in diameter from 0.5 to 2.0cm suggestive ofcannon ball opacities",
"chest x-ray revealed a voluminous opacity of the right upper pulmonary lobe Plainchest x-ray of a sheep during VV ECMO. Note the visualisation of only the accesscannula within the inferior venacava.",
"chest X-ray showing bilateral hilar lymphadenopathy together with lower bilateral interstitial lung densities.",
"chest X-ray of patient one showing fracture of the rightclavicle, third and fourth ribs with hemopneumothorax and left hemothorax",
"chest X-rays after the operation show the right diaphragm to be fixed in the normal location.",
"chest X-ray taken after tube thoracostomies were inserted. Note multiple rib fractures, subcutaneous emphysema, multiple lung opacities, particularly on the right,corresponding to sites of lungcontusion and residual pneumothorax on the left side.",
]
]
# Define the prediction function
def generate_caption(image):
# Process the image
image = Image.fromarray(image)
#inputs = tokenizer(image, return_tensors="pt")
inputs = processor(images=image, return_tensors="pt")#.to(device)
pixel_values = inputs.pixel_values
# Generate caption
generated_ids = model.generate(pixel_values=pixel_values, max_length=100)
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
generated_caption = generated_caption.lower()
generated_caption_list = [generated_caption]
bleu_results = bleu.compute(predictions=generated_caption_list, references=references)
rouge_results = rouge.compute(predictions=generated_caption_list, references=references)
return generated_caption, bleu_results["bleu"], rouge_results["rouge1"]
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
seed =gr.Image()
with gr.Column():
caption = gr.Textbox(label="Generated Caption")
bleu_score = gr.Textbox(label="Generated BLEU Score")
rouge_score = gr.Textbox(label="Generated ROUGE Score") #rouge_results
btn = gr.Button("Generate")
btn.click(generate_caption, inputs=[seed], outputs=[caption, bleu_score, rouge_score])
# Launch
demo.launch()