rubyaiyoung-edu commited on
Commit
31688e6
1 Parent(s): 8e78d74

Creation of app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Importing the libraries
2
+ import joblib
3
+ import gradio as gr
4
+
5
+ # Loading the trained model
6
+ random_forest_model = joblib.load('tuned_random_forest_model.joblib')
7
+ xgb_model = joblib.load('xgb_model_release.joblib')
8
+ logistic_regression_model = joblib.load('lr_lbfgs_model.joblib')
9
+
10
+ def random_forest_predict(new_id, judge_id, prior_misdemeanor, race, county, med_house_income, highest_severity, probation):
11
+ race_to_num = {'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}
12
+ race_num = race_to_num[race]
13
+ processed_input = [new_id, judge_id, prior_misdemeanor, race_num, county, med_house_income, highest_severity, probation]
14
+
15
+ # Predicting
16
+ prediction = random_forest_model.predict([processed_input])
17
+ return "The prisoner is likely to return to jail." if prediction[0] == 1 else "The prisoner is unlikely to return to jail."
18
+
19
+ # Input components
20
+ random_forest_input_components = [
21
+ gr.Number(label="Prisoner ID", value=0),
22
+ gr.Number(label="Judge ID", value=0),
23
+ gr.Number(label="Prior Misdemeanor", value=0),
24
+ gr.Dropdown(label="Race", choices={'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}, value='Caucasian'),
25
+ gr.Number(label="County", value=0),
26
+ gr.Number(label="Median House Income", value=0),
27
+ gr.Number(label="Highest Severity", value=0),
28
+ gr.Number(label="Probation", value=0)
29
+ ]
30
+
31
+ random_forest_interface = gr.Interface(
32
+ fn=random_forest_predict,
33
+ inputs=random_forest_input_components,
34
+ outputs="text",
35
+ title="Random Forest Prediction",
36
+ description="Predicts the likelihood of a prisoner returning to jail using a Random Forest model."
37
+ )
38
+
39
+ def xgb_predict(new_id, judge_id, sex, race, prior_felony, highest_severity, age_judge, probation):
40
+ race_to_num = {'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}
41
+ race_num = race_to_num[race]
42
+ sex_to_num = {'F': 0, 'M': 1}
43
+ sex_num = sex_to_num[sex]
44
+ processed_input = [new_id, judge_id, sex_num, race_num, prior_felony, highest_severity, age_judge, probation]
45
+
46
+ # Predicting
47
+ prediction = xgb_model.predict([processed_input])
48
+ return "The defendant is unlikely to go to jail." if prediction[0] == 1 else "The defendant is likely to go to jail."
49
+
50
+ # Input components
51
+ xgb_input_components = [
52
+ gr.Number(label="Prisoner ID", value=0),
53
+ gr.Number(label="Judge ID", value=0),
54
+ gr.Dropdown(label="Sex", choices={'F': 0, 'M': 1}, value='M'),
55
+ gr.Dropdown(label="Race", choices={'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}, value='Caucasian'),
56
+ gr.Number(label="Prior Felony", value=0),
57
+ gr.Number(label="Highest Severity", value=0),
58
+ gr.Number(label="Age of Judge", value=0),
59
+ gr.Number(label="Probation", value=0)
60
+ ]
61
+
62
+ xgb_interface = gr.Interface(
63
+ fn=xgb_predict,
64
+ inputs=xgb_input_components,
65
+ outputs="text",
66
+ title="XGBoost Prediction",
67
+ description="Predicts the likelihood of a prisoner returning to jail using an XGBoost model."
68
+ )
69
+
70
+ def logistic_regression_predict(new_id, sex, race, offence_category, age_judge, prior_felony, probation, year, case_type):
71
+ race_to_num = {'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}
72
+ race_num = race_to_num[race]
73
+ case_type_to_num = {'Criminal Traffic': 0, 'Felony': 1, 'Misdemeanor': 2}
74
+ case_type_num = case_type_to_num[case_type]
75
+ offence_category_to_num = {'1st Deg. Sex. Assault of Child': 0, '2nd Deg. Sex. Assault of Child': 1, 'Armed Robbery': 2, 'Arson': 3, 'BAC': 4, 'Bail Jumping': 5, 'Battery': 6, 'Burglary': 7, 'Child Abuse': 8, 'Contempt of Court': 9, 'Crimes Against Children': 10, 'Criminal Damage': 11, 'Criminal Trespass': 12, 'Disorderly Conduct': 13, 'Drug Manufacture/Deliver': 14, 'Drug Paraphernalia': 15, 'Drug Possession': 16, 'Entering Locked Vehicle': 17, 'Escape': 18, 'Extradition': 19, 'First Degree Intentional Homicide': 20, 'First Degree Reckless Homicide': 21, 'Fleeing/Eluding': 22, 'Forgery': 23, 'Fourth Degree Sexual Assau': 24, 'Gambling': 25, 'Hit and Run': 26, 'Intimidate Witness/Victim': 27, 'Kidnap/Hostage/False Imprisonment': 28, 'Local or Unidentified Forfeiture': 29, 'Non-Traffic Forfeiture': 30, 'OAR/OAS': 31, 'Operate Vehicle Without Consent': 32, 'Operate Vehicle w/out Consent': 33, 'Operate Without License': 34, 'Operating While Intoxicated': 35, 'Operating while intoxicated': 36, 'Other Bodily Security': 37, 'Other Crimes Against Children': 38, 'Other Drug Offenses': 39, 'Other Felony': 40, 'Other Fraud': 41, 'Other Homicide': 42, 'Other Misdemeanor': 43, 'Other Public Safety Crimes': 44, 'Perjury': 45, 'Public Assistance Fraud': 46, 'Receiving Stolen Property': 47, 'Reckless Driving': 48, 'Resisting Officer': 49, 'Retail Theft (Shoplifting)': 50, 'Sex Crimes': 51, 'Sexual Assault': 52, 'Stalking': 53, 'Substantial/Aggravated Battery': 54, 'Theft': 55, 'Unarmed Robbery': 56, 'Unidentified Felony': 57, 'Unidentified Felony Traffic': 58, 'Unidentified Misdemeanor': 59, 'Unidentified Misdemeanor Traffic': 60, 'Unidentified Traffic Forfeiture': 61, 'Violate Occupational': 62, 'Violation of TRO': 63, 'Weapons/Explosives': 64, 'Worthless Checks': 65}
76
+ offence_category_num = offence_category_to_num[offence_category]
77
+ sex_to_num = {'F': 0, 'M': 1}
78
+ sex_num = sex_to_num[sex]
79
+
80
+ processed_input = [new_id, sex_num, race_num, offence_category_num, age_judge, prior_felony, probation, year, case_type_num]
81
+
82
+ # Predicting
83
+ prediction = logistic_regression_model.predict([processed_input])
84
+ return "The defendant is likely to be detained (not released)." if prediction[0] == 1 else "The defendant is likely to be restrained."
85
+
86
+ # Input components
87
+ logistic_regression_input_components = [
88
+ gr.Number(label="Prisoner ID", value=0),
89
+ gr.Dropdown(label="Sex", choices={'F': 0, 'M': 1}, value='M'),
90
+ gr.Dropdown(label="Race", choices={'African American': 0, 'American Indian or Alaskan Native': 1, 'Asian or Pacific Islander': 2, 'Caucasian': 3, 'Hispanic': 4}, value='Caucasian'),
91
+ gr.Dropdown(label="Offence Category", choices={'1st Deg. Sex. Assault of Child': 0, '2nd Deg. Sex. Assault of Child': 1, 'Armed Robbery': 2, 'Arson': 3, 'BAC': 4, 'Bail Jumping': 5, 'Battery': 6, 'Burglary': 7, 'Child Abuse': 8, 'Contempt of Court': 9, 'Crimes Against Children': 10, 'Criminal Damage': 11, 'Criminal Trespass': 12, 'Disorderly Conduct': 13, 'Drug Manufacture/Deliver': 14, 'Drug Paraphernalia': 15, 'Drug Possession': 16, 'Entering Locked Vehicle': 17, 'Escape': 18, 'Extradition': 19, 'First Degree Intentional Homicide': 20, 'First Degree Reckless Homicide': 21, 'Fleeing/Eluding': 22, 'Forgery': 23, 'Fourth Degree Sexual Assau': 24, 'Gambling': 25, 'Hit and Run': 26, 'Intimidate Witness/Victim': 27, 'Kidnap/Hostage/False Imprisonment': 28, 'Local or Unidentified Forfeiture': 29, 'Non-Traffic Forfeiture': 30, 'OAR/OAS': 31, 'Operate Vehicle Without Consent': 32, 'Operate Vehicle w/out Consent': 33, 'Operate Without License': 34, 'Operating While Intoxicated': 35, 'Operating while intoxicated': 36, 'Other Bodily Security': 37, 'Other Crimes Against Children': 38, 'Other Drug Offenses': 39, 'Other Felony': 40, 'Other Fraud': 41, 'Other Homicide': 42, 'Other Misdemeanor': 43, 'Other Public Safety Crimes': 44, 'Perjury': 45, 'Public Assistance Fraud': 46, 'Receiving Stolen Property': 47, 'Reckless Driving': 48, 'Resisting Officer': 49, 'Retail Theft (Shoplifting)': 50, 'Sex Crimes': 51, 'Sexual Assault': 52, 'Stalking': 53, 'Substantial/Aggravated Battery': 54, 'Theft': 55, 'Unarmed Robbery': 56, 'Unidentified Felony': 57, 'Unidentified Felony Traffic': 58, 'Unidentified Misdemeanor': 59, 'Unidentified Misdemeanor Traffic': 60, 'Unidentified Traffic Forfeiture': 61, 'Violate Occupational': 62, 'Violation of TRO': 63, 'Weapons/Explosives': 64, 'Worthless Checks': 65}, value='Worthless Checks'),
92
+ gr.Number(label="Age of Judge", value=0),
93
+ gr.Number(label="Prior Felony", value=0),
94
+ gr.Number(label="Probation", value=0),
95
+ gr.Number(label="Year", value=0),
96
+ gr.Dropdown(label="Case Type", choices={'Criminal Traffic': 0, 'Felony': 1, 'Misdemeanor': 2}, value='Felony')
97
+ ]
98
+
99
+ logistic_regression_interface = gr.Interface(
100
+ fn=logistic_regression_predict,
101
+ inputs=logistic_regression_input_components,
102
+ outputs="text",
103
+ title="Logistic Regression Prediction",
104
+ description="Predicts the likelihood of a defendant being detained or released using a Logistic Regression model."
105
+ )
106
+
107
+ tabbed_interface = gr.TabbedInterface(
108
+ [random_forest_interface, xgb_interface, logistic_regression_interface],
109
+ ["Random Forest", "XGBoost", "Logistic Regression"]
110
+ )
111
+ tabbed_interface.launch(share=True)