jnyanzi commited on
Commit
e7e5ee8
1 Parent(s): 5f1210b

initial upload

Browse files
Files changed (3) hide show
  1. app.py +113 -0
  2. model.joblib +3 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import the libraries
2
+ import os
3
+ import uuid
4
+ import joblib
5
+ import json
6
+
7
+ import gradio as gr
8
+ import pandas as pd
9
+
10
+ from huggingface_hub import CommitScheduler
11
+ from pathlib import Path
12
+
13
+
14
+ # Run the training script placed in the same directory as app.py
15
+ # The training script will train and persist a linear regression
16
+ # model with the filename 'model.joblib'
17
+
18
+
19
+
20
+ # Load the freshly trained model from disk
21
+ predictor = joblib.load('model.joblib')
22
+
23
+ # Prepare the logging functionality
24
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
25
+ log_folder = log_file.parent
26
+
27
+ scheduler = CommitScheduler(
28
+ repo_id="Insurance_charge_logs", # provide a name "insurance-charge-mlops-logs" for the repo_id
29
+ repo_type="dataset",
30
+ folder_path=log_folder,
31
+ path_in_repo="data",
32
+ every=2
33
+ )
34
+
35
+ # Define the predict function which will take features, convert to dataframe and make predictions using the saved model
36
+ # the functions runs when 'Submit' is clicked or when a API request is made
37
+
38
+ age_input = gr.Number(label='Age')
39
+ sex_input = gr.Dropdown(
40
+ ['female', 'male'],
41
+ label='Gender'
42
+ )
43
+ bmi_input = gr.Number(label='Body Mass Index (BMI)')
44
+ children_input = gr.Number(label='Number of Children')
45
+ smoker_input = gr.Dropdown(
46
+ ['no', 'yes'],
47
+ label='Smoker'
48
+ )
49
+ region_input = gr.Dropdown(
50
+ ['northeast', 'northwest', 'southeast', 'southwest'],
51
+ label='Region'
52
+ )
53
+
54
+ model_output = gr.Label(label="Charges")
55
+
56
+
57
+
58
+ # While the prediction is made, log both the inputs and outputs to a log file
59
+ # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
60
+ # access
61
+ def predict_insurance_charge(age, sex, bmi, children, smoker, region):
62
+ sample = {
63
+ 'age': age,
64
+ 'sex': sex,
65
+ 'bmi': bmi,
66
+ 'children': children,
67
+ 'smoker': smoker,
68
+ 'region': region,
69
+ }
70
+ data_point = pd.DataFrame([sample])
71
+ prediction = predictor.predict(data_point).tolist()
72
+
73
+
74
+ with scheduler.lock:
75
+ with log_file.open("a") as f:
76
+ f.write(json.dumps(
77
+ {
78
+ 'age': age,
79
+ 'sex': sex,
80
+ 'bmi': bmi,
81
+ 'children': children,
82
+ 'smoker': smoker,
83
+ 'region': region,
84
+ 'prediction': prediction[0]
85
+ }
86
+ ))
87
+ f.write("\n")
88
+
89
+ return prediction[0]
90
+
91
+
92
+
93
+ # Set up UI components for input and output
94
+ # Create the gradio interface, make title "HealthyLife Insurance Charge Prediction"
95
+
96
+ demo = gr.Interface(
97
+ fn=predict_insurance_charge,
98
+ inputs=[age_input,
99
+ sex_input,
100
+ bmi_input,
101
+ children_input,
102
+ smoker_input,
103
+ region_input],
104
+ outputs=model_output,
105
+ title="HealthyLife Insurance Charge Prediction",
106
+ description="This API allows you to predict insurance charges for an individual",
107
+ allow_flagging="auto",
108
+ concurrency_limit=8
109
+ )
110
+
111
+ # Launch with a load balancer
112
+ demo.queue()
113
+ demo.launch(share=False)
model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4be404dcd680365b139a4307d6f05d71e7b5ed6553303275f53990e17b6135ef
3
+ size 3998
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ scikit-learn==1.2.2
2
+ numpy==1.25.2
3
+ joblib