Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,16 +17,15 @@ logging.basicConfig(
|
|
17 |
# Flask App
|
18 |
app = Flask(__name__)
|
19 |
app.secret_key = 'your_secret_key'
|
|
|
20 |
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
21 |
app.config['RESULT_FOLDER'] = 'results/'
|
22 |
-
app.config['JSON_FOLDER'] = 'json/'
|
23 |
|
24 |
UPLOAD_FOLDER = 'static/uploads/'
|
25 |
RESULT_FOLDER = 'static/results/'
|
26 |
-
|
27 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
28 |
os.makedirs(RESULT_FOLDER, exist_ok=True)
|
29 |
-
os.makedirs(JSON_FOLDER, exist_ok=True)
|
30 |
|
31 |
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
32 |
os.makedirs(app.config['UPLOAD_FOLDER'])
|
@@ -34,9 +33,6 @@ if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
|
34 |
if not os.path.exists(app.config['RESULT_FOLDER']):
|
35 |
os.makedirs(app.config['RESULT_FOLDER'])
|
36 |
|
37 |
-
if not os.path.exists(app.config['JSON_FOLDER']):
|
38 |
-
os.makedirs(app.config['JSON_FOLDER'])
|
39 |
-
|
40 |
# Set the PaddleOCR home directory to a writable location
|
41 |
os.environ['PADDLEOCR_HOME'] = '/tmp/.paddleocr'
|
42 |
|
@@ -89,9 +85,9 @@ def upload_file():
|
|
89 |
logging.error(f"Error saving file {filename}: {save_error}")
|
90 |
return jsonify({'message': f"Error saving file {filename}"}), 500
|
91 |
|
92 |
-
session['uploaded_files'] = uploaded_files # Store uploaded files in session
|
93 |
logging.info(f"Files successfully uploaded: {uploaded_files}")
|
94 |
-
return process_file(uploaded_files)
|
95 |
|
96 |
except Exception as e:
|
97 |
logging.error(f"An error occurred during file upload: {e}")
|
@@ -109,18 +105,16 @@ def remove_file():
|
|
109 |
logging.warning(f"File not found for removal: {file_path}") # More specific log
|
110 |
|
111 |
session.pop('uploaded_files', None)
|
112 |
-
|
113 |
logging.info("All uploaded files removed")
|
114 |
return redirect(url_for('index'))
|
115 |
|
116 |
-
from flask import jsonify
|
117 |
-
|
118 |
@app.route('/process_file/<filename>', methods=['GET', 'POST'])
|
119 |
def process_file(filename):
|
120 |
try:
|
121 |
uploaded_files = session.get('uploaded_files', [])
|
122 |
if not uploaded_files:
|
123 |
-
|
124 |
logging.warning("No files selected for processing")
|
125 |
return redirect(url_for('index'))
|
126 |
|
@@ -175,23 +169,15 @@ def process_file(filename):
|
|
175 |
session['processed_data'] = processed_data
|
176 |
session['processed_Img'] = processed_Img
|
177 |
|
178 |
-
|
179 |
logging.info("Data processed and analyzed successfully")
|
180 |
return jsonify({'process_data': [processed_data], 'process_image': processed_Img})
|
181 |
|
182 |
except Exception as e:
|
183 |
logging.error(f"An unexpected error occurred during file processing: {e}")
|
184 |
-
|
185 |
return jsonify({'message': 'File processing failed'}), 500
|
186 |
|
187 |
-
|
188 |
-
@app.route('/result')
|
189 |
-
def result():
|
190 |
-
processed_data = session.get('processed_data', {})
|
191 |
-
processed_Img = session.get('processed_Img', {})
|
192 |
-
logging.info(f"Displaying results: Data - {processed_data}, Images - {processed_Img}")
|
193 |
-
return render_template('result.html', data=processed_data, Img=processed_Img)
|
194 |
-
|
195 |
@app.route('/uploads/<filename>')
|
196 |
def uploaded_file(filename):
|
197 |
logging.info(f"Serving file: {filename}")
|
@@ -199,4 +185,4 @@ def uploaded_file(filename):
|
|
199 |
|
200 |
if __name__ == '__main__':
|
201 |
logging.info("Starting Flask app")
|
202 |
-
app.run(debug=True)
|
|
|
17 |
# Flask App
|
18 |
app = Flask(__name__)
|
19 |
app.secret_key = 'your_secret_key'
|
20 |
+
|
21 |
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
22 |
app.config['RESULT_FOLDER'] = 'results/'
|
|
|
23 |
|
24 |
UPLOAD_FOLDER = 'static/uploads/'
|
25 |
RESULT_FOLDER = 'static/results/'
|
26 |
+
|
27 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
28 |
os.makedirs(RESULT_FOLDER, exist_ok=True)
|
|
|
29 |
|
30 |
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
31 |
os.makedirs(app.config['UPLOAD_FOLDER'])
|
|
|
33 |
if not os.path.exists(app.config['RESULT_FOLDER']):
|
34 |
os.makedirs(app.config['RESULT_FOLDER'])
|
35 |
|
|
|
|
|
|
|
36 |
# Set the PaddleOCR home directory to a writable location
|
37 |
os.environ['PADDLEOCR_HOME'] = '/tmp/.paddleocr'
|
38 |
|
|
|
85 |
logging.error(f"Error saving file {filename}: {save_error}")
|
86 |
return jsonify({'message': f"Error saving file {filename}"}), 500
|
87 |
|
88 |
+
#session['uploaded_files'] = uploaded_files # Store uploaded files in session
|
89 |
logging.info(f"Files successfully uploaded: {uploaded_files}")
|
90 |
+
return process_file(uploaded_files), 200
|
91 |
|
92 |
except Exception as e:
|
93 |
logging.error(f"An error occurred during file upload: {e}")
|
|
|
105 |
logging.warning(f"File not found for removal: {file_path}") # More specific log
|
106 |
|
107 |
session.pop('uploaded_files', None)
|
108 |
+
print('Files successfully removed')
|
109 |
logging.info("All uploaded files removed")
|
110 |
return redirect(url_for('index'))
|
111 |
|
|
|
|
|
112 |
@app.route('/process_file/<filename>', methods=['GET', 'POST'])
|
113 |
def process_file(filename):
|
114 |
try:
|
115 |
uploaded_files = session.get('uploaded_files', [])
|
116 |
if not uploaded_files:
|
117 |
+
print('No files selected for processing')
|
118 |
logging.warning("No files selected for processing")
|
119 |
return redirect(url_for('index'))
|
120 |
|
|
|
169 |
session['processed_data'] = processed_data
|
170 |
session['processed_Img'] = processed_Img
|
171 |
|
172 |
+
print('Data processed and analyzed successfully')
|
173 |
logging.info("Data processed and analyzed successfully")
|
174 |
return jsonify({'process_data': [processed_data], 'process_image': processed_Img})
|
175 |
|
176 |
except Exception as e:
|
177 |
logging.error(f"An unexpected error occurred during file processing: {e}")
|
178 |
+
print('An error occurred during file processing')
|
179 |
return jsonify({'message': 'File processing failed'}), 500
|
180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
@app.route('/uploads/<filename>')
|
182 |
def uploaded_file(filename):
|
183 |
logging.info(f"Serving file: {filename}")
|
|
|
185 |
|
186 |
if __name__ == '__main__':
|
187 |
logging.info("Starting Flask app")
|
188 |
+
app.run(debug=True)
|