File size: 2,059 Bytes
2a38579
 
 
 
 
 
 
 
b2be13f
2a38579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3392fba
2a38579
 
 
 
 
 
 
 
 
 
 
 
 
 
0f70127
2a38579
 
 
 
0f70127
 
 
 
 
 
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
import pinecone
from PIL import Image
import streamlit as st
from facenet import FacenetEmbedder
from jinja2 import Environment, FileSystemLoader


st.markdown("<h1 style='text-align: center;'>⚡️ Find Your Celebrity Match ⚡️</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center;'>Take a picture to find your match, learn how it works <a href='https://www.pinecone.io/docs/facial-similarity-search'>here</a>.</p>", unsafe_allow_html=True)

def local_css(file_name):
    st.markdown('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">', unsafe_allow_html=True)
    with open(file_name) as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

@st.experimental_singleton
def init_template():
    local_css("style.css")
    environment = Environment(loader=FileSystemLoader("templates/"))
    template = environment.get_template("card.html")
    return template

PINECONE_KEY = st.secrets["PINECONE_KEY"]

@st.experimental_singleton
def init_pinecone():
    pinecone.init(api_key=PINECONE_KEY, environment="us-west1-gcp")  # get a free api key from app.pinecone.io
    return pinecone.GRPCIndex("celebrity-match")


@st.experimental_singleton
def init_facenet():
    facenet = FacenetEmbedder()
    return facenet


template = init_template() 
index = init_pinecone()
facenet = init_facenet()

col1, col2, col3 = st.columns([0.2, 2, 0.2])

with col2: img_file_buffer = st.camera_input("")

if img_file_buffer is not None:
    img = Image.open(img_file_buffer)
    emb = facenet.encode([img])
    if emb:
        result = index.query(emb[0], top_k=3, include_metadata=True)
        cards = template.render(results=result["matches"])
        st.markdown(f"<div class='container-md' align='center'><div class='row'>{cards}</div></div>", unsafe_allow_html=True)
    else:
        with col2: st.error('Oops! No face was detected, try again.', icon="🚨")