Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from processor import DatasetCommandCenter
|
| 3 |
+
import threading
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# In-memory storage for job status (Use Redis for production)
|
| 9 |
+
jobs = {}
|
| 10 |
+
|
| 11 |
+
@app.route('/')
|
| 12 |
+
def index():
|
| 13 |
+
return render_template('index.html')
|
| 14 |
+
|
| 15 |
+
@app.route('/inspect', methods=['POST'])
|
| 16 |
+
def inspect():
|
| 17 |
+
data = request.json
|
| 18 |
+
token = data.get('token')
|
| 19 |
+
dataset_id = data.get('dataset_id')
|
| 20 |
+
split = data.get('split', 'train')
|
| 21 |
+
|
| 22 |
+
center = DatasetCommandCenter(token=token)
|
| 23 |
+
result = center.inspect_dataset(dataset_id, split=split)
|
| 24 |
+
return jsonify(result)
|
| 25 |
+
|
| 26 |
+
@app.route('/preview', methods=['POST'])
|
| 27 |
+
def preview():
|
| 28 |
+
data = request.json
|
| 29 |
+
token = data.get('token')
|
| 30 |
+
dataset_id = data.get('dataset_id')
|
| 31 |
+
split = data.get('split', 'train')
|
| 32 |
+
recipe = data.get('recipe', {})
|
| 33 |
+
|
| 34 |
+
center = DatasetCommandCenter(token=token)
|
| 35 |
+
try:
|
| 36 |
+
preview_rows = center.preview_transform(dataset_id, split, recipe)
|
| 37 |
+
return jsonify({"status": "success", "rows": preview_rows})
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return jsonify({"status": "error", "message": str(e)})
|
| 40 |
+
|
| 41 |
+
@app.route('/execute', methods=['POST'])
|
| 42 |
+
def execute():
|
| 43 |
+
data = request.json
|
| 44 |
+
job_id = str(uuid.uuid4())
|
| 45 |
+
|
| 46 |
+
token = data.get('token')
|
| 47 |
+
source_id = data.get('dataset_id')
|
| 48 |
+
target_id = data.get('target_id')
|
| 49 |
+
split = data.get('split', 'train')
|
| 50 |
+
recipe = data.get('recipe', {})
|
| 51 |
+
max_rows = data.get('max_rows')
|
| 52 |
+
|
| 53 |
+
# Start background thread
|
| 54 |
+
def task():
|
| 55 |
+
center = DatasetCommandCenter(token=token)
|
| 56 |
+
try:
|
| 57 |
+
jobs[job_id] = {"status": "running"}
|
| 58 |
+
res = center.process_and_push(source_id, split, target_id, recipe, max_rows)
|
| 59 |
+
jobs[job_id] = {"status": "completed", "result": res}
|
| 60 |
+
except Exception as e:
|
| 61 |
+
jobs[job_id] = {"status": "failed", "error": str(e)}
|
| 62 |
+
|
| 63 |
+
thread = threading.Thread(target=task)
|
| 64 |
+
thread.start()
|
| 65 |
+
|
| 66 |
+
return jsonify({"status": "started", "job_id": job_id})
|
| 67 |
+
|
| 68 |
+
@app.route('/status/<job_id>', methods=['GET'])
|
| 69 |
+
def status(job_id):
|
| 70 |
+
return jsonify(jobs.get(job_id, {"status": "unknown"}))
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
app.run(debug=True, port=7860, host="0.0.0.0")
|