-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (118 loc) · 4.39 KB
/
app.py
File metadata and controls
140 lines (118 loc) · 4.39 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import os
import json
from processors.art import ArtProcessor
from processors.food import FoodProcessor
from processors.general import GeneralProcessor
import uuid
from werkzeug.utils import secure_filename
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__, static_folder='static')
CORS(app)
# Configure upload folder
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
# Initialize processors
processors = {
'art': ArtProcessor(),
'food': FoodProcessor(),
'general': GeneralProcessor()
}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def serve_static():
return send_from_directory('static', 'index.html')
@app.route('/api/health')
def health_check():
return jsonify({'status': 'healthy'})
@app.route('/api/processors')
def get_processors():
"""Get available processors and their configurations"""
return jsonify({
'processors': {
'art': {
'supported_styles': processors['art'].supported_styles,
'supported_formats': processors['art'].supported_formats
},
'food': {
'detection_modes': processors['food'].supported_detection_modes,
'metadata_types': processors['food'].supported_metadata
},
'general': {
'optimization_modes': processors['general'].optimization_modes,
'supported_formats': processors['general'].supported_formats
}
}
})
@app.route('/api/process', methods=['POST'])
def process_image():
"""Process an uploaded image"""
try:
# Check if a file was uploaded
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if not allowed_file(file.filename):
return jsonify({'error': 'File type not allowed'}), 400
# Get processing type and settings
processor_type = request.form.get('type', 'general')
settings = json.loads(request.form.get('settings', '{}'))
if processor_type not in processors:
return jsonify({'error': 'Invalid processor type'}), 400
# Create unique filename
filename = secure_filename(file.filename)
unique_filename = f"{uuid.uuid4()}_{filename}"
filepath = os.path.join(UPLOAD_FOLDER, unique_filename)
# Save the file
file.save(filepath)
logger.info(f"Saved file: {filepath}")
try:
# Process the image
processor = processors[processor_type]
result = processor.process(filepath, settings)
return jsonify({
'status': 'success',
'result': result
})
finally:
# Cleanup
try:
os.remove(filepath)
logger.info(f"Cleaned up file: {filepath}")
except Exception as e:
logger.error(f"Error cleaning up file {filepath}: {str(e)}")
except Exception as e:
logger.error(f"Processing error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/settings', methods=['GET'])
def get_settings():
"""Get available settings for all processors"""
return jsonify({
'art': {
'resolution': ['512x512', '1024x1024', '2048x2048'],
'style': processors['art'].supported_styles,
'format': processors['art'].supported_formats
},
'food': {
'resolution': ['512x512', '1024x1024'],
'detection': processors['food'].supported_detection_modes,
'metadata': processors['food'].supported_metadata
},
'general': {
'resolution': ['512x512', '1024x1024', '2048x2048'],
'optimization': processors['general'].optimization_modes,
'format': processors['general'].supported_formats
}
})
if __name__ == '__main__':
# Ensure upload directory exists
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Start the application
app.run(host='0.0.0.0', port=8080, debug=True)