Files changed (1) hide show
  1. app.py +64 -46
app.py CHANGED
@@ -3,7 +3,6 @@ import os
3
  import uuid
4
  import joblib
5
  import json
6
-
7
  import gradio as gr
8
  import pandas as pd
9
 
@@ -11,7 +10,6 @@ from huggingface_hub import CommitScheduler
11
  from pathlib import Path
12
 
13
 
14
- # Preparing the logging functionality
15
  log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
16
  log_folder = log_file.parent
17
 
@@ -21,67 +19,87 @@ scheduler = CommitScheduler(
21
  folder_path=log_folder,
22
  path_in_repo="data",
23
  every=2
24
- )
 
 
 
 
 
25
 
26
- charges_predictor = joblib.load('model.joblib')
 
 
27
 
28
- age_input = gr.Number(label='Age')
29
- bmi_input = gr.Number(label='BMI')
30
- children_input = gr.Number(label='Children')
31
- sex_input = gr.Dropdown(['male', 'female', 'N/A'], value='N/A', label='Sex')
32
- smoker_input = gr.Dropdown(['yes', 'no', 'N/A'], value='N/A', label="Smoker")
33
- region_input = gr.Dropdown(['southeast', 'southwest', 'northeast', 'northwest', 'N/A'],
34
- value='N/A', label='Region')
 
 
 
 
 
 
 
 
 
 
35
 
36
 
37
- model_output = gr.Label(label='Charges')
 
 
 
38
 
39
- # The function runs when 'Submit' is clicked or when a API request is made
40
- def predict_charges(age, bmi, children, sex, smoker, region, prediction):
41
  sample = {
42
- 'age': age,
43
- 'bmi': bmi,
44
- 'children': children,
45
- 'sex': sex,
46
- 'smoker': smoker,
47
- 'region': region,
48
- 'prediction': prediction
49
  }
 
50
  data_point = pd.DataFrame([sample])
51
- print('data point: ', data_point)
52
- prediction = charges_predictor.predict(data_point).tolist()
53
 
 
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 prediction[0]
71
-
72
-
73
- # Setting up UI components for input and output
74
  demo = gr.Interface(
75
  fn=predict_charges,
76
- inputs=[age_input, bmi_input,
77
- children_input, sex_input, smoker_input, region_input],
 
 
 
 
 
 
78
  outputs=model_output,
79
  title="HealthyLife Insurance Charge Prediction",
80
- description="This API allows you to predict the appropiate charges for each patient",
81
  flagging_mode="manual",
82
  concurrency_limit=8
83
  )
84
 
85
- # Launch with a load balancer
86
  demo.queue()
87
- demo.launch(share=False)
 
3
  import uuid
4
  import joblib
5
  import json
 
6
  import gradio as gr
7
  import pandas as pd
8
 
 
10
  from pathlib import Path
11
 
12
 
 
13
  log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
14
  log_folder = log_file.parent
15
 
 
19
  folder_path=log_folder,
20
  path_in_repo="data",
21
  every=2
22
+ )
23
+
24
+ charges_predictor = joblib.load("model.joblib")
25
+
26
+
27
+ # Inputs
28
 
29
+ age_input = gr.Number(label="Age", value=25)
30
+ bmi_input = gr.Number(label="BMI", value=25)
31
+ children_input = gr.Number(label="Children", value=0)
32
 
33
+ sex_input = gr.Dropdown(
34
+ ["male", "female"],
35
+ value="male",
36
+ label="Sex"
37
+ )
38
+
39
+ smoker_input = gr.Dropdown(
40
+ ["yes", "no"],
41
+ value="no",
42
+ label="Smoker"
43
+ )
44
+
45
+ region_input = gr.Dropdown(
46
+ ["southeast", "southwest", "northeast", "northwest"],
47
+ value="southeast",
48
+ label="Region"
49
+ )
50
 
51
 
52
+ model_output = gr.Textbox(label="Predicted Insurance Cost")
53
+
54
+
55
+ def predict_charges(age, bmi, children, sex, smoker, region):
56
 
 
 
57
  sample = {
58
+ "age": age,
59
+ "bmi": bmi,
60
+ "children": children,
61
+ "sex": sex,
62
+ "smoker": smoker,
63
+ "region": region
 
64
  }
65
+
66
  data_point = pd.DataFrame([sample])
 
 
67
 
68
+ prediction = charges_predictor.predict(data_point).tolist()
69
 
70
  with scheduler.lock:
71
+ with log_file.open("a") as f:
72
+ f.write(json.dumps({
73
+ "age": age,
74
+ "bmi": bmi,
75
+ "children": children,
76
+ "sex": sex,
77
+ "smoker": smoker,
78
+ "region": region,
79
+ "prediction": prediction[0]
80
+ }))
81
+ f.write("\n")
82
+
83
+ return f"${prediction[0]:,.2f}"
84
+
85
+
 
 
 
86
  demo = gr.Interface(
87
  fn=predict_charges,
88
+ inputs=[
89
+ age_input,
90
+ bmi_input,
91
+ children_input,
92
+ sex_input,
93
+ smoker_input,
94
+ region_input
95
+ ],
96
  outputs=model_output,
97
  title="HealthyLife Insurance Charge Prediction",
98
+ description="Predict the insurance medical charges based on patient information",
99
  flagging_mode="manual",
100
  concurrency_limit=8
101
  )
102
 
103
+
104
  demo.queue()
105
+ demo.launch()