Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory, render_template
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
UPLOAD_FOLDER = '/tmp/uploads'
|
| 8 |
+
OUTPUT_FOLDER = '/tmp/outputs'
|
| 9 |
+
|
| 10 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 11 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
@app.route('/')
|
| 14 |
+
def index():
|
| 15 |
+
return render_template('index.html')
|
| 16 |
+
|
| 17 |
+
@app.route('/process_image', methods=['POST'])
|
| 18 |
+
def process_image():
|
| 19 |
+
if 'file' not in request.files:
|
| 20 |
+
return jsonify({'status': 'error', 'message': 'No file uploaded'})
|
| 21 |
+
|
| 22 |
+
file = request.files['file']
|
| 23 |
+
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 24 |
+
output_path = os.path.join(OUTPUT_FOLDER, 'output.png')
|
| 25 |
+
|
| 26 |
+
# Save the uploaded image
|
| 27 |
+
file.save(input_path)
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Run NAFNet model using subprocess
|
| 31 |
+
subprocess.run([
|
| 32 |
+
'python', 'NAFNet/demo.py',
|
| 33 |
+
'-opt', 'NAFNet/options/test/REDS/NAFNet-width64.yml',
|
| 34 |
+
'--input_path', input_path,
|
| 35 |
+
'--output_path', output_path
|
| 36 |
+
], check=True)
|
| 37 |
+
|
| 38 |
+
return jsonify({'status': 'success', 'output_path': f'/outputs/output.png'})
|
| 39 |
+
except subprocess.CalledProcessError as e:
|
| 40 |
+
return jsonify({'status': 'error', 'message': f'Failed to run model: {str(e)}'})
|
| 41 |
+
|
| 42 |
+
@app.route('/outputs/<filename>')
|
| 43 |
+
def get_output_image(filename):
|
| 44 |
+
return send_from_directory(OUTPUT_FOLDER, filename)
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
app.run(host='0.0.0.0', port=7860)
|