forked from ghodouss/CleanBeats.ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
67 lines (57 loc) · 2.6 KB
/
upload.py
File metadata and controls
67 lines (57 loc) · 2.6 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
57
58
59
60
61
62
63
64
65
66
67
import os
from flask import Flask, request, redirect, url_for, flash,send_file, render_template, send_from_directory
from werkzeug.utils import secure_filename
import requests
from audio_cleaner import clean_audio
#UPLOAD_FOLDER is where we will store the uploaded files
UPLOAD_FOLDER = "/home/kian/ML/SBHacks/explitive_files"
#allowed format
ALLOWED_EXTENSIONS = set(['mp3','wav'])
downloadFilePos=""
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY']="UPLOAD"
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)#use secure_filename function for safety
filename=file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
downloadFilePos=filename
new_audio_name = clean_audio(app.config['UPLOAD_FOLDER'] + "/" + filename, "em_lyrics.txt")
# return redirect(url_for('uploaded_file',filename=filename))
return render_template('index.html', display="block")
return render_template('index.html', display="none")
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory('//home/kian/ML/SBHacks/clean_files', filename, as_attachment=True)
# download from the Internet
@app.route('/uploads/test_download_Net/<filename>')
def download_file(url_addr):
url = url_addr # user provides url in query string
r = requests.get(url, allow_redirects=True)
# write to a file in the app's instance folder
# come up with a better file name
with app.open_instance_resource('downloaded_file', 'wb') as f:
f.write(r.content)
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
if request.method == "GET":
if os.path.isfile(os.path.join('/home/kian/ML/SBHacks/clean_files', filename)):
return send_from_directory('//home/kian/ML/SBHacks/clean_files', filename, as_attachment=True)
if __name__ == "__main__":
app.run()