Skip to content

Commit 7112f7c

Browse files
rewritten translation logic entirely (backend)
1 parent 02fba08 commit 7112f7c

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

backend/server.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from AI.main import call_openai_model, classify_image
12
import os
23
import sys
34
from flask import Flask, request, jsonify
@@ -18,11 +19,6 @@
1819
def check_filename(filename):
1920
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
2021

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-
2622

2723
@app.route('/upload', methods=['POST'])
2824
def upload_file():
@@ -60,29 +56,25 @@ def upload_file():
6056
# "result_llm": [],
6157
# "message": "Image processed successfully"
6258
# }
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)
7470

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)
7875

79-
# result_huggingface = classify_image_huggingface(image_opencv)
80-
result_llm = classify_image(image_opencv)
8176
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
8678
}), 200
8779

8880
if __name__ == '__main__':

0 commit comments

Comments
 (0)