import streamlit as st import pandas as pd import joblib import tempfile import os from train_models import run_automl import plotly.express as px st.set_page_config(page_title="๐Ÿ”ฎ AutoML Explorer", layout="wide") st.title("๐Ÿ”ฎ AutoML Explorer - Predict Anything From Any CSV") st.markdown(""" Welcome to **AutoML Explorer** โ€” your no-code ML assistant: - ๐Ÿ“‚ Upload any CSV - ๐Ÿง  Let the app clean, preprocess & analyze your data - ๐Ÿค– Tries many ML models including XGBoost & LightGBM - ๐ŸŽฏ Shows the best with full explanation & visuals - ๐Ÿ”ฎ Make live predictions & download result CSV """) file = st.file_uploader("๐Ÿ“ Upload your CSV file", type=["csv"]) if file: df = pd.read_csv(file) st.subheader("๐Ÿ“Š Dataset Preview") st.dataframe(df.head()) st.subheader("๐ŸŽฏ Choose Your Target Column") target = st.selectbox("Select what you want to predict:", df.columns) if st.button("๐Ÿš€ Run AutoML"): with st.spinner("๐Ÿค– Running smart ML analysis..."): result = run_automl(df, target) if "error" in result: st.error(result["error"]) else: st.success("โœจ AutoML Complete! Here's your custom ML report:") st.markdown("### ๐Ÿ” What We Did With Your Data") st.json(result["preprocessing"]) st.markdown("### ๐Ÿค– Model Scores") model_df = pd.DataFrame(result["model_scores"], index=["Score (%)"]).T st.dataframe(model_df) # Bar Chart st.markdown("### ๐Ÿ“Š Visual Model Comparison") chart = px.bar(model_df, x=model_df.index, y="Score (%)", title="Model Performance Comparison", color_discrete_sequence=["#636EFA"]) st.plotly_chart(chart, use_container_width=True) st.markdown("### โœ… Best Model Chosen") st.markdown(f"๐Ÿ† **{result['best_model']}** with **{result['best_accuracy']:.2f}% accuracy/performance**") if result["type"] == "classification": st.markdown("### ๐Ÿ“ˆ Confusion Matrix") st.dataframe(pd.DataFrame(result["confusion_matrix"])) st.markdown("### ๐Ÿงพ Classification Report") st.json(result["classification_report"]) else: st.markdown("### ๐Ÿ“ Regression Metrics") st.json(result["regression_report"]) # Save model & create download button with tempfile.NamedTemporaryFile(delete=False, suffix=".pkl") as tmp_file: joblib.dump(result["model_object"], tmp_file.name) st.download_button("๐Ÿ“ฅ Download Best Model (.pkl)", data=open(tmp_file.name, "rb"), file_name="best_model.pkl") # Predictions CSV st.markdown("### ๐Ÿ“„ Prediction Results (Test Set)") st.dataframe(result["predictions"]) csv = result["predictions"].to_csv(index=False).encode('utf-8') st.download_button("๐Ÿ“ฅ Download Predictions CSV", csv, "predictions.csv", "text/csv") # Live prediction input st.markdown("### ๐Ÿ”ฎ Live Prediction Input") input_data = {} for col in df.drop(columns=[target]).columns: dtype = df[col].dtype if dtype == 'object': input_data[col] = st.selectbox(f"{col}", options=sorted(df[col].dropna().unique())) else: input_data[col] = st.number_input(f"{col}", value=float(df[col].mean())) if st.button("๐ŸŽฏ Predict with Best Model"): user_df = pd.DataFrame([input_data]) from utils import preprocess_data try: X_temp, _, _, _ = preprocess_data(pd.concat([df, user_df], ignore_index=True), target) user_input = X_temp.iloc[-1:] prediction = result["model_object"].predict(user_input)[0] st.success(f"๐Ÿง  Predicted Output: {prediction}") except Exception as e: st.error(f"Prediction failed: {e}")