-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
45 lines (31 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
import os
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from flask_cors import CORS
from main import Infer
# normal backend endpoint that allow us to create a folder that saves the audio file coming from the frontend to further process and test it
app = Flask(__name__)
CORS(app)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'wav', 'mp3', 'm4a','ogg'} # Add allowed audio file extensions
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/predict_gender', methods=['POST'])
def predict_gender_route():
print(request.files)
if 'audio' not in request.files:
return jsonify({'error': 'No file part'})
audio_file = request.files['audio']
if audio_file.filename == '':
return jsonify({'error': 'No selected file'})
if audio_file and allowed_file(audio_file.filename):
filename = secure_filename(audio_file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
audio_file.save(filepath)
result = Infer(filepath)
return jsonify({'gender': result})
return jsonify({'error': "unallaowed"})
if __name__ == '__main__':
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
app.run(debug=True)