basaktamer's picture
Upload 8 files
bd9c6f9 verified
Raw History Blame Contribute Delete
1.55 kB
import streamlit as st
import joblib
import numpy as np
import pandas as pd
# 1. Page Config
st.set_page_config(page_title="Digit Recognizer", page_icon="🔢")
st.title("🔢 Digit Recognizer")
st.write("This app uses a Random Forest model to predict hand-drawn digits.")
# 2. Load the Model
@st.cache_resource
def load_model():
# Ensure digit_recognizer_rf.joblib is in the same folder
return joblib.load("digit_recognizer_rf.joblib")
model = load_model()
# 3. Sidebar for Data Upload (Optional)
st.sidebar.header("Upload Data")
uploaded_file = st.sidebar.file_exists = st.sidebar.file_uploader("Upload test.csv", type="csv")
if uploaded_file is not None:
test_data = pd.read_csv(uploaded_file)
# Select a row to predict
row_index = st.number_input("Select an image index from CSV", min_value=0, max_value=len(test_data)-1, value=0)
if st.button("Predict Digit"):
# Get pixels and normalize (if you did this during training)
pixels = test_data.iloc[row_index].values.reshape(1, -1)
# Display Image
image_reshaped = pixels.reshape(28, 28)
st.image(image_reshaped, caption=f"Image at index {row_index}", width=150)
# Prediction
prediction = model.predict(pixels)
st.success(f"The model predicts this digit is: **{prediction[0]}**")
else:
st.info("Please upload the 'test.csv' file from Kaggle in the sidebar to test the model.")
st.divider()
st.caption("Developed by Basak Tamer | Part of 20-Kaggle Competition Challenge")