s25059's picture
Sync app files
fa069b2 verified
import streamlit as st
import pandas as pd
import joblib
import json
import os
APP_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
METRICS_DIR = os.path.join(ROOT, "metrics")
MODEL_PATH = os.path.join(ROOT, "src", "model.pkl")
model = joblib.load(MODEL_PATH)
with open(os.path.join(METRICS_DIR, "metrics.json")) as f:
metrics = json.load(f)
cat_options = {
"gender": ["Male", "Female", "Other"],
"device_type": ["Mobile", "Desktop", "Tablet"],
"ad_position": ["Top", "Side", "Bottom"],
"browsing_history": ["Sports", "News", "Shopping", "Other"],
"time_of_day": ["Morning", "Afternoon", "Evening", "Night"],
}
st.title("Ad Click Predictor")
st.markdown(
"Wprowadź dane użytkownika i sprawdź, czy kliknie w reklamę!<br>"
"<sup>Model został wybrany automatycznie na podstawie walidacji krzyżowej spośród Random Forest, Logistic Regression oraz Gradient Boosting (Pipeline).</sup>",
unsafe_allow_html=True,
)
age = st.slider("Wiek użytkownika", 18, 64, 30)
gender = st.selectbox("Płeć", cat_options["gender"])
device_type = st.selectbox("Typ urządzenia", cat_options["device_type"])
ad_position = st.selectbox("Pozycja reklamy", cat_options["ad_position"])
browsing_history = st.selectbox("Poprzednia aktywność", cat_options["browsing_history"])
time_of_day = st.selectbox("Pora dnia", cat_options["time_of_day"])
user_input = pd.DataFrame(
[
{
"age": age,
"gender": gender,
"device_type": device_type,
"ad_position": ad_position,
"browsing_history": browsing_history,
"time_of_day": time_of_day,
}
]
)
if st.button("Sprawdź czy użytkownik kliknie w reklamę"):
proba = model.predict_proba(user_input)[0][1]
st.success("Kliknie!" if proba > 0.5 else "Nie kliknie.")
st.markdown(f"**Prawdopodobieństwo kliknięcia:** `{proba:.2%}`")
st.markdown("---")
st.markdown("#### Wybrany model (na podstawie walidacji krzyżowej):")
st.write(f"- Model: `{metrics['best_model']}`")
st.write(f"- Średni F1-score (cross-val): `{metrics['cv_f1_score']:.2%}`")
st.markdown("#### Wyniki metryk na zbiorze testowym:")
st.write(f"- Accuracy: `{metrics['accuracy']:.2%}`")
st.write(f"- F1-score: `{metrics['f1_score']:.2%}`")
st.markdown("#### Macierz pomyłek (Confusion Matrix):")
st.image(os.path.join(METRICS_DIR, "confusion_matrix.png"))