Spaces:
Sleeping
Sleeping
Create proxy/upload_api.py
Browse files- proxy/upload_api.py +32 -0
proxy/upload_api.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
DATA_DIR = "/data"
|
| 6 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
@app.route("/api/upload", methods=["POST"])
|
| 9 |
+
def upload():
|
| 10 |
+
if "file" not in request.files:
|
| 11 |
+
return "No file", 400
|
| 12 |
+
f = request.files["file"]
|
| 13 |
+
if not f.filename.endswith(".conllu"):
|
| 14 |
+
return "Only .conllu allowed", 400
|
| 15 |
+
f.save(os.path.join(DATA_DIR, f.filename))
|
| 16 |
+
return "OK"
|
| 17 |
+
|
| 18 |
+
@app.route("/api/files", methods=["GET"])
|
| 19 |
+
def list_files():
|
| 20 |
+
files = [f for f in os.listdir(DATA_DIR) if f.endswith(".conllu")]
|
| 21 |
+
return jsonify(files)
|
| 22 |
+
|
| 23 |
+
@app.route("/api/download/<path:filename>", methods=["GET"])
|
| 24 |
+
def download(filename):
|
| 25 |
+
return send_from_directory(DATA_DIR, filename, as_attachment=True)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
import argparse
|
| 29 |
+
parser = argparse.ArgumentParser()
|
| 30 |
+
parser.add_argument("--port", type=int, default=5556)
|
| 31 |
+
args = parser.parse_args()
|
| 32 |
+
app.run(host="0.0.0.0", port=args.port)
|