-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
73 lines (60 loc) · 2.14 KB
/
Copy pathapi.py
File metadata and controls
73 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Standard imports
import json
from threading import Thread
from crewai.crews.crew_output import CrewOutput
from uuid import uuid4
# Third-party imports
from flask import Flask, jsonify, request, abort
from flask_cors import CORS
from dotenv import load_dotenv
# Local application/library specific imports
from utils.job_manager import jobs, jobs_lock
from Controller.crew_control.kickoff_crew import kickoff_crew
import logging
load_dotenv()
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route('/api/crew', methods=['POST'])
def run_crew():
logging.info("Received request to run crew")
# Validation
data = request.json
if not data or 'question' not in data:
abort(400, description="Invalid input data provided.")
job_id = str(uuid4())
question = data['question']
thread = Thread(target=kickoff_crew, args=(job_id, question))
thread.start()
return jsonify({"job_id": job_id}), 202
#Returns Status/Results of each subprocess (Job)
@app.route('/api/crew/<job_id>', methods=['GET'])
def get_status(job_id):
with jobs_lock:
job = jobs.get(job_id)
if job is None:
abort(404, description="Job not found")
if isinstance(job.result, CrewOutput):
# Use json_dict if available
if job.result.json_dict:
result_json = job.result.json_dict
# Use json string if json_dict is not available
elif job.result.json:
result_json = json.loads(job.result.json)
else:
result_json = {"error": "No JSON result available"}
else:
try:
result_json = json.loads(job.result)
except (TypeError, json.JSONDecodeError):
result_json = job.result
return jsonify({
"job_id": job_id,
"status": job.status,
"result": result_json,
"events": [{"timestamp": event.timestamp.isoformat(), "data": event.data} for event in job.events]
})
@app.route('/api', methods=['GET'])
def test():
return jsonify({"message":"working" }), 202
if __name__ == '__main__':
app.run(debug=True, port=3001)