Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,39 @@
|
|
1 |
-
import
|
2 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, render_template, jsonify
|
2 |
+
import os
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# Set up the folder where audio files will be saved
|
7 |
+
UPLOAD_FOLDER = 'uploads/'
|
8 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
9 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
10 |
+
|
11 |
+
# Allowed file extensions
|
12 |
+
ALLOWED_EXTENSIONS = {'wav', 'mp3', 'ogg'}
|
13 |
+
|
14 |
+
# Check if the file is allowed
|
15 |
+
def allowed_file(filename):
|
16 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
17 |
+
|
18 |
+
@app.route('/')
|
19 |
+
def index():
|
20 |
+
return render_template('index.html')
|
21 |
+
|
22 |
+
@app.route('/upload', methods=['POST'])
|
23 |
+
def upload_audio():
|
24 |
+
if 'file' not in request.files:
|
25 |
+
return jsonify({"message": "No file part"}), 400
|
26 |
+
|
27 |
+
file = request.files['file']
|
28 |
+
if file.filename == '':
|
29 |
+
return jsonify({"message": "No selected file"}), 400
|
30 |
+
|
31 |
+
if file and allowed_file(file.filename):
|
32 |
+
filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
33 |
+
file.save(filename)
|
34 |
+
return jsonify({"message": "File uploaded successfully", "filename": file.filename}), 200
|
35 |
+
else:
|
36 |
+
return jsonify({"message": "Invalid file type. Only WAV, MP3, and OGG are allowed."}), 400
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
app.run(debug=True)
|