1- from flask import Flask , jsonify , abort , request
2- from llm_functions import get_summary_api_function , get_tags_api_function
3- import json
1+ """
2+ The module contains all of the Python based Firebase functions
3+
4+ * add_summary_on_document_created - when new bills are written to the database, this function will trigger via on_document_created
5+ """
6+
47from firebase_admin import initialize_app
5- from firebase_functions import https_fn , options
8+ from firebase_functions import options
69import os
710from firebase_functions .firestore_fn import (
811 on_document_created ,
1215import bill_on_document_created
1316
1417initialize_app ()
15- app = Flask (__name__ )
1618
1719
1820def is_intersection (keys , required_keys ):
@@ -29,56 +31,6 @@ def set_openai_api_key():
2931 os .environ ["OPENAI_API_KEY" ] = os .environ ["OPENAI_DEV" ]
3032
3133
32- @app .route ("/summary" , methods = ["POST" ])
33- def summary ():
34- set_openai_api_key ()
35- body = json .loads (request .data )
36- # We require bill_id, bill_title, bill_text to exist as keys in the POST
37- if not is_intersection (body .keys (), {"bill_id" , "bill_title" , "bill_text" }):
38- abort (404 , description = "requires bill_id, bill_title, and bill_text" )
39-
40- summary = get_summary_api_function (
41- body ["bill_id" ], body ["bill_title" ], body ["bill_text" ]
42- )
43-
44- if summary ["status" ] in [- 1 , - 2 ]:
45- abort (500 , description = "Unable to generate summary" )
46-
47- return jsonify (summary ["summary" ])
48-
49-
50- @app .route ("/tags" , methods = ["POST" ])
51- def tags ():
52- set_openai_api_key ()
53- body = json .loads (request .data )
54- # We require bill_id, bill_title, bill_text to exist as keys in the POST
55- # Note: & is essentially set intersection
56- if not is_intersection (body .keys (), {"bill_id" , "bill_title" , "bill_text" }):
57- abort (404 , description = "requires bill_id, bill_title, and bill_text" )
58-
59- tags = get_tags_api_function (body ["bill_id" ], body ["bill_title" ], body ["bill_text" ])
60-
61- if tags ["status" ] in [- 1 , - 2 ]:
62- abort (500 , description = "Unable to generate tags" )
63-
64- return jsonify (tags ["tags" ])
65-
66-
67- @app .route ("/ready" , methods = ["GET" ])
68- def ready ():
69- return ""
70-
71-
72- @https_fn .on_request (
73- secrets = ["OPENAI_DEV" , "OPENAI_PROD" ],
74- timeout_sec = 300 ,
75- memory = options .MemoryOption .GB_1 ,
76- )
77- def httpsflaskexample (req : https_fn .Request ) -> https_fn .Response :
78- with app .request_context (req .environ ):
79- return app .full_dispatch_request ()
80-
81-
8234@on_document_created (
8335 secrets = ["OPENAI_DEV" , "OPENAI_PROD" ],
8436 timeout_sec = 300 ,
0 commit comments