Spaces:
Build error
Build error
| 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}") | |