-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.py
60 lines (45 loc) · 1.6 KB
/
search.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
50
51
52
53
54
55
56
57
58
59
60
from sentence_transformers import SentenceTransformer
from flask import Flask, request, jsonify, json
import pinecone
import pandas as pd
# DB
products = pd.read_csv('flipkart_com-ecommerce_sample.csv')
products = products[:100]
#
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
# Set pinecone config
config = json.load(open("pinecone_config.json"))
# Set up Pinecone
pinecone.init(api_key=config['api_key'], environment=config['env'])
pinecone_index = pinecone.Index(config['index_name'])
# Setup the Flask App
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return "Server is working..."
# Search for products
@app.route('/search', methods=['GET'])
def search():
query = request.args.get('q')
# Encode the search query into a vector
query_vector = model.encode(query).tolist()
# Use Pinecone to search the index for similar documents
results = pinecone_index.query(query_vector, top_k=5)
# print('res', results)
# Return the top 10 most similar documents as JSON
response = []
for result in results.matches:
product = products.iloc[[result['id']]]
print(product)
response.append({
'name': product['product_name'].iloc[0],
'description': product['description'].iloc[0],
'price': product['retail_price'].iloc[0],
'image': product['image'].iloc[0],
'rating': product['product_rating'].iloc[0],
'brand': product['brand'].iloc[0],
'score': result.score
})
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)