|
import streamlit as st |
|
import pickle |
|
import pandas as pd |
|
import joblib |
|
|
|
loaded_model = pickle.load(open('trained_model.sav', 'rb')) |
|
preprocessor = joblib.load("preprocessor.pkl") |
|
|
|
def obj_to_int(data): |
|
|
|
one_hot_enc = pd.get_dummies(data, columns=['planting_period', 'variety']) |
|
|
|
return one_hot_enc |
|
|
|
def transform_normalize(data, normalizer): |
|
normalize_data = pd.DataFrame(normalizer.transform(data)) |
|
normalize_data.columns = data.columns |
|
normalize_data.index = data.index |
|
return normalize_data |
|
|
|
def preprocess(data): |
|
|
|
X_encoded = obj_to_int(data) |
|
|
|
required_features = ['planting_period_MT1', |
|
'planting_period_MT2', |
|
'planting_period_MT3', |
|
'variety_Umum', |
|
'variety_Hibrida' |
|
] |
|
|
|
existing_features = set(X_encoded.columns) |
|
|
|
|
|
missing_features = set(required_features) - existing_features |
|
|
|
list_missing_features = list(missing_features) |
|
|
|
for feature in list_missing_features: |
|
X_encoded[feature] = 0 |
|
|
|
|
|
column_order = ['completion_rate', |
|
'complete_time', |
|
'profit','harvest', |
|
'planting_period_MT1', |
|
'planting_period_MT2', |
|
'planting_period_MT3', |
|
'variety_Hibrida', |
|
'variety_Umum' |
|
] |
|
|
|
X_encoded = X_encoded[column_order] |
|
|
|
|
|
X_clean = transform_normalize(data = X_encoded, |
|
normalizer = preprocessor['normalizer']) |
|
|
|
return X_clean |
|
|
|
def calculate_harvest_score(row): |
|
variety = row['variety'] |
|
harvest = row['harvest'] |
|
variety_umum = ['Umum', 'Sintanur', 'Mentik'] |
|
variety_hibrida = ['Hibrida', 'Mapan'] |
|
|
|
if variety in variety_umum: |
|
if harvest < 3000: |
|
score_harvest = 46.79 |
|
elif 3000 <= harvest <= 5000: |
|
score_harvest = 64.24 |
|
elif 5001 <= harvest <= 7000: |
|
score_harvest = 93.57 |
|
else: |
|
score_harvest = 102.83 |
|
|
|
elif variety in variety_hibrida: |
|
if harvest < 5000: |
|
score_harvest = 40.55 |
|
elif 5001 <= harvest <= 7000: |
|
score_harvest = 54.02 |
|
elif 7001 <= harvest <= 9000: |
|
score_harvest = 68.34 |
|
else: |
|
score_harvest = 85.17 |
|
else: |
|
score_harvest = 0 |
|
|
|
return score_harvest |
|
|
|
def calculate_profit_score(row): |
|
profit = row['profit'] |
|
|
|
if profit <= 0: |
|
score_profit = 0 |
|
elif 1 <= profit <= 2000000: |
|
score_profit = 27.84 |
|
else: |
|
score_profit = 39.5 |
|
|
|
return score_profit |
|
|
|
def calculate_complete_time_score(row): |
|
complete_time = row['complete_time'] |
|
|
|
if complete_time <= 3: |
|
score_complete_time = 10.21 |
|
return score_complete_time |
|
|
|
elif 4 <= complete_time <= 7: |
|
score_complete_time = 6.69 |
|
return score_complete_time |
|
|
|
else: |
|
score_complete_time = 1 |
|
return score_complete_time |
|
|
|
def calculate_completion_rate_score(row): |
|
completion_rate = row['completion_rate'] |
|
|
|
if completion_rate <= 60: |
|
score_completion_rate = 17.31 |
|
return score_completion_rate |
|
|
|
elif 61 <= completion_rate <= 70: |
|
score_completion_rate = 21.04 |
|
return score_completion_rate |
|
|
|
elif 71 <= completion_rate <= 80: |
|
score_completion_rate = 25.21 |
|
return score_completion_rate |
|
|
|
elif 81 <= completion_rate <= 90: |
|
score_completion_rate = 30.1 |
|
return score_completion_rate |
|
|
|
else: |
|
score_completion_rate = 34.4 |
|
return score_completion_rate |
|
|
|
def total_score(data): |
|
harvest_score = data.apply(calculate_harvest_score, axis=1) |
|
profit_score = data.apply(calculate_profit_score, axis=1) |
|
completion_rate_score = data.apply(calculate_completion_rate_score, axis=1) |
|
complete_time_score = data.apply(calculate_complete_time_score, axis=1) |
|
|
|
score = harvest_score + profit_score + complete_time_score + completion_rate_score |
|
|
|
return score |
|
|
|
def map_class_to_label(class_score): |
|
class_labels = { |
|
0: "Sangat Baik", |
|
1: "Baik", |
|
2: "Cukup", |
|
3: "Buruk", |
|
4: "Sangat Buruk" |
|
} |
|
return class_labels.get(class_score, "Unknown") |
|
|
|
def calculate_scoring(data): |
|
score = total_score(data) |
|
clean_data = preprocess(data) |
|
hasil = loaded_model.predict(clean_data) |
|
|
|
|
|
hasil_labels = [map_class_to_label(pred_class) for pred_class in hasil] |
|
|
|
print('Hasil adalah', hasil_labels, |
|
'\nTotal Score: ', score) |
|
return hasil_labels, score |
|
|
|
def main(): |
|
st.title("Project Scoring") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
data = pd.read_csv(uploaded_file) |
|
|
|
|
|
st.write("Preview Data:") |
|
st.write(data.head()) |
|
|
|
|
|
if st.button("Calculate Scoring"): |
|
|
|
hasil_scoring = calculate_scoring(data) |
|
|
|
|
|
st.write("Hasil Scoring:") |
|
hasil_df = pd.DataFrame({ |
|
'Kategori': hasil_scoring[0], |
|
'Total Score': hasil_scoring[1] |
|
}) |
|
st.table(hasil_df) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|