File size: 2,248 Bytes
5ccf23b
0c84448
5ccf23b
 
68b488c
 
5ccf23b
68b488c
ec76efb
 
 
 
 
 
 
 
 
 
5ccf23b
 
 
68b488c
ec76efb
a7146ae
 
71ac2a0
a7146ae
ec76efb
a7146ae
5ccf23b
a7146ae
68b488c
 
ec76efb
 
 
 
 
 
 
 
 
 
 
68b488c
ec76efb
 
 
68b488c
 
93f4d26
a7146ae
5ccf23b
ec76efb
 
 
 
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
import streamlit as st
import streamlit.components.v1
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import numpy as np
import pandas as pd

USE_DESCRIPTION_AS_LABEL = False
NUM_OTHER_TYPES = 4
MAX_FILES = 3

def _get_probs(image, text):
    inputs = processor(text=text, images=image, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image
    probs = logits_per_image.softmax(dim=1).detach().numpy().flatten()
    return probs

smaller_model = "openai/clip-vit-base-patch32"
model = CLIPModel.from_pretrained(smaller_model)
processor = CLIPProcessor.from_pretrained(smaller_model)
themes = pd.read_csv("wedding_themes.csv")
themes = themes.query("use=='Y'")
st.title("What's your wedding persona?")
st.subheader("")
filenames = st.file_uploader(f"Modern chic? Fall foliage? Upload up to {MAX_FILES} photos that have aesthetics that you love (photos don't have to be taken at a wedding).",
                             accept_multiple_files=True)
if len(filenames) > MAX_FILES:
    st.warning(f"Only {MAX_FILES} images are allowed!")

if 0 < len(filenames) <= MAX_FILES and st.button("Process"):
    labels = themes.theme.tolist()
    descriptions = themes.description.tolist()
    all_probs = []
    images = []
    for filename in filenames:
        image = Image.open(filename)
        if USE_DESCRIPTION_AS_LABEL:
            probs = _get_probs(image, descriptions)
        else:
            labels_with_wedding = [(x + " wedding").lower() for x in labels]
            probs = _get_probs(image, labels_with_wedding)
        all_probs.append(probs)
        images.append(image)

    # Calculate the total probability and write results.
    combined_probs = np.sum(all_probs, axis=0)
    sorted_args = np.argsort(combined_probs)[::-1][:(1+NUM_OTHER_TYPES)].tolist()
    top_labels = np.array(labels)[sorted_args].tolist()
    top_description = descriptions[sorted_args[0]]
    st.subheader(f"Your result: {top_labels[0]}")
    st.write(f"{top_description} \n\n Other personas you might like: {', '.join(top_labels[1:])}")

    # Display the uploaded images.
    cols = st.columns(len(filenames))
    for col, image in zip(cols, images):
        col.image(image)