Spaces:
Sleeping
Sleeping
File size: 1,443 Bytes
7a03fd9 |
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 |
from flask import Flask, request, jsonify, send_from_directory, render_template
import os
import subprocess
app = Flask(__name__)
UPLOAD_FOLDER = '/tmp/uploads'
OUTPUT_FOLDER = '/tmp/outputs'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process_image', methods=['POST'])
def process_image():
if 'file' not in request.files:
return jsonify({'status': 'error', 'message': 'No file uploaded'})
file = request.files['file']
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
output_path = os.path.join(OUTPUT_FOLDER, 'output.png')
# Save the uploaded image
file.save(input_path)
try:
# Run NAFNet model using subprocess
subprocess.run([
'python', 'NAFNet/demo.py',
'-opt', 'NAFNet/options/test/REDS/NAFNet-width64.yml',
'--input_path', input_path,
'--output_path', output_path
], check=True)
return jsonify({'status': 'success', 'output_path': f'/outputs/output.png'})
except subprocess.CalledProcessError as e:
return jsonify({'status': 'error', 'message': f'Failed to run model: {str(e)}'})
@app.route('/outputs/<filename>')
def get_output_image(filename):
return send_from_directory(OUTPUT_FOLDER, filename)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
|