|
1 |
| -from flask import Flask, jsonify, abort |
2 |
| -from llm_functions import get_summaries_and_tags_api_function |
| 1 | +from flask import Flask, jsonify, abort, request |
| 2 | +from llm_functions import get_summary_api_function, get_tags_api_function |
| 3 | +import json |
3 | 4 |
|
4 | 5 | app = Flask(__name__)
|
5 | 6 |
|
6 |
| -@app.route('/summary', methods=['POST']) |
| 7 | + |
| 8 | +def is_intersection(keys, required_keys): |
| 9 | + return (keys & required_keys) == required_keys |
| 10 | + |
| 11 | + |
| 12 | +@app.route("/summary", methods=["POST"]) |
7 | 13 | def summary():
|
8 | 14 | body = json.loads(request.data)
|
9 | 15 | # We require bill_id, bill_title, bill_text to exist as keys in the POST
|
10 |
| - # Note: & is essentially set intersection |
11 |
| - if not (body.keys() & {"bill_id", "bill_title", "bill_text"}): |
| 16 | + if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}): |
12 | 17 | abort(404, description="requires bill_id, bill_title, and bill_text")
|
13 | 18 |
|
14 |
| - summary = get_summaries_and_tags_api_function( |
15 |
| - body["bill_id"], |
16 |
| - body["bill_title"], |
17 |
| - body["bill_text"] |
| 19 | + summary = get_summary_api_function( |
| 20 | + body["bill_id"], body["bill_title"], body["bill_text"] |
18 | 21 | )
|
19 | 22 |
|
20 | 23 | if summary["status"] in [-1, -2]:
|
21 |
| - abort(404, description="unable to generate summary or tags") |
22 |
| - |
23 |
| - return jsonify(summary) |
| 24 | + abort(500, description="Unable to generate summary") |
| 25 | + |
| 26 | + return jsonify(summary["summary"]) |
| 27 | + |
| 28 | + |
| 29 | +@app.route("/tags", methods=["POST"]) |
| 30 | +def tags(): |
| 31 | + body = json.loads(request.data) |
| 32 | + # We require bill_id, bill_title, bill_text to exist as keys in the POST |
| 33 | + # Note: & is essentially set intersection |
| 34 | + if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}): |
| 35 | + abort(404, description="requires bill_id, bill_title, and bill_text") |
| 36 | + |
| 37 | + tags = get_tags_api_function(body["bill_id"], body["bill_title"], body["bill_text"]) |
| 38 | + |
| 39 | + if tags["status"] in [-1, -2]: |
| 40 | + abort(500, description="Unable to generate tags") |
| 41 | + |
| 42 | + return jsonify(tags["tags"]) |
0 commit comments