Spaces:
Runtime error
Runtime error
WebashalarForML
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -187,6 +187,38 @@ def uploaded_file(filename):
|
|
187 |
logging.info(f"Serving file: {filename}")
|
188 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
if __name__ == '__main__':
|
191 |
logging.info("Starting Flask app")
|
192 |
app.run(debug=True)
|
|
|
187 |
logging.info(f"Serving file: {filename}")
|
188 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
189 |
|
190 |
+
@app.route('/clear_folders', methods=['DELETE'])
|
191 |
+
def clear_folders():
|
192 |
+
try:
|
193 |
+
# Function to clear all files in a given folder
|
194 |
+
def clear_folder(folder_path):
|
195 |
+
if os.path.exists(folder_path):
|
196 |
+
for filename in os.listdir(folder_path):
|
197 |
+
file_path = os.path.join(folder_path, filename)
|
198 |
+
try:
|
199 |
+
if os.path.isfile(file_path):
|
200 |
+
os.remove(file_path)
|
201 |
+
logging.info(f"Deleted file: {file_path}")
|
202 |
+
else:
|
203 |
+
logging.warning(f"{file_path} is not a file, skipping.")
|
204 |
+
except Exception as e:
|
205 |
+
logging.error(f"Error deleting file {file_path}: {e}")
|
206 |
+
return jsonify({'message': f"Error deleting file {file_path}"}), 500
|
207 |
+
else:
|
208 |
+
logging.warning(f"Folder {folder_path} does not exist.")
|
209 |
+
return jsonify({'message': f"Folder {folder_path} does not exist"}), 404
|
210 |
+
|
211 |
+
# Clear both the upload and result folders
|
212 |
+
clear_folder(app.config['UPLOAD_FOLDER'])
|
213 |
+
clear_folder(app.config['RESULT_FOLDER'])
|
214 |
+
|
215 |
+
logging.info("Both upload and result folders cleared successfully.")
|
216 |
+
return jsonify({'message': 'Both upload and result folders cleared successfully'}), 200
|
217 |
+
|
218 |
+
except Exception as e:
|
219 |
+
logging.error(f"An unexpected error occurred while clearing folders: {e}")
|
220 |
+
return jsonify({'message': 'Failed to clear folders'}), 500
|
221 |
+
|
222 |
if __name__ == '__main__':
|
223 |
logging.info("Starting Flask app")
|
224 |
app.run(debug=True)
|