|
import streamlit as st |
|
from transformers import GitProcessor, GitForCausalLM, set_seed |
|
import tifffile as tiff |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
set_seed(226) |
|
|
|
|
|
model = GitForCausalLM.from_pretrained("microsoft/git-base").eval() |
|
processor = GitProcessor.from_pretrained("microsoft/git-base") |
|
|
|
|
|
st.title("What is hidden?") |
|
|
|
|
|
uploaded_files = st.file_uploader("Upload your images", accept_multiple_files=True) |
|
|
|
|
|
if uploaded_files: |
|
|
|
for i in range(0, len(uploaded_files), 2): |
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
if i < len(uploaded_files): |
|
file1 = uploaded_files[i] |
|
isTIFF1 = file1.name.endswith(".tiff") |
|
image1 = tiff.imread(file1) if isTIFF1 else Image.open(file1) |
|
image1 = np.array(image1) |
|
inputs1 = processor(images=image1, return_tensors="pt", do_rescale=not isTIFF1, do_resize=False) |
|
generated_ids1 = model.generate(**inputs1) |
|
caption1 = processor.batch_decode(generated_ids1, skip_special_tokens=True)[0] |
|
col1.image(image1, use_container_width=True) |
|
col1.header(caption1) |
|
|
|
|
|
if i + 1 < len(uploaded_files): |
|
file2 = uploaded_files[i + 1] |
|
isTIFF2 = file2.name.endswith(".tiff") |
|
image2 = tiff.imread(file2) if isTIFF2 else Image.open(file2) |
|
image2 = np.array(image2) |
|
inputs2 = processor(images=image2, return_tensors="pt", do_rescale=not isTIFF2, do_resize=False) |
|
generated_ids2 = model.generate(**inputs2) |
|
caption2 = processor.batch_decode(generated_ids2, skip_special_tokens=True)[0] |
|
col2.image(image2, use_container_width=True) |
|
col2.header(caption2) |
|
|