Skip to content

Commit

Permalink
feat: add deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethereal-O committed Feb 21, 2024
1 parent e6e86e3 commit ac33d14
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 38 deletions.
13 changes: 8 additions & 5 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM ubuntu:20.04
RUN mkdir /work
COPY ../eval /work
WORKDIR /work
COPY ./ /work
RUN mkdir -p /work/results
WORKDIR /work/eval
RUN apt-get update && apt-get install -y python3 python3-pip python3-dev gcc g++ libffi-dev libssl-dev
RUN pip install cython
RUN pip install cryptography
RUN pip install -r requirements.txt
RUN pip install gunicorn
RUN pip install -r /work/deploy/requirements.txt

ENTRYPOINT ["gunicorn", "--workers=4", "--bind=0.0.0.0:5000", "deploy:app"]
EXPOSE 5000

ENTRYPOINT ["gunicorn", "--workers=4", "--log-level", "debug", "--bind=0.0.0.0:5000", "deploy:app"]
7 changes: 7 additions & 0 deletions deploy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
cur_makefile_path := $(dir $(mkfile_path))
build:
docker build $(cur_makefile_path)\.. -f $(cur_makefile_path)\Dockerfile -t multi-benchmark --progress=plain

run:
docker run --name multi-benchmark_test -p 5000:5000 -v $(cur_makefile_path)/../data:/work/data -d multi-benchmark
Binary file modified deploy/requirements.txt
Binary file not shown.
66 changes: 66 additions & 0 deletions deploy/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch File Example</title>
</head>
<body>
<h1>Upload and Send File Example</h1>

<input type="file" id="fileInput">
<button id="sendButton">Send File</button>

<script>
document.getElementById('sendButton').addEventListener('click', function() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];

if (file) {
const reader = new FileReader();

reader.onload = function(event) {
const fileContent = event.target.result;
const jsonData = { fileContent: fileContent };

fetch('http://127.0.0.1:5000/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
// 如果有需要,可以根据后端要求设置其他headers
},
body: JSON.stringify({
"prediction_json": jsonData
})
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'result.zip'; // 下载文件的名称
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
};

reader.readAsText(file);
} else {
console.log('Please select a file to upload.');
}
});



</script>
</body>
</html>
27 changes: 14 additions & 13 deletions eval/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,24 @@ def parse_args_for_score():
return args

def parse_args_for_score_deploy():
parser = argparse.ArgumentParser()
class Args:
pass

# data setting
parser.add_argument('--label_file', type=str, default=None, help='Specify the label json file.')
parser.add_argument('--prediction_dir', type=str, default=None, help='Specify the prediction json files dir.')
parser.add_argument('--prediction_file', type=str, default=None, help='Specify the prediction json file.')
parser.add_argument('--score_file', type=str, default=None, help='Specify the output detail score file.')
parser.add_argument('--reference_dir', type=str, default=None, help='Specify the reference directory where all the other score files are stored. By leaving it empty, only absolute score will be calculated.')

args = Args()
args.label_file = None
args.prediction_dir = None
args.prediction_file = None
args.score_file = None
args.reference_dir = None

# score setting
parser.add_argument('--detail', action='store_true', help='Whether to print the detail of the score.')
parser.add_argument('--only_past', action='store_true', help='Whether to only use the earlier models to calculate the relative score.')

args.detail = False
args.only_past = False
# other functions
parser.add_argument('--model_list', '-l', action='store_true', help='Print the available model list.')

args = parser.parse_args()
args.model_list = False

return args


Expand Down
80 changes: 60 additions & 20 deletions eval/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,87 @@
import uuid
from metrics import *
from args import parse_args_for_score_deploy

from flask import Flask, request, jsonify


from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import io
from contextlib import redirect_stdout
import zipfile

# redirect stdout to string
output = io.StringIO()
app = Flask(__name__)
CORS(app)


args = parse_args_for_score_deploy()
# args.label_file = "../data/problem_v1.2.2_20240212.json"
args.label_file = "../data/problem_final.json"
args.detail = True
args.prediction_dir = "../results"
source_suffix = "/source"
prediction_file_suffix = "/prediction.json"
paras_file_suffix = "/paras.json"
result_zip_suffix = "/result.zip"


prediction_file_suffix = "/prediction.json"
result_file_suffix = "/result.json"
def get_random_dir():
return os.path.join(args.prediction_dir, str(uuid.uuid4()))


def save_prediction_json_to_random_dir(json_data):
random_dir = os.path.join(args.prediction_dir, str(uuid.uuid4()))
os.makedirs(random_dir)
with open(random_dir + prediction_file_suffix, "w") as f:
def save_prediction_json_to_target_dir(json_data, target_dir):
os.makedirs(target_dir)
with open(target_dir + prediction_file_suffix, "w") as f:
json.dump(json_data, f)
return random_dir


def save_result_to_target_dir(result, target_dir):
with open(target_dir + result_file_suffix, "w") as f:
json.dump(result, f)
def save_paras_to_target_dir(paras_data, target_dir):
with open(target_dir + paras_file_suffix, "w") as f:
json.dump(paras_data, f)


def zip_dir(directory, zip_filename):
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
zipf.write(file_path, os.path.relpath(file_path, directory))


# generate all


@app.route("/generate", methods=["POST"])
def generate():
prediction_json = request.json["prediction_json"]
prediction_json = request.json.get("prediction_json", None)
if prediction_json is None:
return jsonify({"result": "false"})
prediction_file = save_prediction_json_to_random_dir(prediction_json)
result_json = request.json
save_result_to_target_dir(result_json, prediction_file)

# generate random dir
random_dir = get_random_dir()
source_dir = random_dir + source_suffix

# save prediction json to random dir
save_prediction_json_to_target_dir(json.loads(
prediction_json["fileContent"]), source_dir)
prediction_file = source_dir + prediction_file_suffix

# save request json too
paras_json = request.json
save_paras_to_target_dir(paras_json, source_dir)

# change the args
copy_args = copy.deepcopy(args)
copy_args.prediction_file = prediction_file
main(copy_args)
return jsonify({"result": "true"})

# run the main function
with redirect_stdout(output):
main(copy_args)
app.logger.info(output.getvalue())

# zip the dir
zip_dir(source_dir, random_dir + result_zip_suffix)

return send_file(random_dir + result_zip_suffix, as_attachment=True)


if __name__ == "__main__":
Expand Down

0 comments on commit ac33d14

Please sign in to comment.