|
3 | 3 | import logging |
4 | 4 | import re |
5 | 5 | from flask import Flask |
6 | | -from flask import request |
| 6 | +from flask import request, escape |
7 | 7 | from joblib import dump, load |
8 | 8 | import numpy as np |
9 | 9 | import os |
@@ -73,37 +73,34 @@ def endpoint_ping(): |
73 | 73 | # Create a path for inference |
74 | 74 | @app.route("/invocations", methods=["POST"]) |
75 | 75 | def endpoint_invocations(): |
76 | | - try: |
77 | | - logger.info(f"Processing request: {request.headers}") |
78 | | - logger.debug(f"Payload: {request.headers}") |
79 | | - |
80 | | - if request.content_type not in SUPPORTED_REQUEST_MIMETYPES: |
81 | | - logger.error(f"Unsupported Content-Type specified: {request.content_type}") |
82 | | - return f"Invalid Content-Type. Supported Content-Types: {', '.join(SUPPORTED_REQUEST_MIMETYPES)}" |
83 | | - elif request.content_type == "text/csv": |
84 | | - # Step 1: Decode payload into input format expected by model |
85 | | - data = request.get_data().decode("utf8") |
86 | | - # Step 2: Perform inference with the loaded model |
87 | | - predictions = model.predict_from_csv(data) |
88 | | - elif request.content_type == "application/json": |
89 | | - data = request.get_data().decode("utf8") |
90 | | - predictions = model.predict_from_json(data) |
91 | | - elif request.content_type == "application/jsonlines": |
92 | | - data = request.get_data().decode("utf8") |
93 | | - predictions = model.predict_from_jsonlines(data) |
94 | | - |
95 | | - # Step 3: Process predictions into the specified response type (if specified) |
96 | | - response_mimetype = request.accept_mimetypes.best_match( |
97 | | - SUPPORTED_RESPONSE_MIMETYPES, default="application/json" |
98 | | - ) |
99 | | - |
100 | | - if response_mimetype == "text/csv": |
101 | | - response = "\n".join(predictions) |
102 | | - elif response_mimetype == "application/jsonlines": |
103 | | - response = "\n".join([json.dumps({"class": pred}) for pred in predictions]) |
104 | | - elif response_mimetype == "application/json": |
105 | | - response = json.dumps({"predictions": [{"class": pred} for pred in predictions]}) |
106 | | - |
107 | | - return response |
108 | | - except Exception as e: |
109 | | - return f"Error during model invocation: {str(e)} for input: {request.get_data()}" |
| 76 | + logger.info(f"Processing request: {request.headers}") |
| 77 | + logger.debug(f"Payload: {request.headers}") |
| 78 | + |
| 79 | + if request.content_type not in SUPPORTED_REQUEST_MIMETYPES: |
| 80 | + logger.error(f"Unsupported Content-Type specified: {request.content_type}") |
| 81 | + return f"Invalid Content-Type. Supported Content-Types: {', '.join(SUPPORTED_REQUEST_MIMETYPES)}" |
| 82 | + elif request.content_type == "text/csv": |
| 83 | + # Step 1: Decode payload into input format expected by model |
| 84 | + data = request.get_data().decode("utf8") |
| 85 | + # Step 2: Perform inference with the loaded model |
| 86 | + predictions = model.predict_from_csv(data) |
| 87 | + elif request.content_type == "application/json": |
| 88 | + data = request.get_data().decode("utf8") |
| 89 | + predictions = model.predict_from_json(data) |
| 90 | + elif request.content_type == "application/jsonlines": |
| 91 | + data = request.get_data().decode("utf8") |
| 92 | + predictions = model.predict_from_jsonlines(data) |
| 93 | + |
| 94 | + # Step 3: Process predictions into the specified response type (if specified) |
| 95 | + response_mimetype = request.accept_mimetypes.best_match( |
| 96 | + SUPPORTED_RESPONSE_MIMETYPES, default="application/json" |
| 97 | + ) |
| 98 | + |
| 99 | + if response_mimetype == "text/csv": |
| 100 | + response = "\n".join(predictions) |
| 101 | + elif response_mimetype == "application/jsonlines": |
| 102 | + response = "\n".join([json.dumps({"class": pred}) for pred in predictions]) |
| 103 | + elif response_mimetype == "application/json": |
| 104 | + response = json.dumps({"predictions": [{"class": pred} for pred in predictions]}) |
| 105 | + |
| 106 | + return response |
0 commit comments