import streamlit as st import torch from joblib import load from PIL import Image 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 = load("./pages/img2txt_v4.joblib") model.load_state_dict(torch.load("./pages/model_weights_i2t_fin.pt", map_location=torch.device('cpu'))) 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") button = st.button('Сгенерировать описание') if button: 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])