Spaces:
Sleeping
Sleeping
MitchelHsu
commited on
Commit
•
c8cd6ca
1
Parent(s):
315b09e
Delete app/app.py
Browse files- app/app.py +0 -91
app/app.py
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
from agent import Agent
|
2 |
-
from config import MODEL
|
3 |
-
from flask import Flask, jsonify, request
|
4 |
-
from utils import read_documents, validate_request_logs
|
5 |
-
from models import GetQuestionAndFactsResponse, SubmitQuestionAndDocumentsResponse, SubmitQuestionAndDocumentRequest
|
6 |
-
|
7 |
-
app = Flask(__name__)
|
8 |
-
agent = Agent(model=MODEL)
|
9 |
-
processing = False
|
10 |
-
submitted_data = None
|
11 |
-
|
12 |
-
|
13 |
-
@app.route('/get_question_and_facts', methods=['GET'])
|
14 |
-
def get_response():
|
15 |
-
global submitted_data, processing, agent
|
16 |
-
|
17 |
-
# If no data found
|
18 |
-
if not submitted_data:
|
19 |
-
response = GetQuestionAndFactsResponse(
|
20 |
-
question='',
|
21 |
-
facts=[],
|
22 |
-
status='No data found, please submit data'
|
23 |
-
)
|
24 |
-
return jsonify(response.dict()), 200
|
25 |
-
|
26 |
-
# If still processing request
|
27 |
-
if processing:
|
28 |
-
response = GetQuestionAndFactsResponse(
|
29 |
-
question=submitted_data.question,
|
30 |
-
facts=[],
|
31 |
-
status='processing'
|
32 |
-
)
|
33 |
-
return jsonify(response.dict()), 200
|
34 |
-
|
35 |
-
# Request processed, create response with Agent summarization
|
36 |
-
response = GetQuestionAndFactsResponse(
|
37 |
-
question=submitted_data.question,
|
38 |
-
facts=agent.get_response_list(),
|
39 |
-
status='done'
|
40 |
-
)
|
41 |
-
|
42 |
-
return jsonify(response.dict()), 200
|
43 |
-
|
44 |
-
|
45 |
-
@app.route('/submit_question_and_documents', methods=['POST'])
|
46 |
-
def submit_question():
|
47 |
-
global submitted_data, processing, agent
|
48 |
-
processing = True
|
49 |
-
request_content = request.get_json()
|
50 |
-
|
51 |
-
# Submit payload read and validation
|
52 |
-
try:
|
53 |
-
submitted_data = SubmitQuestionAndDocumentRequest(**request_content)
|
54 |
-
except ValueError as e:
|
55 |
-
response = SubmitQuestionAndDocumentsResponse(status=f'Request payload does not match expected schema: {str(e)}')
|
56 |
-
return jsonify(response.dict()), 200
|
57 |
-
|
58 |
-
# Validate request URLS formats
|
59 |
-
try:
|
60 |
-
validate_request_logs(submitted_data.documents)
|
61 |
-
except ValueError as e:
|
62 |
-
# Respond with URL validation failed error
|
63 |
-
response = SubmitQuestionAndDocumentsResponse(status=f'URL validation failed: {e}')
|
64 |
-
return jsonify(response.dict()), 200
|
65 |
-
|
66 |
-
# Try loading documents
|
67 |
-
try:
|
68 |
-
logs = read_documents(submitted_data.documents)
|
69 |
-
except Exception as e:
|
70 |
-
# Respond with URL read fail if URL read error
|
71 |
-
response = SubmitQuestionAndDocumentsResponse(status=f'URL read failed: {e}')
|
72 |
-
return jsonify(response.dict()), 200
|
73 |
-
|
74 |
-
# If no data found
|
75 |
-
if len(logs) == 0:
|
76 |
-
response = SubmitQuestionAndDocumentsResponse(status=f'No data found in the URLs')
|
77 |
-
return jsonify(response.dict()), 200
|
78 |
-
|
79 |
-
# Call agent to summarize logs
|
80 |
-
agent.summarize(
|
81 |
-
question=submitted_data.question,
|
82 |
-
logs=logs
|
83 |
-
)
|
84 |
-
|
85 |
-
processing = False
|
86 |
-
response = SubmitQuestionAndDocumentsResponse(status='success')
|
87 |
-
return jsonify(response.dict()), 200
|
88 |
-
|
89 |
-
|
90 |
-
if __name__ == '__main__':
|
91 |
-
app.run(host='0.0.0.0', port=8000, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|