File size: 2,826 Bytes
bcb0385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
import streamlit as st
import pandas as pd
from data_fetcher import get_last_n_races_results, get_next_race_info
from feature_engineering import FeatureEngineer
from model_trainer import ModelTrainer

st.title("๐Ÿ F1 Simple Race Predictor")

n_races = st.number_input("๐Ÿ”ข How many past races should the model use?", min_value=1, max_value=10, value=5)

if st.button(f"Fetch Last {n_races} Race Results"):
    race_results = get_last_n_races_results(n_races)

    if not race_results.empty:
        st.session_state['race_results'] = race_results
        st.success("โœ… Race results fetched!")
        st.dataframe(race_results)

        unique_circuits = race_results['CircuitName'].unique()
        st.info(f"โ„น๏ธ Using results from **{len(unique_circuits)} different circuits**.")
    else:
        st.error("โŒ No race results found.")

if 'race_results' in st.session_state:
    fe = FeatureEngineer(n_races=n_races)

    try:
        features, labels = fe.prepare_features(st.session_state['race_results'])
        st.session_state['features'] = features
        st.session_state['labels'] = labels
    except Exception as e:
        st.error(f"โŒ Error preparing features: {e}")

    if st.button("Train Model"):
        trainer = ModelTrainer()

        try:
            X = st.session_state['features']
            y = st.session_state['labels']

            trainer.train(X, y)
            st.session_state['trainer'] = trainer
            st.success("โœ… Model trained successfully!")
        except Exception as e:
            st.error(f"โŒ Error during training: {e}")

    if 'trainer' in st.session_state and st.button("Predict Next Race"):
        next_race = get_next_race_info()

        if next_race is not None and 'EventName' in next_race:
            race_name = next_race['EventName']
            st.subheader(f"๐ŸŽ๏ธ Predictions for: {race_name}")
            st.info(f"๐Ÿ”ฎ Predicting race results for **{race_name}**!")
        else:
            st.subheader("๐ŸŽ๏ธ Predictions for: (Unknown Upcoming Race)")
            st.warning("โš ๏ธ Next race information is missing.")

        try:
            prediction_features = fe.prepare_prediction_features(st.session_state['race_results'])
            preds = st.session_state['trainer'].predict(prediction_features)

            drivers = st.session_state['race_results']['DriverId'].unique()
            prediction_df = pd.DataFrame({
                'Driver': drivers,
                'Predicted Finish Position': preds
            }).sort_values('Predicted Finish Position')

            st.write("### ๐Ÿ Predicted Race Results")
            st.dataframe(prediction_df)

        except Exception as e:
            st.error(f"โŒ Error during prediction: {e}")