Thesis / app.py
Haus0226's picture
Update app.py
ef17006 verified
import streamlit as st
from transformers import GitProcessor, GitForCausalLM, set_seed
import tifffile as tiff
import numpy as np
from PIL import Image
# Set random seed for reproducibility
set_seed(226)
# Load the model and processor
model = GitForCausalLM.from_pretrained("microsoft/git-base").eval()
processor = GitProcessor.from_pretrained("microsoft/git-base")
# Streamlit app title
st.title("What is hidden?")
# File uploader to accept multiple files
uploaded_files = st.file_uploader("Upload your images", accept_multiple_files=True)
# Check if files were uploaded
if uploaded_files:
# Iterate over pairs of images to display side by side
for i in range(0, len(uploaded_files), 2):
# Create columns for displaying two images and captions side by side
col1, col2 = st.columns(2)
# Process the first image
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)
# Process the second image, if available
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)