Spaces:
Runtime error
Runtime error
Aadhithya
commited on
Commit
·
cea99fd
1
Parent(s):
a85f8ee
app.py
Browse files- app (1).py +69 -0
app (1).py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
import uuid # Import the UUID module
|
5 |
+
from werkzeug.utils import secure_filename
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Configuration for file uploads and output
|
10 |
+
UPLOAD_FOLDER = 'uploads'
|
11 |
+
OUTPUT_FOLDER = 'output_files'
|
12 |
+
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif', 'mp4', 'avi', 'mov'}
|
13 |
+
|
14 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
15 |
+
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
16 |
+
|
17 |
+
def allowed_file(filename):
|
18 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
19 |
+
|
20 |
+
@app.route('/', methods=['GET', 'POST'])
|
21 |
+
def index():
|
22 |
+
if request.method == 'POST':
|
23 |
+
# Handle file uploads and processing options
|
24 |
+
source_file = request.files['source']
|
25 |
+
target_file = request.files['target']
|
26 |
+
frame_processors = request.form.getlist('frame_processor')
|
27 |
+
|
28 |
+
if source_file and allowed_file(source_file.filename) and target_file and allowed_file(target_file.filename):
|
29 |
+
# Generate unique filenames with UUIDs for uploaded files
|
30 |
+
source_filename = str(uuid.uuid4()) + '_' + secure_filename(source_file.filename)
|
31 |
+
target_filename = str(uuid.uuid4()) + '_' + secure_filename(target_file.filename)
|
32 |
+
|
33 |
+
# Save uploaded files
|
34 |
+
source_path = os.path.join(app.config['UPLOAD_FOLDER'], source_filename)
|
35 |
+
target_path = os.path.join(app.config['UPLOAD_FOLDER'], target_filename)
|
36 |
+
source_file.save(source_path)
|
37 |
+
target_file.save(target_path)
|
38 |
+
|
39 |
+
# Determine output file name with UUID
|
40 |
+
output_filename = str(uuid.uuid4()) + '.jpg' # Default output format is JPEG
|
41 |
+
|
42 |
+
# Build and execute the processing command here
|
43 |
+
processing_command = ['python', 'run.py', '-s', source_path, '-t', target_path, '-o', os.path.join(app.config['OUTPUT_FOLDER'], output_filename)]
|
44 |
+
processing_command.extend(['--frame-processor', *frame_processors])
|
45 |
+
|
46 |
+
try:
|
47 |
+
# Execute the processing command using subprocess
|
48 |
+
subprocess.run(processing_command, check=True)
|
49 |
+
|
50 |
+
# Redirect to the output page
|
51 |
+
return redirect(url_for('output', filename=output_filename))
|
52 |
+
except subprocess.CalledProcessError:
|
53 |
+
return render_template('error.html')
|
54 |
+
|
55 |
+
return render_template('index.html')
|
56 |
+
|
57 |
+
@app.route('/output/<filename>')
|
58 |
+
def output(filename):
|
59 |
+
return render_template('output.html', filename=filename)
|
60 |
+
|
61 |
+
@app.route('/download/<filename>')
|
62 |
+
def download(filename):
|
63 |
+
return send_from_directory(app.config['OUTPUT_FOLDER'], filename, as_attachment=True)
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
67 |
+
os.makedirs(app.config['OUTPUT_FOLDER'], exist_ok=True)
|
68 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
69 |
+
|