Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load the saved models
|
7 |
+
encoder_file = "ordinal_encoder.joblib"
|
8 |
+
scaler_file = "min_max_scaler.joblib"
|
9 |
+
joblib_file = "best_random_forest_model_s.joblib"
|
10 |
+
|
11 |
+
loaded_encoder = joblib.load(encoder_file)
|
12 |
+
loaded_scaler = joblib.load(scaler_file)
|
13 |
+
loaded_model = joblib.load(joblib_file)
|
14 |
+
|
15 |
+
# Define the prediction function
|
16 |
+
def predict_from_csv(file_path):
|
17 |
+
df = pd.read_csv(file_path)
|
18 |
+
|
19 |
+
# Convert date columns to datetime
|
20 |
+
df['appt_slot_date'] = pd.to_datetime(df['appt_slot_date'])
|
21 |
+
df['appt_date'] = pd.to_datetime(df['appt_date'])
|
22 |
+
|
23 |
+
# Calculate the days_difference
|
24 |
+
df['days_difference'] = (df['appt_slot_date'] - df['appt_date']).dt.days
|
25 |
+
|
26 |
+
input_columns = ['facilitytype', 'appt_type', 'service_name', 'facility_name', 'directorate_name', 'year', 'region', 'days_difference']
|
27 |
+
test_data = df[input_columns]
|
28 |
+
test_data = test_data.astype(str)
|
29 |
+
|
30 |
+
test_data_encoded = loaded_encoder.transform(test_data)
|
31 |
+
test_data_scaled = loaded_scaler.transform(test_data_encoded)
|
32 |
+
|
33 |
+
predictions = loaded_model.predict(test_data_scaled)
|
34 |
+
|
35 |
+
df['Prediction'] = predictions
|
36 |
+
|
37 |
+
df['Prediction'] = np.where(predictions == 0, "No Show", "Show")
|
38 |
+
print(df['Prediction'].value_counts())
|
39 |
+
|
40 |
+
output_file = "predictions.csv"
|
41 |
+
df.to_csv(output_file, index=False)
|
42 |
+
|
43 |
+
return output_file
|
44 |
+
|
45 |
+
# Define the Gradio interface
|
46 |
+
interface = gr.Interface(
|
47 |
+
fn=predict_from_csv,
|
48 |
+
inputs=gr.File(label='Upload CSV file', type='filepath', file_count='single'),
|
49 |
+
outputs=gr.File(label='Download CSV file with predictions'),
|
50 |
+
title="Appointment Status Prediction in Saudi Arabian Health Care System",
|
51 |
+
description="Upload a CSV file with the required columns to get predictions."
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the Gradio app
|
55 |
+
if __name__ == "__main__":
|
56 |
+
interface.launch(debug=True)
|