|
| 1 | +from AI.main import call_openai_model, classify_image |
1 | 2 | import os |
2 | 3 | import sys |
3 | 4 | from flask import Flask, request, jsonify |
|
18 | 19 | def check_filename(filename): |
19 | 20 | return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] |
20 | 21 |
|
21 | | -def reset_uploads(): |
22 | | - for filename in os.listdir(UPLOAD_FOLDER): |
23 | | - file_path = os.path.join(UPLOAD_FOLDER, filename) |
24 | | - os.remove(file_path) |
25 | | - |
26 | 22 |
|
27 | 23 | @app.route('/upload', methods=['POST']) |
28 | 24 | def upload_file(): |
@@ -60,29 +56,25 @@ def upload_file(): |
60 | 56 | # "result_llm": [], |
61 | 57 | # "message": "Image processed successfully" |
62 | 58 | # } |
63 | | -@app.route('/processing_translate', methods=['GET']) |
64 | | -def procesing_translate(): |
65 | | - data = request.json |
66 | | - if not data or 'filename' not in data: |
67 | | - return jsonify({'error': 'No filename provided'}), 400 |
68 | | - |
69 | | - filename = data['filename'] |
70 | | - file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
71 | | - |
72 | | - if not os.path.exists(file_path): |
73 | | - return jsonify({'error': 'File not found'}), 404 |
| 59 | +@app.route('/translate', methods=['GET']) |
| 60 | +def translate(): |
| 61 | + all_translations = [] |
| 62 | + for filename in os.listdir(UPLOAD_FOLDER): |
| 63 | + file_path = os.path.join(UPLOAD_FOLDER, filename) |
| 64 | + image_opencv = cv2.imread(file_path) |
| 65 | + if image_opencv is None: |
| 66 | + return jsonify({'error': f'Could not read image: {filename}'}), 400 |
| 67 | + |
| 68 | + translation_result = classify_image(image_opencv) |
| 69 | + all_translations.append(translation_result) |
74 | 70 |
|
75 | | - image_opencv = cv2.imread(file_path) |
76 | | - if image_opencv is None: |
77 | | - return jsonify({'error': 'Could not read the image'}), 400 |
| 71 | + os.remove(file_path) |
| 72 | + |
| 73 | + translation_string = ' '.join(all_translations) |
| 74 | + final_translation = call_openai_model(translation_string) |
78 | 75 |
|
79 | | - # result_huggingface = classify_image_huggingface(image_opencv) |
80 | | - result_llm = classify_image(image_opencv) |
81 | 76 | return jsonify({ |
82 | | - 'message': 'Image processed successfully', |
83 | | - 'filename': filename, |
84 | | - 'result_llm': result_llm |
85 | | - # 'huggingface_result': result_huggingface |
| 77 | + 'translation': final_translation.content |
86 | 78 | }), 200 |
87 | 79 |
|
88 | 80 | if __name__ == '__main__': |
|
0 commit comments