imseldrith commited on
Commit
085d956
1 Parent(s): f944b4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -30
app.py CHANGED
@@ -1,55 +1,69 @@
1
- from flask import Flask, render_template, request, redirect, url_for
2
- import subprocess
3
  import os
 
 
4
  from werkzeug.utils import secure_filename
5
 
6
  app = Flask(__name__)
7
 
8
- # Configuration for file uploads
9
  UPLOAD_FOLDER = 'uploads'
10
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
11
  OUTPUT_FOLDER = 'output_files'
 
 
 
12
  app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
13
 
 
 
 
14
  @app.route('/', methods=['GET', 'POST'])
15
  def index():
16
  if request.method == 'POST':
17
- # Handle file uploads
18
  source_file = request.files['source']
19
  target_file = request.files['target']
20
- frame_processor = request.form.getlist('frame_processor')
21
-
22
- # Save uploaded files
23
- source_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(source_file.filename))
24
- target_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(target_file.filename))
25
- source_file.save(source_path)
26
- target_file.save(target_path)
27
-
28
- # Determine output file name
29
- output_filename = secure_filename('output.jpg') # Default output format is JPEG
30
 
31
- # Build the command based on the provided options
32
- command = ['python', 'run.py', '-s', source_path, '-t', target_path]
33
- command.extend(['-o', os.path.join(app.config['OUTPUT_FOLDER'], output_filename)])
34
- command.extend(['--frame-processor', *frame_processor])
35
-
36
- # Execute the command
37
- try:
38
- subprocess.run(command, check=True)
39
- return redirect(url_for('output', filename=output_filename))
40
- except subprocess.CalledProcessError:
41
- return render_template('error.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  return render_template('index.html')
44
 
45
- @app.route('/output_files/<filename>')
46
  def output(filename):
47
  return render_template('output.html', filename=filename)
48
- @app.route('/output_files/<filename>')
49
- def output_image(filename):
50
- return app.send_static_file(os.path.join(app.config['OUTPUT_FOLDER'], filename))
 
51
 
52
  if __name__ == '__main__':
53
  os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
54
  os.makedirs(app.config['OUTPUT_FOLDER'], exist_ok=True)
55
  app.run(host="0.0.0.0", port=7860, debug=True)
 
 
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
+