jonathanagustin commited on
Commit
b394efd
1 Parent(s): 3162720

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import requests
3
+ import gradio as gr
4
+
5
+ # Set up logging
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger(__name__)
8
+
9
+ # Configuration for the model
10
+ API_URL = "https://api-inference.huggingface.co/models/aai540-group3/diabetes-readmission"
11
+
12
+ # Define constants for the Gradio interface
13
+ AGE_RANGE = (0, 100)
14
+ TIME_IN_HOSPITAL_RANGE = (1, 14)
15
+ NUM_PROCEDURES_RANGE = (0, 10)
16
+ NUM_MEDICATIONS_RANGE = (0, 20)
17
+ NUMBER_DIAGNOSES_RANGE = (1, 10)
18
+ READMITTED_CHOICES = ["<30", ">30", "NO"]
19
+
20
+ # Define the inference function
21
+ def predict(
22
+ age, time_in_hospital, num_procedures, num_medications, number_diagnoses,
23
+ metformin, repaglinide, nateglinide, chlorpropamide, glimepiride,
24
+ glipizide, glyburide, pioglitazone, rosiglitazone, acarbose, insulin,
25
+ readmitted
26
+ ):
27
+ # Create a dictionary from the input features
28
+ input_data = {
29
+ "age": age,
30
+ "time_in_hospital": time_in_hospital,
31
+ "num_procedures": num_procedures,
32
+ "num_medications": num_medications,
33
+ "number_diagnoses": number_diagnoses,
34
+ "metformin": metformin,
35
+ "repaglinide": repaglinide,
36
+ "nateglinide": nateglinide,
37
+ "chlorpropamide": chlorpropamide,
38
+ "glimepiride": glimepiride,
39
+ "glipizide": glipizide,
40
+ "glyburide": glyburide,
41
+ "pioglitazone": pioglitazone,
42
+ "rosiglitazone": rosiglitazone,
43
+ "acarbose": acarbose,
44
+ "insulin": insulin,
45
+ "readmitted": readmitted
46
+ }
47
+
48
+ try:
49
+ # Make a request to the Hugging Face inference API
50
+ response = requests.post(API_URL, json={"inputs": input_data})
51
+ response.raise_for_status() # Raise an error for bad responses
52
+
53
+ prediction = response.json()
54
+ logger.info(f"Prediction received: {prediction}")
55
+ return f"<h1 style='font-size: 48px; color: green;'>Prediction: {prediction}</h1>"
56
+
57
+ except requests.exceptions.RequestException as e:
58
+ logger.error(f"Error in API request: {e}")
59
+ return "<h1 style='font-size: 48px; color: red;'>Error in prediction</h1>"
60
+
61
+ # Create Gradio interface
62
+ iface = gr.Interface(
63
+ fn=predict,
64
+ inputs=[
65
+ gr.Slider(minimum=AGE_RANGE[0], maximum=AGE_RANGE[1], label="Age"),
66
+ gr.Slider(minimum=TIME_IN_HOSPITAL_RANGE[0], maximum=TIME_IN_HOSPITAL_RANGE[1], label="Time in Hospital (days)"),
67
+ gr.Slider(minimum=NUM_PROCEDURES_RANGE[0], maximum=NUM_PROCEDURES_RANGE[1], label="Number of Procedures"),
68
+ gr.Slider(minimum=NUM_MEDICATIONS_RANGE[0], maximum=NUM_MEDICATIONS_RANGE[1], label="Number of Medications"),
69
+ gr.Slider(minimum=NUMBER_DIAGNOSES_RANGE[0], maximum=NUMBER_DIAGNOSES_RANGE[1], label="Number of Diagnoses"),
70
+ gr.Checkbox(label="Metformin"),
71
+ gr.Checkbox(label="Repaglinide"),
72
+ gr.Checkbox(label="Nateglinide"),
73
+ gr.Checkbox(label="Chlorpropamide"),
74
+ gr.Checkbox(label="Glimepiride"),
75
+ gr.Checkbox(label="Glipizide"),
76
+ gr.Checkbox(label="Glyburide"),
77
+ gr.Checkbox(label="Pioglitazone"),
78
+ gr.Checkbox(label="Rosiglitazone"),
79
+ gr.Checkbox(label="Acarbose"),
80
+ gr.Checkbox(label="Insulin"),
81
+ gr.Radio(choices=READMITTED_CHOICES, label="Readmitted")
82
+ ],
83
+ outputs=gr.HTML(label="Prediction"),
84
+ title="Diabetes Readmission Prediction",
85
+ description="Enter patient data to predict the likelihood of readmission."
86
+ )
87
+
88
+ # Launch the Gradio app
89
+ if __name__ == "__main__":
90
+ iface.launch()