-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
43 lines (31 loc) · 1.22 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
import os
from dotenv import load_dotenv
from flask import Flask, render_template, request
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import utils
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024
uri = f"mongodb+srv://{os.getenv('USER')}:{os.getenv('PASSWORD')}@cluster0.vultpjd.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri, server_api=ServerApi('1'))
@app.route('/', methods=["GET", "POST"])
def index():
"""
Handle requests for song retrieval by hummed query.
"""
if request.method == "POST":
db = client["MusicCatalog"]
collection = db["MusicCatalog"]
x, fs = utils.parse(request)
f0 = utils.pyin(x, fs)
note_nums = utils.hz_to_midi(f0)
candidates = utils.knn(collection, note_nums)
result = utils.dtw(candidates, note_nums, int(1.12 * len(note_nums)), len(note_nums) // 12)
return render_template('results.html', result=result)
else:
# Render index template for query submission
return render_template('index.html')
if __name__ == '__main__':
app.run()