File size: 1,463 Bytes
75fa944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83df8e8
75fa944
 
 
 
 
 
83df8e8
75fa944
 
 
 
 
 
 
1
2
3
4
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
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])