import streamlit as st import torch from joblib import load from PIL import Image from transformers import VisionEncoderDecoderModel device = 'cpu' # tokenizer = load("./pages/tokenizer_v3.joblib") # feature_extractor = load("./pages/feature_extractor_v3.joblib") tokenizer = load("tokenizer_v3.joblib") feature_extractor = load("feature_extractor_v3.joblib") model = VisionEncoderDecoderModel.from_pretrained("dumperize/movie-picture-captioning") # model = load("model_img2txt_v3.joblib") model.load_state_dict(torch.load("model_weights_i2t_fin.pt", map_location=torch.device('cpu'))) # model.eval() max_length = 512 min_length = 32 num_beams = 7 gen_kwargs = {"max_length": max_length, "min_length": min_length, "num_beams": num_beams} uploaded_file = st.file_uploader("Выберите изображение обложки книги в формате jpeg или jpg...", type=["jpg", "jpeg"]) if uploaded_file is not None: image = Image.open(uploaded_file) st.image(image, caption='Загруженное изображение') image = image.resize([224,224]) if image.mode != "RGB": image = image.convert(mode="RGB") pixel_values = feature_extractor(images=[image], return_tensors="pt").pixel_values pixel_values = pixel_values.to(device) output_ids = model.generate(pixel_values, **gen_kwargs) preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True) preds = [pred.strip() for pred in preds] st.write(preds[0]) # image = Image.open(image_path) # image = image.resize([224,224]) # if image.mode != "RGB": # image = image.convert(mode="RGB") # pixel_values = feature_extractor(images=[image], return_tensors="pt").pixel_values # pixel_values = pixel_values.to(device) # output_ids = model.generate(pixel_values, **gen_kwargs) # preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True) # print([pred.strip() for pred in preds])