AceVen57 commited on
Commit
3237b39
1 Parent(s): a8a9139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -65
app.py CHANGED
@@ -1,97 +1,102 @@
1
  # Import the libraries
2
 
3
- import os
4
  import uuid
5
  import joblib
6
  import json
7
 
8
  import gradio as gr
9
- import pandas as pd
 
 
 
10
 
11
  # Run the training script placed in the same directory as app.py
12
- # The training script will train and persist a linear regression
13
  # model with the filename 'model.joblib'
14
 
15
  os.system("python train.py")
16
 
17
-
18
  # Load the freshly trained model from disk
19
 
20
- insurance_charge_predictor = joblib.load("model.joblib")
21
 
22
  # Prepare the logging functionality
23
- # log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
24
- # log_folder = log_file.parent
25
-
26
- # scheduler = CommitScheduler(
27
- # repo_id="insurance-charge-mlops-logs", # provide a name "insurance-charge-mlops-logs" for the repo_id
28
- # repo_type="dataset",
29
- # folder_path=log_folder,
30
- # path_in_repo="data",
31
- # every=2
32
- # )
33
-
34
- # Define the predict function which will take features, convert to dataframe and make predictions using the saved model
35
- # the functions runs when 'Submit' is clicked or when a API request is made
36
- def predict_charge(age, sex, bmi, children, somker, region):
37
- smaple = {'age': age,
38
- 'bmi': bmi,
39
- 'children': children,
40
- 'sex': sex,
41
- 'smoker': smoker,
42
- 'region': region,
43
- 'prediction': prediction[0]
44
- }
45
- data_point = pd.DataFrame([sample])
46
- prediction = insurance_charge_predicter.predict(data_point).tolist()
47
 
48
- # While the prediction is made, log both the inputs and outputs to a log file
49
- # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
50
- # access
51
-
52
- # with scheduler.lock:
53
- # with log_file.open("a") as f:
54
- # f.write(json.dumps(
55
- # {
56
- # 'age': age,
57
- # 'bmi': bmi,
58
- # 'children': children,
59
- # 'sex': sex,
60
- # 'smoker': smoker,
61
- # 'region': region,
62
- # 'prediction': prediction[0]
63
- # }
64
- # ))
65
- # f.write("\n")
66
 
67
- return prediction[0]
 
 
 
 
 
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # Set up UI components for input and output
72
 
73
- age_input = gr.Number(label = "age")
74
- bmi_input = gr.Number(label = "bmi")
75
- children_input = gr.Number(label = "children")
76
- sex_input = gr.Dropdown(["male","female"], label = "sex")
77
- smoker_input = gr.Dropdown(["Yes","No"], label = "smoker")
78
- region_input = gr.Dropdown(["southeast","southwest", "northwest", "northeast"], label = "region")
79
- model_output = gr.Label( label = " Insurance Chaeges")
 
 
80
 
81
- # Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
 
 
82
  demo = gr.Interface(
83
  fn=predict_insurance_charge,
84
- inputs=[age_input, bmi_input, children_input, sex_input, smoker_input, region_input],
85
- outputs = model_output,
86
- title = "Healthy Insurence Candidate Perdiction",
87
- description = "This API will predict and estimate isurance charges based on candidate's attributes",
88
- examples = [[33,33.44,5,"male","no", "southeast"],
89
- [40,38.20,2,"female","no", "northwest"],
90
- [52,36.20,0,"male","no", "northwest"]],
91
- concurrency_limit = 16
92
-
93
  )
94
 
95
  # Launch with a load balancer
96
  demo.queue()
97
  demo.launch(share=False)
 
 
1
  # Import the libraries
2
 
3
+ import os
4
  import uuid
5
  import joblib
6
  import json
7
 
8
  import gradio as gr
9
+ import pandas as pd
10
+
11
+ from huggingface_hub import CommitScheduler
12
+ from pathlib import Path
13
 
14
  # Run the training script placed in the same directory as app.py
15
+ # The training script will train and persist a logistic regression
16
  # model with the filename 'model.joblib'
17
 
18
  os.system("python train.py")
19
 
 
20
  # Load the freshly trained model from disk
21
 
22
+ insurance_charge_predictor = joblib.load('model.joblib')
23
 
24
  # Prepare the logging functionality
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
27
+ log_folder = log_file.parent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ scheduler = CommitScheduler(
30
+ repo_id="insurance-charge-logs",
31
+ repo_type="dataset",
32
+ folder_path=log_folder,
33
+ path_in_repo="data",
34
+ every=2
35
+ )
36
 
37
+ # Define the predict function that runs when 'Submit' is clicked or when a API request is made
38
+ def predict_insurance_charge(age, bmi, children,sex, smoker, region):
39
+ sample = {
40
+ 'age': age,
41
+ 'bmi': bmi,
42
+ 'children': children,
43
+ 'sex': sex,
44
+ 'smoker': smoker,
45
+ 'region': region
46
+ }
47
+
48
+ data_point = pd.DataFrame([sample])
49
+ prediction = insurance_charge_predictor.predict(data_point).tolist()
50
 
51
+ # While the prediction is made, log both the inputs and outputs to a local log file
52
+ # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
53
+ # access
54
+
55
+ with scheduler.lock:
56
+ with log_file.open("a") as f:
57
+ f.write(json.dumps(
58
+ {
59
+ 'age': age,
60
+ 'bmi': bmi,
61
+ 'children': children,
62
+ 'sex': sex,
63
+ 'smoker': smoker,
64
+ 'region': region,
65
+ 'prediction': prediction[0]
66
+ }
67
+ ))
68
+ f.write("\n")
69
+
70
+ return round(prediction[0],2)
71
 
72
  # Set up UI components for input and output
73
 
74
+ age_input = gr.Number(label='age')
75
+ bmi_input = gr.Number(label='bmi')
76
+ children_input = gr.Number(label='children')
77
+ sex_input = gr.Dropdown(['female','male'],label='sex')
78
+ smoker_input = gr.Dropdown(['yes','no'],label='smoker')
79
+ region_input = gr.Dropdown(
80
+ ['southeast', 'southwest', 'northwest', 'northeast'],
81
+ label='region'
82
+ )
83
 
84
+ model_output = gr.Label(label="Insurance Charges")
85
+
86
+ # Create the interface
87
  demo = gr.Interface(
88
  fn=predict_insurance_charge,
89
+ inputs=[age_input, bmi_input, children_input,sex_input, smoker_input, region_input],
90
+ outputs=model_output,
91
+ title="HealthyLife Insurance Charge Prediction",
92
+ description="This API allows you to predict the estimating insurance charges based on customer attributes",
93
+ examples=[[33,33.44,5,'male','no','southeast'],
94
+ [58,25.175,0,'male','no','northeast'],
95
+ [52,38.380,2,'female','no','northeast']],
96
+ concurrency_limit=16
 
97
  )
98
 
99
  # Launch with a load balancer
100
  demo.queue()
101
  demo.launch(share=False)
102
+