mennabar4a8 commited on
Commit
cee6701
·
verified ·
1 Parent(s): 8e3dcf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -45
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,7 @@ 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
 
@@ -23,65 +22,87 @@ scheduler = CommitScheduler(
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
+ # logging
14
  log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
15
  log_folder = log_file.parent
16
 
 
22
  every=2
23
  )
24
 
25
+ charges_predictor = joblib.load("model.joblib")
26
+
27
+
28
+ # Inputs
29
+
30
+ age_input = gr.Number(label="Age")
31
+ bmi_input = gr.Number(label="BMI")
32
+ children_input = gr.Number(label="Children")
33
+
34
+ sex_input = gr.Dropdown(
35
+ ["male", "female"],
36
+ label="Sex"
37
+ )
38
+
39
+ smoker_input = gr.Dropdown(
40
+ ["yes", "no"],
41
+ label="Smoker"
42
+ )
43
+
44
+ region_input = gr.Dropdown(
45
+ ["southeast", "southwest", "northeast", "northwest"],
46
+ label="Region"
47
+ )
48
+
49
+
50
+ model_output = gr.Textbox(label="Predicted Insurance Cost")
51
 
 
 
 
 
 
 
 
52
 
53
+ # prediction function
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
+ # logging
71
  with scheduler.lock:
72
+ with log_file.open("a") as f:
73
+ f.write(json.dumps({
74
+ "age": age,
75
+ "bmi": bmi,
76
+ "children": children,
77
+ "sex": sex,
78
+ "smoker": smoker,
79
+ "region": region,
80
+ "prediction": prediction[0]
81
+ }))
82
+ f.write("\n")
83
+
84
+ return f"${prediction[0]:,.2f}"
85
+
86
+
87
+ # UI
88
+
 
89
  demo = gr.Interface(
90
  fn=predict_charges,
91
+ inputs=[
92
+ age_input,
93
+ bmi_input,
94
+ children_input,
95
+ sex_input,
96
+ smoker_input,
97
+ region_input
98
+ ],
99
  outputs=model_output,
100
  title="HealthyLife Insurance Charge Prediction",
101
+ description="Predict the insurance medical charges based on patient information",
102
  flagging_mode="manual",
103
  concurrency_limit=8
104
  )
105
 
106
+
107
  demo.queue()
108
+ demo.launch()