Spaces:
Runtime error
Runtime error
Create flask_api.py
Browse files- flask_api.py +21 -0
flask_api.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, send_from_directory, abort
|
| 2 |
+
import os
|
| 3 |
+
app = Flask(__name__)
|
| 4 |
+
DATASETS_DIR = os.path.abspath(os.getenv("DATASETS_DIR", "./datasets"))
|
| 5 |
+
@app.route('/health', methods=['GET'])
|
| 6 |
+
def health():
|
| 7 |
+
return jsonify({"status": "OK", "service": "flask", "timestamp": __import__('datetime').datetime.utcnow().isoformat()})
|
| 8 |
+
@app.route('/datasets', methods=['GET'])
|
| 9 |
+
def list_datasets():
|
| 10 |
+
if not os.path.isdir(DATASETS_DIR):
|
| 11 |
+
return jsonify({"error": "Datasets directory not found"}), 404
|
| 12 |
+
files = os.listdir(DATASETS_DIR)
|
| 13 |
+
return jsonify({"datasets": files})
|
| 14 |
+
@app.route('/datasets/<filename>', methods=['GET'])
|
| 15 |
+
def get_dataset(filename):
|
| 16 |
+
try:
|
| 17 |
+
return send_from_directory(DATASETS_DIR, filename, as_attachment=True)
|
| 18 |
+
except FileNotFoundError:
|
| 19 |
+
abort(404)
|
| 20 |
+
if __name__ == '__main__':
|
| 21 |
+
app.run(host="0.0.0.0", port=5000)
|