|
| 1 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 2 | +import os |
| 3 | +import json |
| 4 | +import retriever |
| 5 | +import ml_model |
| 6 | +import extractor |
| 7 | + |
| 8 | +hostName = "0.0.0.0" |
| 9 | +serverPort = 8080 |
| 10 | + |
| 11 | +ML_MODEL = os.getenv("ML_MODEL") |
| 12 | + |
| 13 | +DATA_URL = os.getenv("DATA_URL", "https://s3.amazonaws.com/fast-ai-nlp/amazon_review_polarity_csv.tgz") |
| 14 | +OUTPUT_PATH = os.getenv("OUTPUT_PATH", "./amazon_review_polarity_csv.tgz") |
| 15 | +OBJECT_NAME = os.getenv("OBJECT_NAME", "raw/amazon_review_polarity_csv.tgz") |
| 16 | + |
| 17 | +class Executor(BaseHTTPRequestHandler): |
| 18 | + def do_POST(self): |
| 19 | + content_length = int(self.headers['Content-Length']) |
| 20 | + post_data = self.rfile.read(content_length) |
| 21 | + request = json.loads(post_data.decode('utf-8')) |
| 22 | + |
| 23 | + if not "invoke" in self.path: |
| 24 | + self.send_response(404) |
| 25 | + self.end_headers() |
| 26 | + return |
| 27 | + |
| 28 | + try: |
| 29 | + params = request["Params"] |
| 30 | + except: |
| 31 | + params = {} |
| 32 | + try: |
| 33 | + func = request["Function"] |
| 34 | + except: |
| 35 | + func = None |
| 36 | + |
| 37 | + if "context" in os.environ: |
| 38 | + context = json.loads(os.environ["CONTEXT"]) |
| 39 | + else: |
| 40 | + context = {} |
| 41 | + |
| 42 | + print(f"Function: {func}") |
| 43 | + print(f"Params: {params}") |
| 44 | + print(f"Context: {context}") |
| 45 | + |
| 46 | + response = {} |
| 47 | + try: |
| 48 | + if func is None: |
| 49 | + raise Exception("function not defined!") |
| 50 | + |
| 51 | + if func == "retrieve": |
| 52 | + ''' Invocation example: |
| 53 | + |
| 54 | + POST localhost:8080/invoke |
| 55 | + { |
| 56 | + "Function" : "retrieve", |
| 57 | + "Params" : { |
| 58 | + "data_url": "https://s3.amazonaws.com/fast-ai-nlp/amazon_review_polarity_csv.tgz", |
| 59 | + "local_dir": "./amazon_review_polarity_csv.tgz", |
| 60 | + "object_name": "raw/amazon_review_polarity_csv.tgz" |
| 61 | + } |
| 62 | + } |
| 63 | + ''' |
| 64 | + print(params) |
| 65 | + data_url = str(params.get("data_url", DATA_URL)) |
| 66 | + local_temp_dir = str(params.get("local_dir", OUTPUT_PATH)) |
| 67 | + data_object_name = str(params.get("object_name", OBJECT_NAME)) |
| 68 | + |
| 69 | + print(f"Running function 'retriever' with params {data_url}, {local_temp_dir}, {data_object_name}") |
| 70 | + result = retriever.handler(data_url=data_url, local_temp_path=local_temp_dir, object_name=data_object_name) |
| 71 | + # result = True |
| 72 | + elif func == "train": |
| 73 | + ''' Invocation example: |
| 74 | + |
| 75 | + POST localhost:8080/invoke |
| 76 | + { |
| 77 | + "Function" : "train", |
| 78 | + "Params" : { |
| 79 | + "subset": 0.001, |
| 80 | + "max_features": 2, |
| 81 | + "train_object_data": "data/train.csv", |
| 82 | + "local_train_file": "train.csv", |
| 83 | + "local_model_file": "sentiment_model.pkl", |
| 84 | + "local_vectorizer_file": "tfidf_vectorizer.pkl", |
| 85 | + "output_model_object": "model/sentiment_model.pkl", |
| 86 | + "output_vectorizer_object": "model/tfidf_vectorizer.pkl" |
| 87 | + } |
| 88 | + } |
| 89 | + ''' |
| 90 | + print(f"Running function 'handle_train' with params {params}, {context}") |
| 91 | + result = ml_model.handler_train(params, context) |
| 92 | + |
| 93 | + elif func == "evaluate": |
| 94 | + ''' Invocation example: |
| 95 | + |
| 96 | + POST localhost:8080/invoke |
| 97 | + { |
| 98 | + "Function" : "evaluate", |
| 99 | + "Params" : { |
| 100 | + "test_object_data": "data/test.csv", |
| 101 | + "local_test_file": "test.csv", |
| 102 | + "subset": 0.0002, |
| 103 | + "local_model_file": "sentiment_model.pkl", |
| 104 | + "local_vectorizer_file": "tfidf_vectorizer.pkl", |
| 105 | + "input_model_object": "model/sentiment_model.pkl", |
| 106 | + "input_vectorizer_object": "model/tfidf_vectorizer.pkl" |
| 107 | + } |
| 108 | + } |
| 109 | + ''' |
| 110 | + print(f"Running function 'handle_evaluate' with params {params}, {context}") |
| 111 | + result = ml_model.handler_evaluate(params, context) |
| 112 | + |
| 113 | + elif func == "extract": |
| 114 | + ''' Invocation example: |
| 115 | + |
| 116 | + POST localhost:8080/invoke |
| 117 | + { |
| 118 | + "Function" : "extract", |
| 119 | + "Params" : { |
| 120 | + "tgz_input_object_name": "data/test.csv", |
| 121 | + "subset" : 0.002, |
| 122 | + "local_dataset_file": "./amazon_review_polarity_csv.tgz", |
| 123 | + "local_output_dir": "./data", |
| 124 | + "output_train_object_name": "data/train.csv", |
| 125 | + "output_test_object_name": "data/test.csv" |
| 126 | + } |
| 127 | + } |
| 128 | + ''' |
| 129 | + print(f"Running function 'extract' with params {params}, {context}") |
| 130 | + result = extractor.handler(params, context) |
| 131 | + |
| 132 | + else: |
| 133 | + raise Exception("Unsupported function") |
| 134 | + |
| 135 | + response["Result"] = json.dumps(result) |
| 136 | + response["Success"] = True |
| 137 | + except Exception as e: |
| 138 | + print(e) |
| 139 | + response["Success"] = False |
| 140 | + response["Error"] = str(e) |
| 141 | + |
| 142 | + self.send_response(200) |
| 143 | + self.send_header("Content-type", "application/json") |
| 144 | + self.end_headers() |
| 145 | + self.wfile.write(bytes(json.dumps(response), "utf-8")) |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + print("Launching HTTP Server... ") |
| 151 | + srv = HTTPServer((hostName, serverPort), Executor) |
| 152 | + try: |
| 153 | + print("Running server ... ") |
| 154 | + srv.serve_forever() |
| 155 | + except KeyboardInterrupt: |
| 156 | + pass |
| 157 | + srv.server_close() |
| 158 | + |
0 commit comments