55import importlib .util
66from pathlib import Path
77
8- module_path = Path (__file__ ).resolve ().parent .parent / 'AI' / ' main.py'
8+ module_path = Path (__file__ ).resolve ().parent .parent / "AI" / " main.py"
99
1010spec = importlib .util .spec_from_file_location ("ai" , module_path )
1111ai = importlib .util .module_from_spec (spec )
1717if parent_dir not in sys .path :
1818 sys .path .insert (0 , parent_dir )
1919from AI .main import classify_image
20+
2021app = Flask (__name__ )
2122
22- UPLOAD_FOLDER = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'uploads' )
23- app .config ['UPLOAD_FOLDER' ] = UPLOAD_FOLDER
24- app .config ['ALLOWED_EXTENSIONS' ] = ['jpg' , 'png' , 'jpeg' ]
23+ UPLOAD_FOLDER = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "uploads" )
24+ app .config ["UPLOAD_FOLDER" ] = UPLOAD_FOLDER
25+ app .config ["ALLOWED_EXTENSIONS" ] = ["jpg" , "png" , "jpeg" ]
26+
27+ cod_counter = 10000
28+
2529
26- cod_counter = 1
2730def check_filename (filename ):
28- return '.' in filename and filename .rsplit ('.' , 1 )[1 ].lower () in app .config ['ALLOWED_EXTENSIONS' ]
31+ return (
32+ "." in filename
33+ and filename .rsplit ("." , 1 )[1 ].lower () in app .config ["ALLOWED_EXTENSIONS" ]
34+ )
2935
3036
31- @app .route (' /upload' , methods = [' POST' ])
37+ @app .route (" /upload" , methods = [" POST" ])
3238def upload_file ():
3339 global cod_counter
3440 # Check if a file is present in the request.
35- if ' file' not in request .files :
36- return jsonify ({' error' : ' No file part' }), 400
41+ if " file" not in request .files :
42+ return jsonify ({" error" : " No file part" }), 400
3743
38- file = request .files [' file' ]
44+ file = request .files [" file" ]
3945
40- if file .filename == '' :
46+ if file .filename == "" :
4147 # In case we have an empty file.
42- return jsonify ({' error' : ' No selected file' }), 400
48+ return jsonify ({" error" : " No selected file" }), 400
4349
4450 if file and check_filename (file .filename ):
4551 # Append the cod_counter to the filename because without it,
@@ -48,52 +54,41 @@ def upload_file():
4854 filename = f"{ cod_counter } _{ file .filename } "
4955 # Concatenate with a counter because otherwise, it would place
5056 # different images as the same file object in the folder.
51- file .save (os .path .join (app .config [' UPLOAD_FOLDER' ], filename ))
57+ file .save (os .path .join (app .config [" UPLOAD_FOLDER" ], filename ))
5258 cod_counter += 1
53- return jsonify ({' message' : ' File uploaded successfully!' }), 200
59+ return jsonify ({" message" : " File uploaded successfully!" }), 200
5460 else :
55- return jsonify ({'error' : 'File type not allowed' }), 400
56-
57- # Image processing route, expecting to receive a request like:
58- # {
59- # "filename": "image_name.jpg"
60- # }
61-
62- # And the correct response would be something like:
63- # {
64- # "filename": "image_name.jpg",
65- # "result_llm": [],
66- # "message": "Image processed successfully"
67- # }
68- @app .route ('/translate' , methods = ['GET' ])
61+ return jsonify ({"error" : "File type not allowed" }), 400
62+
63+
64+ @app .route ("/translate" , methods = ["GET" ])
6965def translate ():
7066 all_translations = []
7167 for filename in os .listdir (UPLOAD_FOLDER ):
7268 file_path = os .path .join (UPLOAD_FOLDER , filename )
7369 image_opencv = cv2 .imread (file_path )
7470 if image_opencv is None :
75- return jsonify ({' error' : f' Could not read image: { filename } ' }), 400
76-
71+ return jsonify ({" error" : f" Could not read image: { filename } " }), 400
72+
7773 # translation_result = ai.classify_image_huggingface(image_opencv)
7874 # print(translation_result)
79-
75+
8076 translation_result = ai .classify_image (image_opencv )
8177 if translation_result :
8278 all_translations .append (translation_result [0 ][0 ].category_name )
8379 if translation_result :
8480 print (translation_result [0 ][0 ].category_name )
85-
81+ print ( filename )
8682 os .remove (file_path )
87-
88- translation_string = '' .join (all_translations )
83+
84+ translation_string = "" .join (all_translations )
8985 final_translation = ai .call_openai_model (translation_string )
9086 print (final_translation .content )
9187
92- return jsonify ({
93- 'translation' : final_translation .content
94- }), 200
88+ return jsonify ({"translation" : final_translation .content }), 200
89+
9590
96- if __name__ == ' __main__' :
91+ if __name__ == " __main__" :
9792 if not os .path .exists (UPLOAD_FOLDER ):
9893 os .makedirs (UPLOAD_FOLDER )
99- app .run (debug = True , host = ' 0.0.0.0' , port = 5000 )
94+ app .run (debug = True , host = " 0.0.0.0" , port = 5000 )
0 commit comments