tasleem111 commited on
Commit
4b0746f
·
verified ·
1 Parent(s): 90ff9b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import joblib
4
+ import json
5
+
6
+ import gradio as gr
7
+ import pandas as pd
8
+
9
+ from huggingface_hub import CommitScheduler
10
+ from pathlib import Path
11
+
12
+ # Run the training script placed in the same directory as app.py
13
+ # The training script will train and persist a logistic regression
14
+ # model with the filename 'model.joblib'
15
+
16
+ os.system("python train.py")
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
+
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-mlops-logs",
29
+ repo_type="dataset",
30
+ folder_path=log_folder,
31
+ path_in_repo="data",
32
+ every=2
33
+ )
34
+
35
+ # Define the predict function that runs when 'Submit' is clicked or when a API request is made
36
+ def predict_insurance_charge(age, sex, bmi, children, smoker, region):
37
+ sample = {
38
+ 'age': age,
39
+ 'bmi': bmi,
40
+ 'children': children,
41
+ 'sex': sex,
42
+ 'smoker': smoker,
43
+ 'region': region
44
+ }
45
+
46
+ data_point = pd.DataFrame([sample])
47
+ prediction = insurance_charge_predictor.predict(data_point).tolist()
48
+
49
+ # While the prediction is made, log both the inputs and outputs to a local log file
50
+ # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
51
+ # access
52
+
53
+ with scheduler.lock:
54
+ with log_file.open("a") as f:
55
+ f.write(json.dumps(
56
+ {
57
+ 'age': age,
58
+ 'bmi': bmi,
59
+ 'children': children,
60
+ 'sex': sex,
61
+ 'smoker': smoker,
62
+ 'region': region,
63
+ 'prediction': prediction[0]
64
+ }
65
+ ))
66
+ f.write("\n")
67
+
68
+ return prediction[0]
69
+
70
+ # Set up UI components for input and output
71
+
72
+ age_input = gr.Number(label='age')
73
+ bmi_input = gr.Number(label='bmi')
74
+ children_input = gr.Number(label='children')
75
+ sex_input = gr.Dropdown(['male','female'],label='sex')
76
+ smoker_input = gr.Dropdown(['yes','no'],label='smoker')
77
+ region_input = gr.Dropdown(
78
+ ['southeast', 'southwest', 'northwest', 'northeast'],
79
+ label='region'
80
+ )
81
+
82
+ model_output = gr.Label(label="Insurance Charges")
83
+
84
+ # Create the interface
85
+ demo = gr.Interface(
86
+ fn=predict_insurance_charge,
87
+ inputs=[age_input, bmi_input, children_input,sex_input, smoker_input, region_input],
88
+ outputs=model_output,
89
+ title="HealthyLife Insurance Charge Prediction",
90
+ description="This API allows you to predict the estimating insurance charges based on customer attributes",
91
+ examples=[[33,33.44,5,'male','no','southeast'],
92
+ [58,25.175,0,'male','no','northeast'],
93
+ [52,38.380,2,'female','no','northeast']],
94
+ concurrency_limit=16
95
+ )
96
+
97
+ # Launch with a load balancer
98
+ demo.queue()
99
+ demo.launch(share=False)