-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
49 lines (33 loc) · 1.17 KB
/
app.py
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
from fastapi import FastAPI
import pinecone
from process_input import encode_search_query, encode_image_query
from dotenv import load_dotenv
import streamlit as st
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
PINECONE_ENVIRONMENT = st.secrets["PINECONE_ENVIRONMENT"]
pinecone.init(api_key = PINECONE_API_KEY, environment = PINECONE_ENVIRONMENT)
PINECONE_INDEX = st.secrets["PINECONE_INDEX"]
index = pinecone.Index(PINECONE_INDEX)
app = FastAPI()
@app.get("/retrieve_closest_image")
def retrieve_closest_image(text: str):
encoded_text = encode_search_query(text)
closest_image_ids = index.query(
vector=encoded_text,
top_k=3,
include_values=False
)
closest_image_ids = [i['id'] for i in closest_image_ids['matches']]
return closest_image_ids
@app.get("/retrieve_similar_images")
def retrieve_similar_images():
image_vector = encode_image_query()
closest_image_ids = index.query(
vector=image_vector,
top_k=3,
metric='euclidean',
include_values=False
)
print("We have reached here")
closest_image_ids = [i['id'] for i in closest_image_ids['matches']]
return closest_image_ids