alperugurcan commited on
Commit
1845b66
Β·
verified Β·
1 Parent(s): 35277f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -65
app.py CHANGED
@@ -1,58 +1,26 @@
1
  import gradio as gr
2
  import pandas as pd
3
- from catboost import CatBoostClassifier
4
  import joblib
5
 
6
- # Load and prepare data
7
- train_df = pd.read_csv('/kaggle/input/amazon-employee-access-challenge/train.csv')
8
- X = train_df.drop('ACTION', axis=1)
9
- y = train_df['ACTION']
10
-
11
- # Train and save model
12
- def train_and_save_model():
13
- model = CatBoostClassifier(
14
- iterations=100,
15
- learning_rate=0.1,
16
- depth=6,
17
- verbose=0,
18
- task_type='CPU',
19
- bootstrap_type='Bernoulli',
20
- subsample=0.8,
21
- eval_metric='Accuracy',
22
- early_stopping_rounds=20
23
- )
24
- model.fit(X, y)
25
- joblib.dump(model, 'amazon_access_model.joblib', compress=3)
26
- return model
27
 
28
- # Cache common values
29
- COMMON_VALUES = {
30
- 'ROLE_ROLLUP_1': train_df['ROLE_ROLLUP_1'].mode()[0],
31
- 'ROLE_ROLLUP_2': train_df['ROLE_ROLLUP_2'].mode()[0],
32
- 'ROLE_DEPTNAME': train_df['ROLE_DEPTNAME'].mode()[0],
33
- 'ROLE_FAMILY_DESC': train_df['ROLE_FAMILY_DESC'].mode()[0],
34
- 'ROLE_FAMILY': train_df['ROLE_FAMILY'].mode()[0],
35
- 'ROLE_CODE': train_df['ROLE_CODE'].mode()[0]
36
- }
37
-
38
- # Load or train model
39
- try:
40
- model = joblib.load('amazon_access_model.joblib')
41
- except:
42
- model = train_and_save_model()
43
 
44
  def predict_access(resource, mgr_id, role_title):
 
45
  input_data = pd.DataFrame([[
46
  resource,
47
  mgr_id,
48
- COMMON_VALUES['ROLE_ROLLUP_1'],
49
- COMMON_VALUES['ROLE_ROLLUP_2'],
50
- COMMON_VALUES['ROLE_DEPTNAME'],
51
  role_title,
52
- COMMON_VALUES['ROLE_FAMILY_DESC'],
53
- COMMON_VALUES['ROLE_FAMILY'],
54
- COMMON_VALUES['ROLE_CODE']
55
- ]], columns=X.columns)
56
 
57
  prediction = model.predict(input_data)[0]
58
  confidence = model.predict_proba(input_data)[0][prediction]
@@ -60,32 +28,17 @@ def predict_access(resource, mgr_id, role_title):
60
  result = "βœ… Access Granted" if prediction == 1 else "❌ Access Denied"
61
  return f"{result} (Confidence: {confidence:.2%})"
62
 
63
- # Create Gradio interface
64
  iface = gr.Interface(
65
  fn=predict_access,
66
  inputs=[
67
- gr.Dropdown(
68
- choices=sorted(train_df['RESOURCE'].unique().tolist())[:100], # Limit choices
69
- label="Resource"
70
- ),
71
- gr.Dropdown(
72
- choices=sorted(train_df['MGR_ID'].unique().tolist())[:100], # Limit choices
73
- label="Manager"
74
- ),
75
- gr.Dropdown(
76
- choices=sorted(train_df['ROLE_TITLE'].unique().tolist()),
77
- label="Role Title"
78
- )
79
  ],
80
  outputs=gr.Text(label="Access Decision"),
81
  title="Amazon Access Control",
82
- description="Select employee details to check access permission",
83
- theme="soft",
84
- examples=[
85
- [train_df['RESOURCE'].iloc[0], train_df['MGR_ID'].iloc[0], train_df['ROLE_TITLE'].iloc[0]],
86
- [train_df['RESOURCE'].iloc[1], train_df['MGR_ID'].iloc[1], train_df['ROLE_TITLE'].iloc[1]]
87
- ]
88
  )
89
 
90
- if __name__ == "__main__":
91
- iface.launch()
 
1
  import gradio as gr
2
  import pandas as pd
 
3
  import joblib
4
 
5
+ # Load the saved model
6
+ model = joblib.load('amazon_access_model.joblib')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Load minimal data just for dropdowns
9
+ train_df = pd.read_csv('/kaggle/input/amazon-employee-access-challenge/train.csv')
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def predict_access(resource, mgr_id, role_title):
12
+ # Common values for other fields
13
  input_data = pd.DataFrame([[
14
  resource,
15
  mgr_id,
16
+ train_df['ROLE_ROLLUP_1'].mode()[0],
17
+ train_df['ROLE_ROLLUP_2'].mode()[0],
18
+ train_df['ROLE_DEPTNAME'].mode()[0],
19
  role_title,
20
+ train_df['ROLE_FAMILY_DESC'].mode()[0],
21
+ train_df['ROLE_FAMILY'].mode()[0],
22
+ train_df['ROLE_CODE'].mode()[0]
23
+ ]], columns=train_df.columns[1:]) # Exclude ACTION column
24
 
25
  prediction = model.predict(input_data)[0]
26
  confidence = model.predict_proba(input_data)[0][prediction]
 
28
  result = "βœ… Access Granted" if prediction == 1 else "❌ Access Denied"
29
  return f"{result} (Confidence: {confidence:.2%})"
30
 
31
+ # Simple interface
32
  iface = gr.Interface(
33
  fn=predict_access,
34
  inputs=[
35
+ gr.Dropdown(choices=sorted(train_df['RESOURCE'].unique().tolist())[:100], label="Resource"),
36
+ gr.Dropdown(choices=sorted(train_df['MGR_ID'].unique().tolist())[:100], label="Manager"),
37
+ gr.Dropdown(choices=sorted(train_df['ROLE_TITLE'].unique().tolist()), label="Role Title")
 
 
 
 
 
 
 
 
 
38
  ],
39
  outputs=gr.Text(label="Access Decision"),
40
  title="Amazon Access Control",
41
+ theme="soft"
 
 
 
 
 
42
  )
43
 
44
+ iface.launch()