import pinecone from PIL import Image import streamlit as st from facenet import FacenetEmbedder from jinja2 import Environment, FileSystemLoader st.markdown("

⚡️ Find Your Celebrity Match ⚡️

", unsafe_allow_html=True) st.markdown("

Take a picture to find your match, learn how it works here.

", unsafe_allow_html=True) def local_css(file_name): st.markdown('', unsafe_allow_html=True) with open(file_name) as f: st.markdown(f'', 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"
{cards}
", unsafe_allow_html=True) else: with col2: st.error('Oops! No face was detected, try again.', icon="🚨")