-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_server.py
executable file
·74 lines (65 loc) · 2.95 KB
/
grpc_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from concurrent import futures
import grpc
import bentoml_service_pb2
import bentoml_service_pb2_grpc
import logging
import pandas as pd
from services.data_broker_service import DataBrokerService
from services.data_prediction_service import DataPredictionService
from services.data_rmse_service import DataRMSEService
class BentoMLServiceServicer(bentoml_service_pb2_grpc.BentoMLServiceServicer):
def __init__(self):
self.data_broker_service = DataBrokerService()
self.data_prediction_service = DataPredictionService()
self.data_rmse_service = DataRMSEService()
def GetDataHead(self, request, context):
logging.info("GetDataHead called")
try:
data_head = self.data_broker_service.get_data_head()
logging.info(f"data_head: {data_head}")
response = bentoml_service_pb2.DataHeadResponse()
for row in data_head:
logging.info(f"Processing row: {row}")
map_entry = response.data_head.add()
for key, value in row.items():
if isinstance(value, pd.Timestamp):
value = value.strftime('%Y-%m-%d %H:%M:%S')
logging.info(f"Adding key: {key}, value: {value}")
map_entry.row[key] = str(value)
return response
except Exception as e:
logging.error(f"Error in GetDataHead: {e}")
context.set_details(str(e))
context.set_code(grpc.StatusCode.INTERNAL)
return bentoml_service_pb2.DataHeadResponse()
def GetPredictions(self, request, context):
predictions = self.data_prediction_service.get_predictions()
return bentoml_service_pb2.PredictionsResponse(predictions=predictions)
def GetRMSE(self, request, context):
rmse = self.data_rmse_service.get_rmse()
return bentoml_service_pb2.RMEResponse(rmse=rmse)
def GetInference(self, request, context):
data_head = self.data_broker_service.get_data_head()
predictions = self.data_prediction_service.get_predictions()
rmse = self.data_rmse_service.get_rmse()
response = bentoml_service_pb2.InferenceResponse()
for row in data_head:
map_entry = response.data_head.add()
for key, value in row.items():
if isinstance(value, pd.Timestamp):
value = value.strftime('%Y-%m-%d %H:%M:%S')
map_entry.row[key] = str(value)
response.predictions.extend(predictions)
response.rmse = rmse
return response
def serve():
logging.basicConfig(level=logging.INFO)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
bentoml_service_pb2_grpc.add_BentoMLServiceServicer_to_server(
BentoMLServiceServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
logging.info("Server started on port 50051")
server.wait_for_termination()
if __name__ == '__main__':
serve()