rifqiakram commited on
Commit
08151c9
1 Parent(s): 27f9269

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +201 -0
  2. preprocessor.pkl +3 -0
  3. requirements.txt +10 -0
  4. trained_model.sav +0 -0
app.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+ import joblib
5
+
6
+ loaded_model = pickle.load(open('D:/Agri Sparta Indonesia/Credit Score/Multiclass Project Score/deploy/trained_model.sav', 'rb'))
7
+ preprocessor = joblib.load("D:/Agri Sparta Indonesia/Credit Score/Multiclass Project Score/deploy/preprocessor.pkl")
8
+
9
+ def obj_to_int(data):
10
+ # Mengganti nilai pada kolom planting_period
11
+ one_hot_enc = pd.get_dummies(data, columns=['planting_period', 'variety'])
12
+
13
+ return one_hot_enc
14
+
15
+ def transform_normalize(data, normalizer):
16
+ normalize_data = pd.DataFrame(normalizer.transform(data))
17
+ normalize_data.columns = data.columns
18
+ normalize_data.index = data.index
19
+ return normalize_data
20
+
21
+ def preprocess(data):
22
+ # 1. Encoding Categorical Value
23
+ X_encoded = obj_to_int(data)
24
+
25
+ required_features = ['planting_period_MT1',
26
+ 'planting_period_MT2',
27
+ 'planting_period_MT3',
28
+ 'variety_Umum',
29
+ 'variety_Hibrida'
30
+ ]
31
+
32
+ existing_features = set(X_encoded.columns)
33
+
34
+ # Ambil perbedaan antara fitur yang dibutuhkan dan yang sudah ada
35
+ missing_features = set(required_features) - existing_features
36
+
37
+ list_missing_features = list(missing_features)
38
+
39
+ for feature in list_missing_features:
40
+ X_encoded[feature] = 0
41
+
42
+ # urutkan column sesuai data train
43
+ column_order = ['completion_rate',
44
+ 'complete_time',
45
+ 'profit','harvest',
46
+ 'planting_period_MT1',
47
+ 'planting_period_MT2',
48
+ 'planting_period_MT3',
49
+ 'variety_Hibrida',
50
+ 'variety_Umum'
51
+ ]
52
+
53
+ X_encoded = X_encoded[column_order]
54
+
55
+ # 2. Normalization
56
+ X_clean = transform_normalize(data = X_encoded,
57
+ normalizer = preprocessor['normalizer'])
58
+
59
+ return X_clean
60
+
61
+ def calculate_harvest_score(row):
62
+ variety = row['variety']
63
+ harvest = row['harvest']
64
+ variety_umum = ['Umum', 'Sintanur', 'Mentik']
65
+ variety_hibrida = ['Hibrida', 'Mapan']
66
+
67
+ if variety in variety_umum:
68
+ if harvest < 3000:
69
+ score_harvest = 46.79
70
+ elif 3000 <= harvest <= 5000:
71
+ score_harvest = 64.24
72
+ elif 5001 <= harvest <= 7000:
73
+ score_harvest = 93.57
74
+ else:
75
+ score_harvest = 102.83
76
+
77
+ elif variety in variety_hibrida:
78
+ if harvest < 5000:
79
+ score_harvest = 40.55
80
+ elif 5001 <= harvest <= 7000:
81
+ score_harvest = 54.02
82
+ elif 7001 <= harvest <= 9000:
83
+ score_harvest = 68.34
84
+ else:
85
+ score_harvest = 85.17
86
+ else:
87
+ score_harvest = 0
88
+
89
+ return score_harvest
90
+
91
+ def calculate_profit_score(row):
92
+ profit = row['profit']
93
+
94
+ if profit <= 0:
95
+ score_profit = 0
96
+ elif 1 <= profit <= 2000000:
97
+ score_profit = 27.84
98
+ else:
99
+ score_profit = 39.5
100
+
101
+ return score_profit
102
+
103
+ def calculate_complete_time_score(row):
104
+ complete_time = row['complete_time']
105
+
106
+ if complete_time <= 3:
107
+ score_complete_time = 10.21
108
+ return score_complete_time
109
+
110
+ elif 4 <= complete_time <= 7:
111
+ score_complete_time = 6.69
112
+ return score_complete_time
113
+
114
+ else:
115
+ score_complete_time = 1
116
+ return score_complete_time
117
+
118
+ def calculate_completion_rate_score(row):
119
+ completion_rate = row['completion_rate']
120
+
121
+ if completion_rate <= 60:
122
+ score_completion_rate = 17.31
123
+ return score_completion_rate
124
+
125
+ elif 61 <= completion_rate <= 70:
126
+ score_completion_rate = 21.04
127
+ return score_completion_rate
128
+
129
+ elif 71 <= completion_rate <= 80:
130
+ score_completion_rate = 25.21
131
+ return score_completion_rate
132
+
133
+ elif 81 <= completion_rate <= 90:
134
+ score_completion_rate = 30.1
135
+ return score_completion_rate
136
+
137
+ else:
138
+ score_completion_rate = 34.4
139
+ return score_completion_rate
140
+
141
+ def total_score(data):
142
+ harvest_score = data.apply(calculate_harvest_score, axis=1)
143
+ profit_score = data.apply(calculate_profit_score, axis=1)
144
+ completion_rate_score = data.apply(calculate_completion_rate_score, axis=1)
145
+ complete_time_score = data.apply(calculate_complete_time_score, axis=1)
146
+
147
+ score = harvest_score + profit_score + complete_time_score + completion_rate_score
148
+
149
+ return score
150
+
151
+ def map_class_to_label(class_score):
152
+ class_labels = {
153
+ 0: "Sangat Baik",
154
+ 1: "Baik",
155
+ 2: "Cukup",
156
+ 3: "Buruk",
157
+ 4: "Sangat Buruk"
158
+ }
159
+ return class_labels.get(class_score, "Unknown")
160
+
161
+ def calculate_scoring(data):
162
+ score = total_score(data)
163
+ clean_data = preprocess(data)
164
+ hasil = loaded_model.predict(clean_data)
165
+
166
+ # Mapping nilai pada hasil ke label yang diinginkan
167
+ hasil_labels = [map_class_to_label(pred_class) for pred_class in hasil]
168
+
169
+ print('Hasil adalah', hasil_labels,
170
+ '\nTotal Score: ', score)
171
+ return hasil_labels, score
172
+
173
+ def main():
174
+ st.title("Project Scoring")
175
+
176
+ # Membuat tombol untuk upload file CSV
177
+ uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
178
+
179
+ if uploaded_file is not None:
180
+ # Membaca file CSV menjadi DataFrame
181
+ data = pd.read_csv(uploaded_file)
182
+
183
+ # Menampilkan preview dari data yang diupload
184
+ st.write("Preview Data:")
185
+ st.write(data.head())
186
+
187
+ # Membuat tombol "Calculate Scoring"
188
+ if st.button("Calculate Scoring"):
189
+ # Memanggil fungsi untuk menghitung scoring
190
+ hasil_scoring = calculate_scoring(data)
191
+
192
+ # Menampilkan hasil scoring
193
+ st.write("Hasil Scoring:")
194
+ hasil_df = pd.DataFrame({
195
+ 'Kategori': hasil_scoring[0],
196
+ 'Total Score': hasil_scoring[1]
197
+ })
198
+ st.table(hasil_df)
199
+
200
+ if __name__ == "__main__":
201
+ main()
preprocessor.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:23a82f8723bdf2bdc87a564ae0bcf069f5f160bf31a76c6fd54070f1eb5a88e7
3
+ size 11522768
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit==1.29.0
2
+ pandas==1.5.3
3
+ transformers==4.36.0
4
+ streamlit==1.29.0
5
+ pandas==1.5.3
6
+ joblib==1.2.0
7
+ scikit-learn==1.2.2
8
+ numpy==1.23.3
9
+ xgboost==2.0.2
10
+ pickle
trained_model.sav ADDED
Binary file (44.6 kB). View file