Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,22 @@
|
|
2 |
from flask import Flask, send_from_directory, jsonify, request
|
3 |
import os
|
4 |
import json
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
# Directory to store settings
|
9 |
SETTINGS_DIR = "settings"
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Real-world simulation parameters (unscaled)
|
14 |
simulation_params = {
|
@@ -63,8 +72,10 @@ def save_settings():
|
|
63 |
with open(filename, 'w') as f:
|
64 |
json.dump(simulation_params, f, indent=4)
|
65 |
return jsonify({"status": "success", "message": "Settings saved successfully"})
|
|
|
|
|
66 |
except Exception as e:
|
67 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
68 |
|
69 |
# API to load settings from a JSON file
|
70 |
@app.route('/api/load', methods=['GET'])
|
@@ -78,8 +89,10 @@ def load_settings():
|
|
78 |
return jsonify({"status": "success", "params": simulation_params})
|
79 |
else:
|
80 |
return jsonify({"status": "error", "message": "No saved settings found"}), 404
|
|
|
|
|
81 |
except Exception as e:
|
82 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
83 |
|
84 |
if __name__ == '__main__':
|
85 |
port = int(os.environ.get('PORT', 7860)) # Default port for Hugging Face Spaces
|
|
|
2 |
from flask import Flask, send_from_directory, jsonify, request
|
3 |
import os
|
4 |
import json
|
5 |
+
import errno
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
# Directory to store settings
|
10 |
SETTINGS_DIR = "settings"
|
11 |
+
|
12 |
+
# Ensure the settings directory exists and has the correct permissions
|
13 |
+
try:
|
14 |
+
if not os.path.exists(SETTINGS_DIR):
|
15 |
+
os.makedirs(SETTINGS_DIR, mode=0o775)
|
16 |
+
# Attempt to set permissions (may fail if user lacks privileges, but directory should already be created with correct permissions in Dockerfile)
|
17 |
+
os.chmod(SETTINGS_DIR, 0o775)
|
18 |
+
except OSError as e:
|
19 |
+
# Log the error (in a production environment, you'd use proper logging)
|
20 |
+
print(f"Error setting up settings directory: {e}")
|
21 |
|
22 |
# Real-world simulation parameters (unscaled)
|
23 |
simulation_params = {
|
|
|
72 |
with open(filename, 'w') as f:
|
73 |
json.dump(simulation_params, f, indent=4)
|
74 |
return jsonify({"status": "success", "message": "Settings saved successfully"})
|
75 |
+
except PermissionError as e:
|
76 |
+
return jsonify({"status": "error", "message": "Permission denied: Unable to save settings. Please check directory permissions."}), 500
|
77 |
except Exception as e:
|
78 |
+
return jsonify({"status": "error", "message": f"Error saving settings: {str(e)}"}), 500
|
79 |
|
80 |
# API to load settings from a JSON file
|
81 |
@app.route('/api/load', methods=['GET'])
|
|
|
89 |
return jsonify({"status": "success", "params": simulation_params})
|
90 |
else:
|
91 |
return jsonify({"status": "error", "message": "No saved settings found"}), 404
|
92 |
+
except PermissionError as e:
|
93 |
+
return jsonify({"status": "error", "message": "Permission denied: Unable to load settings. Please check directory permissions."}), 500
|
94 |
except Exception as e:
|
95 |
+
return jsonify({"status": "error", "message": f"Error loading settings: {str(e)}"}), 500
|
96 |
|
97 |
if __name__ == '__main__':
|
98 |
port = int(os.environ.get('PORT', 7860)) # Default port for Hugging Face Spaces
|