broadfield-dev commited on
Commit
70aa1ed
·
verified ·
1 Parent(s): 1831bf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -1,7 +1,8 @@
1
- from flask import Flask, render_template, request, jsonify, send_file
2
  from parser import parse_source_to_graph
3
- from dataset_gen import create_dataset_entry, get_dataset_stats, upload_to_hub, OUTPUT_FILE
4
  import os
 
5
 
6
  app = Flask(__name__)
7
 
@@ -14,6 +15,35 @@ def parse():
14
  code = request.json.get('code', '')
15
  return jsonify(parse_source_to_graph(code))
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  @app.route('/dataset/add', methods=['POST'])
18
  def add_entry():
19
  code = request.json.get('code', '')
 
1
+ from flask import Flask, render_template, request, jsonify, send_file, Response
2
  from parser import parse_source_to_graph
3
+ from dataset_gen import create_dataset_entry, get_dataset_stats, upload_to_hub, build_dataset_entry, OUTPUT_FILE
4
  import os
5
+ import json
6
 
7
  app = Flask(__name__)
8
 
 
15
  code = request.json.get('code', '')
16
  return jsonify(parse_source_to_graph(code))
17
 
18
+ # --- New API Endpoint ---
19
+ @app.route('/api/process', methods=['POST'])
20
+ def api_process():
21
+ """
22
+ Direct API for external tools.
23
+ Input: JSON { "code": "...", "format": "json" | "jsonl" }
24
+ Output: The dataset entry object or a raw JSONL string.
25
+ """
26
+ data = request.get_json()
27
+ if not data or 'code' not in data:
28
+ return jsonify({"error": "No code provided"}), 400
29
+
30
+ code = data['code']
31
+ fmt = data.get('format', 'json') # Default to JSON object
32
+
33
+ # Generate the entry without saving to disk
34
+ entry = build_dataset_entry(code)
35
+
36
+ if "error" in entry:
37
+ return jsonify(entry), 400
38
+
39
+ if fmt == 'jsonl':
40
+ # Return as a string suitable for appending to a file
41
+ return Response(json.dumps(entry) + '\n', mimetype='text/plain')
42
+
43
+ # Return as standard JSON object
44
+ return jsonify(entry)
45
+
46
+
47
  @app.route('/dataset/add', methods=['POST'])
48
  def add_entry():
49
  code = request.json.get('code', '')