File size: 2,450 Bytes
cec776e
 
 
 
 
 
d07948e
 
cec776e
 
 
 
fa069b2
cec776e
 
7c8bd85
 
 
 
 
fa069b2
7c8bd85
cec776e
 
 
 
 
 
 
 
 
fa069b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cec776e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa069b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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"))