-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
583 lines (491 loc) · 24.1 KB
/
main.py
File metadata and controls
583 lines (491 loc) · 24.1 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
from flask import Flask, request, jsonify, Response
import os
import sys
import logging
from dotenv import load_dotenv
from src.image_classifier import ImageClassifier
from src.image_analyzer import ImageAnalyzer
from src.rows_vision import RowsVision
from time import time
from io import BytesIO
from src.logging_config import setup_logging
from src.config import AppConfig
import json
load_dotenv()
setup_logging()
app = Flask(__name__)
logger = logging.getLogger(__name__)
app.config['JSON_AS_ASCII'] = False
# Validate required environment variables
required_env_vars = ['API_KEY_ANTHROPIC', 'API_KEY_OPENAI', 'API_KEY_GEMINI', 'API_KEY_GROQ']
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
if missing_vars:
logger.error(f"Missing required environment variables: {missing_vars}")
sys.exit(1)
# Get API keys
api_key = os.getenv('API_KEY_ANTHROPIC')
api_key_openai = os.getenv('API_KEY_OPENAI')
api_key_gemini = os.getenv('API_KEY_GEMINI')
api_key_groq = os.getenv('API_KEY_GROQ')
# Initialize components
classifier = ImageClassifier(api_key, api_key_openai, api_key_gemini, api_key_groq)
analyzer = ImageAnalyzer(api_key, api_key_openai, api_key_gemini, api_key_groq)
rows_vision = RowsVision(classifier, analyzer)
config = AppConfig()
def _format_instructions_result(result, include_name=False):
"""
Format the result from classify_with_instructions to return only data_points array
or optionally include name if requested.
Args:
result: Raw result from classifier
include_name: Whether to include the name in the response
Returns:
Formatted result according to new output requirements:
- Default: data_points array only
- With include_name=True: {"name": "...", "data_points": [...]}
"""
logger.info(f'Formatting result: {type(result)} - {result}')
# Handle error cases
if isinstance(result, dict) and 'error' in result:
return result
# Handle dict result (single chart/table)
if isinstance(result, dict):
logger.info(f'Result is dict: {result}')
# Check if it has the expected structure with data_points
if 'data_points' in result:
data_points = result['data_points']
if include_name and 'name' in result:
logger.info('Including name in response')
# Return only name and data_points, exclude type, sampled_axis, has_data_labels
return {
"name": result['name'],
"data_points": data_points
}
else:
logger.info('Returning only data_points')
# Return only data_points array
return data_points
else:
# If it's a dict but doesn't have data_points, return as-is
logger.info('Dict without data_points, returning as-is')
return result
# Handle list of chart results
elif isinstance(result, list) and len(result) > 0:
# Get the first chart/table result
chart_data = result[0]
logger.info(f'Chart data: {type(chart_data)} - {chart_data}')
# Check if it's a dict with the expected structure
if isinstance(chart_data, dict) and 'data_points' in chart_data:
data_points = chart_data['data_points']
if include_name and 'name' in chart_data:
logger.info('Including name in response')
# Return only name and data_points, exclude type, sampled_axis, has_data_labels
return {
"name": chart_data['name'],
"data_points": data_points
}
else:
logger.info('Returning only data_points')
# Return only data_points array
return data_points
else:
# If it's not a dict with data_points, return the first element as-is
logger.info('First element is not a dict with data_points, returning as-is')
return chart_data
# If result is already a simple array (data_points), return as-is
if isinstance(result, list):
logger.info('Result is already a list, returning as-is')
return result
# Fallback to original result if formatting fails
logger.warning('Fallback: returning original result')
return result
@app.route('/api/run', methods=['POST'])
def run_external_api():
"""
Process image from URL using AI models for classification and data extraction.
Expected JSON payload:
{
"image_url": "https://example.com/image.jpg",
"model_classification": "anthropic", # optional
"model_extraction": "anthropic", # optional
"time_outputs": false # optional
}
"""
try:
data = request.json
if not data:
return jsonify({'error': 'Missing JSON payload'}), 400
# Validate image_url
image_url = data.get('image_url')
if not image_url:
return jsonify({'error': 'Missing image_url parameter'}), 400
if not image_url.startswith(('http://', 'https://')):
return jsonify({'error': 'Invalid image URL - must start with http:// or https://'}), 400
# Get optional parameters with defaults
model_classification = data.get('model_classification', 'anthropic')
model_extraction = data.get('model_extraction', 'anthropic')
time_outputs = data.get('time_outputs', False)
# Validate model names
valid_models = ['anthropic', 'openai', 'google', 'groq']
if model_classification not in valid_models:
return jsonify({'error': f'Invalid model_classification. Must be one of: {valid_models}'}), 400
if model_extraction not in valid_models:
return jsonify({'error': f'Invalid model_extraction. Must be one of: {valid_models}'}), 400
try:
file_extension, filename, file_stream = rows_vision.download_image_from_url(image_url)
except Exception as e:
logger.error(f"Failed to download image from {image_url}: {str(e)}")
return jsonify({'error': f'Failed to download image: {str(e)}'}), 400
if time_outputs:
start_time = time()
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction)
total_time = round(time() - start_time, 3)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {
"result": formatted_result,
"metrics": {
"total_time": total_time
}
}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
else:
# Get raw result and format it like the instructions endpoint
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {"result": formatted_result}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
except Exception as e:
logger.error(f"Unexpected error in run_external_api: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/api/run-file', methods=['POST'])
def run_external_api_file():
"""
Process image from URL or local file path using AI models.
Expected JSON payload:
{
"image_url": "https://example.com/image.jpg", # OR
"file_path": "/path/to/local/image.jpg", # OR
"model_classification": "anthropic", # optional
"model_extraction": "anthropic", # optional
"time_outputs": false # optional
}
"""
try:
data = request.json
if not data:
return jsonify({'error': 'Missing JSON payload'}), 400
# Get optional parameters with defaults
model_classification = data.get('model_classification', 'anthropic')
model_extraction = data.get('model_extraction', 'anthropic')
time_outputs = data.get('time_outputs', False)
# Validate model names
valid_models = ['anthropic', 'openai', 'google', 'groq']
if model_classification not in valid_models:
return jsonify({'error': f'Invalid model_classification. Must be one of: {valid_models}'}), 400
if model_extraction not in valid_models:
return jsonify({'error': f'Invalid model_extraction. Must be one of: {valid_models}'}), 400
image_url = data.get('image_url')
file_path = data.get('file_path')
if not image_url and not file_path:
return jsonify({'error': 'Missing image_url or file_path parameter'}), 400
if image_url and file_path:
return jsonify({'error': 'Provide either image_url or file_path, not both'}), 400
try:
if image_url:
if not image_url.startswith(('http://', 'https://')):
return jsonify({'error': 'Invalid image URL - must start with http:// or https://'}), 400
file_extension, filename, file_stream = rows_vision.download_image_from_url(image_url)
elif file_path:
if not os.path.exists(file_path):
return jsonify({'error': f'File not found: {file_path}'}), 400
file_extension = os.path.splitext(file_path)[1].lstrip(".")
filename = os.path.basename(file_path)
with open(file_path, "rb") as f:
file_stream = BytesIO(f.read())
except Exception as e:
logger.error(f"Failed to load image: {str(e)}")
return jsonify({'error': f'Failed to load image: {str(e)}'}), 400
if time_outputs:
start_time = time()
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction)
total_time = round(time() - start_time, 3)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {
"result": formatted_result,
"metrics": {
"total_time": total_time
}
}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
else:
# Get raw result and format it like the instructions endpoint
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {"result": formatted_result}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
except Exception as e:
logger.error(f"Unexpected error in run_external_api_file: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/api/run-one-shot', methods=['POST'])
def run_external_api_one_shot():
"""
Process image from URL or local file path using AI models.
Expected JSON payload:
{
"image_url": "https://example.com/image.jpg", # OR
"file_path": "/path/to/local/image.jpg", # OR
"model_classification": "anthropic", # optional
"model_extraction": "anthropic", # optional
"time_outputs": false # optional
}
"""
try:
data = request.json
if not data:
return jsonify({'error': 'Missing JSON payload'}), 400
# Get optional parameters with defaults
model_classification = data.get('model_classification', 'anthropic')
model_extraction = data.get('model_extraction', 'anthropic')
time_outputs = data.get('time_outputs', False)
# Validate model names
valid_models = ['anthropic', 'openai', 'google', 'groq']
if model_classification not in valid_models:
return jsonify({'error': f'Invalid model_classification. Must be one of: {valid_models}'}), 400
if model_extraction not in valid_models:
return jsonify({'error': f'Invalid model_extraction. Must be one of: {valid_models}'}), 400
image_url = data.get('image_url')
file_path = data.get('file_path')
if not image_url and not file_path:
return jsonify({'error': 'Missing image_url or file_path parameter'}), 400
if image_url and file_path:
return jsonify({'error': 'Provide either image_url or file_path, not both'}), 400
try:
if image_url:
if not image_url.startswith(('http://', 'https://')):
return jsonify({'error': 'Invalid image URL - must start with http:// or https://'}), 400
file_extension, filename, file_stream = rows_vision.download_image_from_url(image_url)
elif file_path:
if not os.path.exists(file_path):
return jsonify({'error': f'File not found: {file_path}'}), 400
file_extension = os.path.splitext(file_path)[1].lstrip(".")
filename = os.path.basename(file_path)
with open(file_path, "rb") as f:
file_stream = BytesIO(f.read())
except Exception as e:
logger.error(f"Failed to load image: {str(e)}")
return jsonify({'error': f'Failed to load image: {str(e)}'}), 400
if time_outputs:
start_time = time()
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction, skip_step=True)
total_time = round(time() - start_time, 3)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {
"result": formatted_result,
"metrics": {
"total_time": total_time
}
}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
else:
# Get raw result and format it like the instructions endpoint
raw_result = rows_vision.run_image_json(file_extension, filename, file_stream, model_classification, model_extraction, skip_step=True)
# Convert dict format back to data_points format for consistency
if isinstance(raw_result, list) and len(raw_result) > 0 and isinstance(raw_result[0], dict):
# Convert from dict format back to data_points array format
headers = list(raw_result[0].keys())
data_points = [headers]
for row_dict in raw_result:
row = [row_dict.get(header, None) for header in headers]
data_points.append(row)
formatted_result = data_points
else:
formatted_result = raw_result
response_data = {"result": formatted_result}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
except Exception as e:
logger.error(f"Unexpected error in run_external_api_one_shot: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/api/classify-with-instructions', methods=['POST'])
def classify_with_instructions():
"""
Classify and extract data from image with custom instructions using single model.
Expected JSON payload:
{
"image_url": "https://example.com/image.jpg", # OR
"file_path": "/path/to/local/image.jpg", # OR
"instructions": "Extract only the revenue data from this chart", # optional - if empty, passes only image
"model": "google", # 'google', 'openai', or 'anthropic'
"time_outputs": false, # optional
"include_name": false # optional - include chart name in response
}
"""
try:
data = request.json
if not data:
return jsonify({'error': 'Missing JSON payload'}), 400
# Get instructions parameter (optional - can be empty)
instructions = data.get('instructions', '')
model = data.get('model', 'google')
time_outputs = data.get('time_outputs', False)
include_name = data.get('include_name', False)
# Validate model names
valid_models = ['google', 'openai', 'anthropic']
if model not in valid_models:
return jsonify({'error': f'Invalid model. Must be one of: {valid_models}'}), 400
image_url = data.get('image_url')
file_path = data.get('file_path')
if not image_url and not file_path:
return jsonify({'error': 'Missing image_url or file_path parameter'}), 400
if image_url and file_path:
return jsonify({'error': 'Provide either image_url or file_path, not both'}), 400
try:
if image_url:
if not image_url.startswith(('http://', 'https://')):
return jsonify({'error': 'Invalid image URL - must start with http:// or https://'}), 400
file_extension, filename, file_stream = rows_vision.download_image_from_url(image_url)
elif file_path:
if not os.path.exists(file_path):
return jsonify({'error': f'File not found: {file_path}'}), 400
file_extension = os.path.splitext(file_path)[1].lstrip(".")
filename = os.path.basename(file_path)
with open(file_path, "rb") as f:
file_stream = BytesIO(f.read())
except Exception as e:
logger.error(f"Failed to load image: {str(e)}")
return jsonify({'error': f'Failed to load image: {str(e)}'}), 400
# Validate file extension
good_image, file_type = classifier.check_file_extension(filename)
if not good_image:
return jsonify({'error': f'Unsupported file type: {filename}'}), 400
if time_outputs:
start_time = time()
result = classifier.classify_with_instructions(file_stream, file_type, instructions, model)
total_time = round(time() - start_time, 3)
# Format result according to new output requirements
formatted_result = _format_instructions_result(result, include_name)
response_data = {
"result": formatted_result,
"metrics": {
"total_time": total_time
}
}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
else:
result = classifier.classify_with_instructions(file_stream, file_type, instructions, model)
# Format result according to new output requirements
formatted_result = _format_instructions_result(result, include_name)
response_data = {"result": formatted_result}
# Use Response with ensure_ascii=False
return Response(
response=json.dumps(response_data, ensure_ascii=False),
status=200,
mimetype='application/json; charset=utf-8'
)
except Exception as e:
logger.error(f"Unexpected error in classify_with_instructions: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/health', methods=['GET'])
def health_check():
"""Health check endpoint for monitoring and Docker."""
return jsonify({
'status': 'healthy',
'service': 'rows_vision',
'timestamp': time()
})
@app.errorhandler(413)
def request_entity_too_large(error):
return jsonify({'error': 'File too large'}), 413
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(405)
def method_not_allowed(error):
return jsonify({'error': 'Method not allowed'}), 405
if __name__ == '__main__':
logger.info("Starting Image Analysis API server...")
app.run(host=config.host, port=config.port, debug=config.debug)