binaychandra commited on
Commit
4390bf9
·
1 Parent(s): 1d7e89f

added route for prediction

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -59,6 +59,65 @@ def get_prediction_from_jobrun():
59
  print("Status Code:", response.status_code)
60
  return response.text
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  @app.get("/get_prediction_from_databricks")
63
  def run_xpipeline():
64
  print(f"Running the pipeline : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ")
 
59
  print("Status Code:", response.status_code)
60
  return response.text
61
 
62
+ @app.get("/get_prediction_on_userinput")
63
+ def run_pred_pipeline():
64
+ print(f"Running the pipeline : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ")
65
+
66
+ headers = {
67
+ "Authorization": f"Bearer {API_TOKEN}",
68
+ "Content-Type": "application/json"
69
+ }
70
+ # Pipeline details
71
+ pipeline_id = "403360183892362"
72
+ json_data = None
73
+ payload = {
74
+ 'job_id': pipeline_id,
75
+ 'notebook_params': {
76
+ "salesorg_cd": "GB01",
77
+ "category_mdlz": "EUCO",
78
+ "basecode": "GB10002",
79
+ "scenario": "sc_1",
80
+ "week_date": "2025-04-28",
81
+ "level_of_sugar": "STANDARD",
82
+ "pack_group": "CHOC ADULT SGLS",
83
+ "product_range": "MILKA",
84
+ "segment": "CHOC SGLS",
85
+ "supersegment": "STANDARD CHOCOLATE",
86
+ "base_number_in_multipack": "SINGLE",
87
+ "flavour": "CITRUS",
88
+ "choco": "MILK",
89
+ "salty": "NO",
90
+ "weight_per_unit_mdlz": "0.28",
91
+ "list_price_per_unit_mdlz": "1.75"}
92
+ }
93
+
94
+ # Trigger the run
95
+ api_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/run-now"
96
+ response = requests.post(api_url, headers=headers, data=json.dumps(payload))
97
+ response_json = response.json()
98
+ print(f"\nPrediction pipeline started with details : {response_json}\n")
99
+ run_id = response_json["run_id"]
100
+ #pred_out = pd.DataFrame()
101
+ while True:
102
+ time.sleep(2)
103
+ api_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/get?run_id={run_id}"
104
+ response = requests.get(api_url, headers=headers)
105
+ response_json = response.json()
106
+ task_run_id = response_json['tasks'][0]['run_id']
107
+ run_status = response_json["state"]["life_cycle_state"]
108
+ print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Status : {run_status}")
109
+ job_status = response_json["state"].get('result_state')
110
+ if job_status == 'SUCCESS':
111
+ api_url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/get-output"
112
+ payload = dict(run_id=task_run_id)
113
+ response = requests.get(api_url, headers=headers, data=json.dumps(payload))
114
+ output_json = json.loads(response.json()['notebook_output']['result'])
115
+ nb_output = output_json['prediction']
116
+ break;
117
+
118
+ return output_json
119
+
120
+
121
  @app.get("/get_prediction_from_databricks")
122
  def run_xpipeline():
123
  print(f"Running the pipeline : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ")