import gradio as gr import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import make_pipeline import plotly.express as px # Simulate data np.random.seed(42) n_samples = 100 income = np.random.randint(30000, 200000, n_samples) investment = 0.000001 * (income ** 2) - 0.05 * income + 10000 + np.random.normal(0, 5000, n_samples) df = pd.DataFrame({'Monthly_Income': income, 'MF_Investment': investment}) # Train polynomial model X = df[['Monthly_Income']] y = df['MF_Investment'] model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression()) model.fit(X, y) # Predict and round df['Predicted_Investment'] = model.predict(X).round(0).astype(int) # Sort by Monthly Income df = df.sort_values(by='Monthly_Income') # Prediction function using min/max income def investment_predictor(min_income, max_income): filtered_df = df[(df['Monthly_Income'] >= min_income) & (df['Monthly_Income'] <= max_income)] table = filtered_df[['Monthly_Income', 'Predicted_Investment']].head(20) fig = px.scatter(filtered_df, x='Monthly_Income', y='MF_Investment', title="📉 Actual vs Predicted Investment", labels={'MF_Investment': 'Actual Investment'}, color_discrete_sequence=["blue"]) fig.add_traces(px.line(filtered_df, x='Monthly_Income', y='Predicted_Investment').data) fig.update_traces(name='Predicted Investment', selector=dict(mode='lines')) return table, fig # UI components min_income_slider = gr.Slider(minimum=int(df['Monthly_Income'].min()), maximum=int(df['Monthly_Income'].max()), step=1000, value=int(df['Monthly_Income'].min()), label="Minimum Monthly Income (₹)") max_income_slider = gr.Slider(minimum=int(df['Monthly_Income'].min()), maximum=int(df['Monthly_Income'].max()), step=1000, value=int(df['Monthly_Income'].max()), label="Maximum Monthly Income (₹)") # Interface demo = gr.Interface( fn=investment_predictor, inputs=[min_income_slider, max_income_slider], outputs=[ gr.Dataframe(label="📊 Prediction Table (Top 20)"), gr.Plot(label="📉 Actual vs Predicted Investment") ], title="📈 Mutual Fund Investment Predictor ", description="This dashboard uses Polynomial Regression to predict mutual fund investments based on monthly income." ) demo.launch()