File size: 2,747 Bytes
88e550b
128a4c1
 
80200b5
128a4c1
 
 
 
 
 
 
 
 
 
 
 
 
69dfe66
88e550b
7369efb
 
69dfe66
 
 
 
 
 
128a4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7369efb
 
 
128a4c1
 
69dfe66
0f27d7b
 
 
69dfe66
 
 
 
128a4c1
 
 
 
 
 
 
 
 
 
7369efb
 
 
128a4c1
 
 
 
dc1d715
 
 
 
 
 
 
 
 
 
 
 
 
 
7369efb
dc1d715
7369efb
 
dc1d715
 
128a4c1
80200b5
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit as st
import os
import torch
from pathlib import Path
from transformers import AutoTokenizer
from jax import numpy as jnp
import json
import requests
import zipfile
import io
import natsort
from PIL import Image as PilImage

from torchvision import datasets, transforms
from torchvision.transforms import CenterCrop, Normalize, Resize, ToTensor
from torchvision.transforms.functional import InterpolationMode
from tqdm import tqdm
from modeling_hybrid_clip import FlaxHybridCLIP

import utils


@st.cache
def get_model():
    return FlaxHybridCLIP.from_pretrained("clip-italian/clip-italian")


@st.cache
def download_images():
    # from sentence_transformers import SentenceTransformer, util
    img_folder = "photos/"
    if not os.path.exists(img_folder) or len(os.listdir(img_folder)) == 0:
        os.makedirs(img_folder, exist_ok=True)

        photo_filename = "unsplash-25k-photos.zip"
        if not os.path.exists(photo_filename):  # Download dataset if does not exist
            print(f"Downloading {photo_filename}...")
            r = requests.get("http://sbert.net/datasets/" + photo_filename, stream=True)
            z = zipfile.ZipFile(io.BytesIO(r.content))
            print("Extracting the dataset...")
            z.extractall(path=img_folder)
    print("Done.")


@st.cache()
def get_image_features():
    return jnp.load("static/features/features.npy")


"""

# 👋 Ciao!

# CLIP Italian Demo (Flax Community Week)
"""


os.environ["TOKENIZERS_PARALLELISM"] = "false"


query = st.text_input("Insert a query text")
if query:

    with st.spinner("Computing in progress..."):
        model = get_model()
        download_images()

        image_features = get_image_features()

        model = get_model()
        tokenizer = AutoTokenizer.from_pretrained(
            "dbmdz/bert-base-italian-xxl-uncased", cache_dir=None, use_fast=True
        )

        image_size = model.config.vision_config.image_size

        val_preprocess = transforms.Compose(
            [
                Resize([image_size], interpolation=InterpolationMode.BICUBIC),
                CenterCrop(image_size),
                ToTensor(),
                Normalize(
                    (0.48145466, 0.4578275, 0.40821073),
                    (0.26862954, 0.26130258, 0.27577711),
                ),
            ]
        )

        dataset = utils.CustomDataSet("photos/", transform=val_preprocess)

        image_paths = utils.find_image(
            query, model, dataset, tokenizer, image_features, n=2
        )

    st.image(image_paths)


def read_markdown_file(markdown_file):
    return Path(markdown_file).read_text()

intro_markdown = read_markdown_file("readme.md")
st.markdown(intro_markdown, unsafe_allow_html=True)