-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add API for bee-hive Signed-off-by: Akihiko Kuroda <[email protected]>
- Loading branch information
1 parent
d1a95d1
commit 03962c7
Showing
6 changed files
with
158 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Assisted by watsonx Code Assistant | ||
from flask import Flask, request, jsonify | ||
import os | ||
import json | ||
import sys | ||
import io | ||
|
||
import yaml | ||
from bee_hive.workflow import Workflow | ||
|
||
app = Flask(__name__) | ||
|
||
def parse_yaml(file_path): | ||
with open(file_path, "r") as file: | ||
yaml_data = list(yaml.safe_load_all(file)) | ||
return yaml_data | ||
|
||
@app.route('/', methods=['POST']) | ||
def process_workflow(): | ||
if request.method == 'POST': | ||
# Check if a file was uploaded | ||
if 'agents' not in request.files: | ||
return 'No agent definition' | ||
|
||
agents = request.files['agents'] | ||
|
||
# Check if the file has a filename | ||
if agents.filename == '': | ||
return 'No agents file name' | ||
|
||
# Save the file to the server | ||
agents.save("agents") | ||
|
||
if 'workflow' not in request.files: | ||
return 'No agent definition' | ||
|
||
workflow = request.files['workflow'] | ||
|
||
# Check if the file has a filename | ||
if workflow.filename == '': | ||
return 'No workflow file name' | ||
|
||
# Save the file to the server | ||
workflow.save("workflow") | ||
|
||
agents_yaml = parse_yaml("agents") | ||
workflow_yaml = parse_yaml("workflow") | ||
try: | ||
workflow_instance = Workflow(agents_yaml, workflow_yaml[0]) | ||
except Exception as excep: | ||
raise RuntimeError("Unable to create agents") from excep | ||
|
||
output = io.StringIO() | ||
sys.stdout = output | ||
workflow_instance.run() | ||
sys.stdout = sys.__stdout__ | ||
|
||
response = { | ||
'workflow': workflow.filename, | ||
'agents': agents.filename, | ||
'output': output.getvalue() | ||
} | ||
|
||
return jsonify(response) | ||
|
||
return render_template('upload.html') | ||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: bee-hive | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: bee-hive | ||
template: | ||
metadata: | ||
labels: | ||
app: bee-hive | ||
spec: | ||
containers: | ||
- name: bee-hive | ||
image: bee-hive:latest | ||
imagePullPolicy: Never | ||
ports: | ||
- containerPort: 5000 | ||
env: | ||
- name: DUMMY | ||
value: dummyvalue | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: bee-hive | ||
spec: | ||
selector: | ||
app: bee-hive | ||
ports: | ||
- protocol: TCP | ||
port: 80 | ||
targetPort: 5000 | ||
type: NodePort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
|
||
python3.11 api.py | ||
|