-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
39 lines (28 loc) · 1.18 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
from flask import Flask, request, render_template
import openai
from openai.embeddings_utils import get_embedding, cosine_similarity
import pandas as pd
import numpy as np
import config
app = Flask(__name__)
openai.api_key = config.OPENAI_API_KEY
@app.route('/static/<path:filename>')
def serve_static(filename):
return app.send_static_file(filename)
@app.route('/')
def search_form():
return render_template('search_form.html')
@app.route('/search')
def search():
# Get the search query from the URL query string
query = request.args.get('query')
search_term_vector = get_embedding(query, engine="text-embedding-ada-002")
df = pd.read_csv('earnings-embeddings.csv')
df['embedding'] = df['embedding'].apply(eval).apply(np.array)
df["similarities"] = df['embedding'].apply(lambda x: cosine_similarity(x, search_term_vector))
sorted_by_similarity = df.sort_values("similarities", ascending=False).head(3)
results = sorted_by_similarity['text'].values.tolist()
# Render the search results template, passing in the search query and results
return render_template('search_results.html', query=query, results=results)
if __name__ == '__main__':
app.run()