File size: 1,870 Bytes
21974b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333ec78
21974b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import joblib
import numpy as np
import pandas as pd
import gradio as gr

# Load the saved models
encoder_file = "ordinal_encoder.joblib"
scaler_file = "min_max_scaler.joblib"
joblib_file = "best_random_forest_model_s.joblib"

loaded_encoder = joblib.load(encoder_file)
loaded_scaler = joblib.load(scaler_file)
loaded_model = joblib.load(joblib_file)

# Define the prediction function
def predict_from_csv(file_path):
    df = pd.read_csv(file_path)

    # Convert date columns to datetime
    df['appt_slot_date'] = pd.to_datetime(df['appt_slot_date'])
    df['appt_date'] = pd.to_datetime(df['appt_date'])

    # Calculate the days_difference
    df['days_difference'] = (df['appt_slot_date'] - df['appt_date']).dt.days

    input_columns = ['facilitytype', 'appt_type', 'service_name', 'facility_name', 'directorate_name', 'year', 'region', 'days_difference']
    test_data = df[input_columns]
    test_data = test_data.astype(str)

    test_data_encoded = loaded_encoder.transform(test_data)
    test_data_scaled = loaded_scaler.transform(test_data_encoded)

    predictions = loaded_model.predict(test_data_scaled)

    df['Prediction'] = predictions

    df['Prediction'] = np.where(predictions == 0, "No Show", "Show")
    print(df['Prediction'].value_counts())

    output_file = "predictions.csv"
    df.to_csv(output_file, index=False, encoding='utf-8-sig')

    return output_file

# Define the Gradio interface
interface = gr.Interface(
    fn=predict_from_csv,
    inputs=gr.File(label='Upload CSV file', type='filepath', file_count='single'),
    outputs=gr.File(label='Download CSV file with predictions'),
    title="Appointment Status Prediction in Saudi Arabian Health Care System",
    description="Upload a CSV file with the required columns to get predictions."
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch(debug=True)