Update src/streamlit_app.py
Browse files- src/streamlit_app.py +106 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,108 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os, torch, transformers
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from io import BytesIO
|
| 7 |
|
| 8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_caption_model():
|
| 12 |
+
# load medicap
|
| 13 |
+
ckpt_name = 'aehrc/medicap'
|
| 14 |
+
medicap = transformers.AutoModel.from_pretrained(ckpt_name, trust_remote_code=True)
|
| 15 |
+
medicap = medicap.to(device)
|
| 16 |
+
medicap.eval()
|
| 17 |
+
|
| 18 |
+
# transform image
|
| 19 |
+
image_processor = transformers.AutoFeatureExtractor.from_pretrained(ckpt_name)
|
| 20 |
+
medicap_transforms = transforms.Compose(
|
| 21 |
+
[
|
| 22 |
+
transforms.Resize(size=image_processor.size['shortest_edge']),
|
| 23 |
+
transforms.CenterCrop(size=[
|
| 24 |
+
image_processor.size['shortest_edge'],
|
| 25 |
+
image_processor.size['shortest_edge'],
|
| 26 |
+
]
|
| 27 |
+
),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
+
transforms.Normalize(
|
| 30 |
+
mean=image_processor.image_mean,
|
| 31 |
+
std=image_processor.image_std,
|
| 32 |
+
),
|
| 33 |
+
]
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# tokenizer
|
| 37 |
+
medicap_tokenizer = transformers.GPT2Tokenizer.from_pretrained(ckpt_name)
|
| 38 |
+
|
| 39 |
+
return medicap, medicap_transforms, medicap_tokenizer
|
| 40 |
+
|
| 41 |
+
def generate_image_caption(image, model, transformer, tokenizer):
|
| 42 |
+
image = transformer(image)
|
| 43 |
+
image = image.unsqueeze(0)
|
| 44 |
+
outputs = model.generate(
|
| 45 |
+
pixel_values=image.to(device),
|
| 46 |
+
bos_token_id=tokenizer.bos_token_id,
|
| 47 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 48 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 49 |
+
return_dict_in_generate=True,
|
| 50 |
+
use_cache=True,
|
| 51 |
+
max_length=256,
|
| 52 |
+
num_beams=4,
|
| 53 |
+
output_attentions=False
|
| 54 |
+
)
|
| 55 |
+
return tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
@st.cache_resource
|
| 58 |
+
def load_qa_model():
|
| 59 |
+
model_name = "microsoft/BioGPT-Large-PubMedQA"
|
| 60 |
+
biogpt_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 61 |
+
biogpt = AutoModelForCausalLM.from_pretrained(model_name)
|
| 62 |
+
biogpt = biogpt.to(device)
|
| 63 |
+
biogpt.eval()
|
| 64 |
+
|
| 65 |
+
return biogpt, biogpt_tokenizer
|
| 66 |
+
|
| 67 |
+
def generate_answer(description, question, model, tokenizer):
|
| 68 |
+
prompt = f"question: {question} context: {description}"
|
| 69 |
+
new_input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
|
| 70 |
+
input_ids = new_input_ids
|
| 71 |
+
|
| 72 |
+
generated_output = model.generate(
|
| 73 |
+
input_ids,
|
| 74 |
+
max_new_tokens=100, # Max new tokens for the bot's response
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
response = tokenizer.decode(generated_output[0], skip_special_tokens=True)
|
| 78 |
+
|
| 79 |
+
return response
|
| 80 |
+
|
| 81 |
+
st.set_page_config(page_title="Image Caption + QA", layout="centered")
|
| 82 |
+
st.title("🖼️ Caption-Based Question Answering")
|
| 83 |
+
|
| 84 |
+
uploaded_file = st.file_uploader("Choose Image", type = ["jpg", "jpeg", "png"])
|
| 85 |
+
if uploaded_file is not None:
|
| 86 |
+
img = Image.open(uploaded_file)
|
| 87 |
+
st.image(img)
|
| 88 |
+
|
| 89 |
+
# image description
|
| 90 |
+
medicap, medicap_transforms, medicap_tokenizer = load_caption_model()
|
| 91 |
+
caption = generate_image_caption(img, medicap, medicap_transforms, medicap_tokenizer)
|
| 92 |
+
|
| 93 |
+
st.subheader("📝 Generated Description")
|
| 94 |
+
st.info(caption)
|
| 95 |
+
|
| 96 |
+
# vqa
|
| 97 |
+
st.markdown("---")
|
| 98 |
+
|
| 99 |
+
st.subheader("❓ Ask a Question About the Image")
|
| 100 |
+
question = st.text_input("Type your question")
|
| 101 |
+
|
| 102 |
+
if question:
|
| 103 |
+
biogpt, biogpt_tokenizer = load_qa_model()
|
| 104 |
+
response = generate_answer(caption, question, biogpt, biogpt_tokenizer)
|
| 105 |
+
st.success(f"{response}")
|
| 106 |
+
|
| 107 |
+
else:
|
| 108 |
+
st.info("Please upload an image file.")
|