-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (36 loc) · 1.3 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
# app.py
import subprocess
import uuid
from flask import Flask, request, jsonify, send_file
import requests
from werkzeug.utils import secure_filename
import os
import ffmpeg
from scipy.spatial import distance
def create_app():
app = Flask(__name__, static_folder='uploads', static_url_path='/uploads')
app.config['UPLOAD_FOLDER'] = '/app/uploads/'
upload_folder = app.config['UPLOAD_FOLDER']
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
# Other setup code...
return app
app = create_app()
@app.route('/', methods=['GET'])
def homepage():
return "Homepage"
@app.route('/hello', methods=['GET'])
def hello():
return "Hello"
@app.route('/get_similar', methods=['POST'])
def cosine_similarity():
data = request.json
query_vector = data['query_vector']
vector_text_pairs = data['vectors']
# Extract embeddings and their corresponding texts
vectors = [pair['embeddings'] for pair in vector_text_pairs]
texts = [pair['text'] for pair in vector_text_pairs]
# Calculate cosine similarity for each vector
# Return the index of the most similar vector
most_similar_index = max(range(len(vectors)), key=lambda index: 1 - distance.cosine(query_vector, vectors[index]))
return jsonify({'most_similar_text': texts[most_similar_index]})