binaychandra commited on
Commit
2bfc17a
·
1 Parent(s): 86d39c1

Implement API response processing and refactor prediction output handling

Browse files
Files changed (2) hide show
  1. app.py +15 -91
  2. helper.py +29 -0
app.py CHANGED
@@ -11,7 +11,7 @@ from pydantic import BaseModel
11
  from fastapi import Query
12
  from transformers import pipeline
13
 
14
- from helper import generate_random_predictions, get_sample_similarity_attr
15
 
16
  app = FastAPI()
17
 
@@ -113,95 +113,18 @@ def run_pred_pipeline(input: PredictionInput):
113
 
114
  ## Hardcoding for testing purposes ##
115
 
116
- temp_predictions_dict = generate_random_predictions()
117
- sample_sim_attr = get_sample_similarity_attr()
118
- data_out = {
119
- "status" : "success",
120
- "data" : {
121
- "id": input.dict()['id'],
122
- "predictions": temp_predictions_dict,
123
- "similarity": sample_sim_attr
124
- # "predictions": {
125
- # "ASDA": {
126
- # "Apr-25": 741.86,
127
- # "May-25": 2624.14,
128
- # "Jun-25": 808.83,
129
- # "Jul-25": 923.99,
130
- # "Aug-25": 280.57,
131
- # "Sep-25": 13.72,
132
- # "Oct-25": 20.58,
133
- # "Nov-25": 23.9,
134
- # "Dec-25": 1619.17,
135
- # "Jan-26": 1123.3,
136
- # "Feb-26": 235.05,
137
- # "Mar-26": 162.03,
138
- # "Apr-26": 410.15
139
- # },
140
- # "MORRISONS": {
141
- # "Apr-25": 2331.82,
142
- # "May-25": 12573.63,
143
- # "Jun-25": 8536.11,
144
- # "Jul-25": 11987.12,
145
- # "Aug-25": 7898.69,
146
- # "Sep-25": 6396.44,
147
- # "Oct-25": 6263.68,
148
- # "Nov-25": 4706.39,
149
- # "Dec-25": 4583.83,
150
- # "Jan-26": 5898.89,
151
- # "Feb-26": 4337.92,
152
- # "Mar-26": 6339.77,
153
- # "Apr-26": 5191.83
154
- # },
155
- # "SAINSBURYS": {
156
- # "Apr-25": 392.79,
157
- # "May-25": 4353.46,
158
- # "Jun-25": 2627.94,
159
- # "Jul-25": 3361.95,
160
- # "Aug-25": 5763.03,
161
- # "Sep-25": 2985.44,
162
- # "Oct-25": 3457.49,
163
- # "Nov-25": 2631.01,
164
- # "Dec-25": 2645.14,
165
- # "Jan-26": 3034.98,
166
- # "Feb-26": 2958.94,
167
- # "Mar-26": 4043.73,
168
- # "Apr-26": 3364.26
169
- # },
170
- # "TESCO": {
171
- # "Apr-25": 2302.79,
172
- # "May-25": 18921.9,
173
- # "Jun-25": 17958.08,
174
- # "Jul-25": 18710.57,
175
- # "Aug-25": 13609.1,
176
- # "Sep-25": 18693.05,
177
- # "Oct-25": 21091.39,
178
- # "Nov-25": 18796.81,
179
- # "Dec-25": 21114.51,
180
- # "Jan-26": 20039.52,
181
- # "Feb-26": 21608.5,
182
- # "Mar-26": 22534.27,
183
- # "Apr-26": 16405.85
184
- # },
185
- # "TOTAL_MARKET": {
186
- # "Apr-25": 10964.68,
187
- # "May-25": 77262.14,
188
- # "Jun-25": 62432.31,
189
- # "Jul-25": 76078.74,
190
- # "Aug-25": 52031.48,
191
- # "Sep-25": 47737.41,
192
- # "Oct-25": 51094.34,
193
- # "Nov-25": 42181.84,
194
- # "Dec-25": 47680.7,
195
- # "Jan-26": 50010.67,
196
- # "Feb-26": 46154.89,
197
- # "Mar-26": 49339.0,
198
- # "Apr-26": 39747.65
199
- # }
200
- # }
201
- }
202
- }
203
 
204
- return data_out
205
 
206
  print(f"Here is the input dict : {input.dict()}")
207
  print(f"Running the pipeline : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ")
@@ -257,10 +180,11 @@ def run_pred_pipeline(input: PredictionInput):
257
  payload = dict(run_id=task_run_id)
258
  response = requests.get(api_url, headers=headers, data=json.dumps(payload))
259
  output_json = json.loads(response.json()['notebook_output']['result'])
 
260
  #nb_output = output_json['prediction']
261
  break;
262
-
263
- return output_json
264
 
265
 
266
  @app.get("/get_prediction_from_databricks")
 
11
  from fastapi import Query
12
  from transformers import pipeline
13
 
14
+ from helper import generate_random_predictions, get_sample_similarity_attr, process_api_response
15
 
16
  app = FastAPI()
17
 
 
113
 
114
  ## Hardcoding for testing purposes ##
115
 
116
+ # temp_predictions_dict = generate_random_predictions()
117
+ # sample_sim_attr = get_sample_similarity_attr()
118
+ # data_out = {
119
+ # "status" : "success",
120
+ # "data" : {
121
+ # "id": input.dict()['id'],
122
+ # "predictions": temp_predictions_dict,
123
+ # "similarity": sample_sim_attr
124
+ # }
125
+ # }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ # return data_out
128
 
129
  print(f"Here is the input dict : {input.dict()}")
130
  print(f"Running the pipeline : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ")
 
180
  payload = dict(run_id=task_run_id)
181
  response = requests.get(api_url, headers=headers, data=json.dumps(payload))
182
  output_json = json.loads(response.json()['notebook_output']['result'])
183
+ output_dict = process_api_response(output_json)
184
  #nb_output = output_json['prediction']
185
  break;
186
+ print()
187
+ return output_dict
188
 
189
 
190
  @app.get("/get_prediction_from_databricks")
helper.py CHANGED
@@ -1,5 +1,34 @@
1
  import pandas as pd
2
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def get_sample_similarity_attr():
5
  sample_sim = {
 
1
  import pandas as pd
2
  import numpy as np
3
+ import json
4
+
5
+ def process_api_response(json_response):
6
+ # Use json.loads() to parse the JSON string
7
+ input_data = json.loads(json_response)
8
+
9
+ # Extract predictions and similarity attributes
10
+ predictions = input_data.get("predictions", {})
11
+ similarity_attr = input_data.get("similarity_attr", {})
12
+
13
+ # Convert predictions into a DataFrame and back to a dictionary
14
+ temp_predictions_df = pd.DataFrame(predictions).T
15
+ temp_predictions_dict = temp_predictions_df.to_dict()
16
+
17
+ # Convert similarity attributes into a DataFrame and back to a dictionary
18
+ sim_attr_df = pd.DataFrame(similarity_attr).T
19
+ sample_sim_attr = sim_attr_df.to_dict()
20
+
21
+ # Construct final output dictionary
22
+ data_out = {
23
+ "status": "success",
24
+ "data": {
25
+ "id": input_data.get("id", "default_id"), # Assuming an 'id' key exists in the input
26
+ "predictions": temp_predictions_dict,
27
+ "similarity": sample_sim_attr
28
+ }
29
+ }
30
+
31
+ return data_out
32
 
33
  def get_sample_similarity_attr():
34
  sample_sim = {