Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def health_check():
return jsonify({'status': 'ok'}), 200

# Route for validating calibration


@app.route("/api/session/calib_validation", methods=["POST"])
def calib_validation():
"""
Expand All @@ -72,8 +74,16 @@ def calib_validation():
return session_route.calib_results()
return Response('Invalid request method for route', status=405, mimetype='application/json')


@app.route('/api/session/batch_predict', methods=['POST'])
def batch_predict():
if request.method == 'POST':
return session_route.batch_predict()
return Response('Invalid request method for route', status=405, mimetype='application/json')
return session_route.batch_predict()


@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok"}), 200


if __name__ == "__main__":
app.run(debug=True, host="127.0.0.1", port=5001)
34 changes: 10 additions & 24 deletions app/routes/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import math
import numpy as np


from pathlib import Path
import os
import pandas as pd
Expand Down Expand Up @@ -35,10 +36,10 @@
def convert_nan_to_none(obj):
"""
Recursively converts NaN and Inf values to None for proper JSON serialization.

Args:
obj: Python object (dict, list, float, etc.)

Returns:
The object with NaN/Inf values converted to None
"""
Expand All @@ -57,8 +58,6 @@ def convert_nan_to_none(obj):
return obj




def calib_results():
from_ruxailab = json.loads(request.form['from_ruxailab'])
file_name = json.loads(request.form['file_name'])
Expand Down Expand Up @@ -110,7 +109,8 @@ def calib_results():
f"{Path().absolute()}/app/services/calib_validation/csv/data/", exist_ok=True
)
predict_csv_file = f"{Path().absolute()}/app/services/calib_validation/csv/data/{file_name}_predict_train_data.csv"
csv_columns = ["left_iris_x", "left_iris_y", "right_iris_x", "right_iris_y"]
csv_columns = ["left_iris_x", "left_iris_y",
"right_iris_x", "right_iris_y"]
try:
with open(predict_csv_file, "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
Expand Down Expand Up @@ -147,6 +147,7 @@ def calib_results():
data = convert_nan_to_none(data)
return Response(json.dumps(data), status=200, mimetype='application/json')


def batch_predict():
try:
data = request.get_json()
Expand All @@ -162,35 +163,20 @@ def batch_predict():

base_path = Path().absolute() / "app/services/calib_validation/csv/data"
calib_csv_path = base_path / f"{calib_id}_fixed_train_data.csv"
predict_csv_path = base_path / "temp_batch_predict.csv"

# CSV temporário
with open(predict_csv_path, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=[
"left_iris_x", "left_iris_y", "right_iris_x", "right_iris_y"
])
writer.writeheader()
for item in iris_data:
writer.writerow({
"left_iris_x": item["left_iris_x"],
"left_iris_y": item["left_iris_y"],
"right_iris_x": item["right_iris_x"],
"right_iris_y": item["right_iris_y"],
})
df_predict = pd.DataFrame(iris_data)

result = gaze_tracker.predict_new_data_simple(
calib_csv_path=calib_csv_path,
predict_csv_path=predict_csv_path,
predict_df=df_predict,
iris_data=iris_data,
# model_X="Random Forest Regressor",
# model_Y="Random Forest Regressor",
screen_width=screen_width,
screen_height=screen_height,
)

return jsonify(convert_nan_to_none(result))

except Exception as e:
print("Erro batch_predict:", e)

traceback.print_exc()
return Response("Erro interno", status=500)
return Response("Erro interno", status=500)
Loading