forked from gibsonPhillips/MQP-Song-Segmentation-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonServer.py
More file actions
56 lines (47 loc) · 1.7 KB
/
pythonServer.py
File metadata and controls
56 lines (47 loc) · 1.7 KB
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 signal
import sys
import os
from flask import Flask, request, jsonify
from waitress import serve
from librosaMusicSeg import runSegmentation
app = Flask(__name__)
SERVER_PORT = int(os.environ.get("PY_SERVER_PORT", "5001"))
# Create an endpoint
@app.route('/call-python', methods=['POST'])
def call_python():
# Get data from the request
# input_data = request.json.get('name', 'Guest')
# get arguments from algopy
data = request.get_json()
song_path = data['song']
algorithm = data['algorithm']
clusters = data['clusters']
print(f"Algorithm: {algorithm}")
# Call the Python function
if(algorithm == 1):
result = runSegmentation(song_path, "CQT", "Agglomerative", clusters)
elif(algorithm == 2):
result = runSegmentation(song_path, "Mel", "KMeans", clusters)
elif(algorithm == 3):
result = runSegmentation(song_path, "CQT", "GMM", clusters)
elif(algorithm == 4):
result = runSegmentation(song_path, "STFT", "KMeans", clusters)
else:
result = []
# Return the result as a JSON response
return jsonify([[int(a), float(b), float(c), int(d)] for a, b, c, d in result])
# Handle server shutdown
@app.route('/shutdown', methods=['POST'])
def shutdown():
if request.method == 'POST':
os.kill(os.getpid(), signal.SIGINT)
return 'Server shutting down...'
# Handle termination signals
def handle_exit_signal(signum, frame):
print("Shutting down Flask server...")
sys.exit(0)
signal.signal(signal.SIGINT, handle_exit_signal)
signal.signal(signal.SIGTERM, handle_exit_signal)
if __name__ == '__main__':
print("Starting Flask server with Waitress...")
serve(app, host='0.0.0.0', port=SERVER_PORT)