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

Refactor API response handling to include similarity attributes in output

Browse files
Files changed (2) hide show
  1. app.py +11 -3
  2. helper.py +13 -10
app.py CHANGED
@@ -180,11 +180,19 @@ def run_pred_pipeline(input: PredictionInput):
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")
 
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
+ temp_predictions_dict, sample_sim_attr = process_api_response(output_json)
184
+ data_out = {
185
+ "status" : "success",
186
+ "data" : {
187
+ "id": input.dict()['id'],
188
+ "predictions": temp_predictions_dict,
189
+ "similarity": sample_sim_attr
190
+ }
191
+ }
192
  #nb_output = output_json['prediction']
193
  break;
194
+
195
+ return data_out
196
 
197
 
198
  @app.get("/get_prediction_from_databricks")
helper.py CHANGED
@@ -4,7 +4,8 @@ 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", {})
@@ -19,16 +20,18 @@ def process_api_response(json_response):
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 = {
 
4
 
5
  def process_api_response(json_response):
6
  # Use json.loads() to parse the JSON string
7
+ print("Type of json_response : ", type(json_response))
8
+ input_data = json_response
9
 
10
  # Extract predictions and similarity attributes
11
  predictions = input_data.get("predictions", {})
 
20
  sample_sim_attr = sim_attr_df.to_dict()
21
 
22
  # Construct final output dictionary
23
+ return temp_predictions_dict, sample_sim_attr
24
+
25
+ # data_out = {
26
+ # "status": "success",
27
+ # "data": {
28
+ # "id": input_data.get("id", "default_id"), # Assuming an 'id' key exists in the input
29
+ # "predictions": temp_predictions_dict,
30
+ # "similarity": sample_sim_attr
31
+ # }
32
+ # }
33
 
34
+ # return data_out
35
 
36
  def get_sample_similarity_attr():
37
  sample_sim = {