-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
56 lines (38 loc) · 1.38 KB
/
server.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
import os
from flask import Flask, jsonify, request
from compare import compare_mfcc
SERVER_PORT = 7777
SERVER_ROOT_DIR = os.path.dirname(__file__)
WEBSITE_STATIC_FOLDER = "website/static/"
ALLOWED_EXTENSIONS = set(["wav", "mp3"])
UPLOAD_FOLDER = "uploads/"
UPLOAD_FILENAME = "sound.wav"
TARGET_FILENAME = "target.wav"
app = Flask(
__name__,
static_url_path="",
static_folder=WEBSITE_STATIC_FOLDER,
)
@app.route("/api")
def api():
return {"message": "Hello from the API!"}
@app.route("/api/upload-audio", methods=["POST"])
def api_upload_audio():
if "file" not in request.files:
return jsonify({"message": "No file part"}), 400
file = request.files["file"]
filename = file.filename
if file.filename == "":
return jsonify({"message": "No selected file"}), 400
if not allowedFile(filename):
return jsonify({"message": "File type not allowed", "filename": filename}), 400
print("Processing:", filename)
file.save(os.path.join(SERVER_ROOT_DIR, UPLOAD_FOLDER, UPLOAD_FILENAME))
message = compare_mfcc(
UPLOAD_FOLDER + UPLOAD_FILENAME, UPLOAD_FOLDER + TARGET_FILENAME
)
return jsonify({"message": message, "filename": filename})
def allowedFile(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.run(port=SERVER_PORT, debug=True)