Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import joblib | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import numpy as np | |
| # --- Load model and encoders --- | |
| model = joblib.load("DB_model_IMAN.pkl") | |
| cat_encoders = joblib.load("categorical_encoders.pkl") # encoders for categorical features | |
| num_encoders = joblib.load("numerical_scaler.pkl") # StandardScaler 讗讜 讚讜诪讛 | |
| df = pd.DataFrame(pd.read_excel('/Users/shryqb/PycharmProjects/PythonProject/bachlor/some_running/iman_project/Rabies__Weather__War_Combined_1.4.25.xlsx')) | |
| # --- Get unique values for dropdowns --- | |
| animal_species_options = df['Animal Species'].dropna().unique().tolist() | |
| rabies_species_options = df['Rabies Species'].dropna().unique().tolist() | |
| settlement_options = df['Settlement'].dropna().unique().tolist() | |
| region_weather_options = df['Region_Weather'].dropna().unique().tolist() | |
| # --- Feature lists --- | |
| feature_names = ['Year', 'Animal Species', 'Rabies Species', 'Settlement', 'x', 'y', | |
| 'Region_Weather', 'Avg Temperature', 'Monthly Precipitation (mm)', | |
| 'Rainy Days', 'War in Israel'] | |
| categorical_features = ['Year','Animal Species', 'Rabies Species', 'Settlement', 'Region_Weather','War in Israel'] | |
| numerical_features = [ 'x', 'y', 'Avg Temperature', 'Monthly Precipitation (mm)', 'Rainy Days'] | |
| ###################################################################################################################################################### | |
| # setting | |
| # --- Streamlit page config --- | |
| st.set_page_config(page_title="Rabies Multi-Target Prediction", layout="wide", page_icon="馃") | |
| # --- Background style --- | |
| st.markdown( | |
| """ | |
| <style> | |
| .stApp { | |
| background: linear-gradient(to right, #e0ffe0, #fff0e0); | |
| color: #333; | |
| } | |
| h1, h2, h3 { | |
| color: #aa0000; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| ###################################################################################################################################################### | |
| st.title("Rabies Multi-Target Prediction Dashboard") | |
| st.header("Predict New Record") | |
| # --- Collect inputs with selectbox --- | |
| year = st.number_input("Year", min_value=1900, max_value=2030, value=2025) | |
| animal_species = st.selectbox("Animal Species", animal_species_options) | |
| rabies_species = st.selectbox("Rabies Species", rabies_species_options) | |
| settlement = st.selectbox("Settlement", settlement_options) | |
| x_coord = st.number_input("x coordinate", value=0.0) | |
| y_coord = st.number_input("y coordinate", value=0.0) | |
| region_weather = st.selectbox("Region Weather", region_weather_options) | |
| avg_temp = st.number_input("Avg Temperature", value=25.0) | |
| monthly_precip = st.number_input("Monthly Precipitation (mm)", value=50.0) | |
| rainy_days = st.number_input("Rainy Days", value=5) | |
| war_in_israel = st.selectbox("War in Israel", ["No", "Yes"]) | |
| # --- Create input DataFrame --- | |
| input_df = pd.DataFrame([[ | |
| year, animal_species, rabies_species, settlement, x_coord, y_coord, | |
| region_weather, avg_temp, monthly_precip, rainy_days, war_in_israel | |
| ]], columns=feature_names) | |
| # --- Encode categorical features --- | |
| for col in categorical_features: | |
| if col in cat_encoders: | |
| input_df[col] = cat_encoders[col].transform(input_df[col].astype(str)) | |
| # --- Scale numerical features --- | |
| if num_encoders is not None: | |
| input_df[numerical_features] = num_encoders.transform(input_df[numerical_features]) | |
| st.subheader("Input Data") | |
| st.dataframe(input_df) | |
| input_df['War in Israel'] = input_df['War in Israel'].map({'Yes': 1, 'No': 0}) | |
| # --- Make prediction --- | |
| if st.button("Predict"): | |
| try: | |
| prediction = model.predict(input_df) | |
| st.subheader("Predicted Values with Confidence") | |
| if hasattr(model, "predict_proba"): | |
| proba_list = model.predict_proba(input_df) | |
| target_names = ['Region', 'Month'] | |
| for i, target in enumerate(target_names): | |
| probs = proba_list[i] | |
| top_idx = probs.argsort(axis=1)[:, -2:][:, ::-1] | |
| top_conf = probs[0, top_idx[0,0]] * 100 | |
| top_label = cat_encoders[target].inverse_transform([top_idx[0,0]])[0] | |
| #second_conf = probs[0, top_idx[0,1]] * 100 | |
| #second_label = cat_encoders[target].inverse_transform([top_idx[0,1]])[0] | |
| st.write(f"{target}: Predict: {top_label} - Confident: {top_conf:.2f} %") | |
| #st.write(f"{target} Second best: {second_label} - Confident: {second_conf:.2f}%") | |
| else: | |
| st.success(f"Prediction: {prediction}") | |
| # --- Display prediction probabilities per target --- | |
| if hasattr(model, "predict_proba"): | |
| proba_list = model.predict_proba(input_df) | |
| target_names = ['Region', 'Month'] | |
| for i, target in enumerate(target_names): | |
| probs = proba_list[i][0] # 讛住转讘专讜讬讜转 诇讻诇 讛拽讟讙讜专讬讜转 | |
| categories = cat_encoders[target].classes_ # 砖诪讜转 讛拽讟讙讜专讬讜转 讛诪拽讜专讬讬诐 | |
| # 诇讛讻讬谉 DataFrame | |
| df_probs = pd.DataFrame({ | |
| "Category": categories, | |
| "Probability (%)": probs * 100 | |
| }).sort_values(by="Probability (%)", ascending=False) | |
| st.subheader(f"{target} Prediction Probabilities") | |
| #st.dataframe(df_probs) # 诪爪讬讙 讙诐 讟讘诇讛 | |
| # --- 讙专祝 讘专 --- | |
| fig, ax = plt.subplots(figsize=(10, 5)) | |
| colors = [ | |
| "#FF5733", # 讻转讜诐-讗讚讜诐 讝讜讛专 | |
| "#33FF57", # 讬专讜拽 讝讜讛专 | |
| "#3357FF", # 讻讞讜诇 讝讜讛专 | |
| "#FF33A8", # 讜专讜讚 讝讜讛专 | |
| "#F3FF33", # 爪讛讜讘 讝讜讛专 | |
| "#33FFF6", # 转讻诇转 讝讜讛专 | |
| "#FF8C33" # 讻转讜诐 讻讛讛-讝讜讛专 | |
| ] | |
| sns.barplot(x="Category", y="Probability (%)", data=df_probs, palette=colors, ax=ax) | |
| plt.xticks(rotation=45, ha='right') | |
| plt.ylim(0, 100) | |
| st.pyplot(fig) | |
| except Exception as e: | |
| st.error(f"Error during prediction: {e}") | |