Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
from sklearn.pipeline import Pipeline
|
| 6 |
from sklearn.preprocessing import StandardScaler
|
| 7 |
from sklearn.decomposition import PCA
|
| 8 |
from sklearn.ensemble import RandomForestClassifier
|
| 9 |
|
| 10 |
-
#
|
| 11 |
data = {
|
| 12 |
'Expense_Ratio': [0.7, 0.5, 1.2, 0.9, 0.6, 1.5, 0.3, 0.8, 1.0, 0.4],
|
| 13 |
'AUM': [1000, 1500, 800, 1200, 1100, 500, 1600, 1400, 700, 1800],
|
|
@@ -20,7 +19,7 @@ df = pd.DataFrame(data)
|
|
| 20 |
X = df.drop("Label", axis=1)
|
| 21 |
y = df["Label"]
|
| 22 |
|
| 23 |
-
#
|
| 24 |
pipeline = Pipeline([
|
| 25 |
('scaler', StandardScaler()),
|
| 26 |
('pca', PCA(n_components=2)),
|
|
@@ -28,8 +27,30 @@ pipeline = Pipeline([
|
|
| 28 |
])
|
| 29 |
pipeline.fit(X, y)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
input_df = pd.DataFrame([{
|
| 34 |
"Expense_Ratio": expense_ratio,
|
| 35 |
"AUM": aum,
|
|
@@ -37,12 +58,16 @@ def predict(expense_ratio, aum, risk_score, return_3y, return_5y):
|
|
| 37 |
"3_Year_Return": return_3y,
|
| 38 |
"5_Year_Return": return_5y
|
| 39 |
}])
|
|
|
|
| 40 |
prediction = pipeline.predict(input_df)[0]
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
gr.Interface(
|
| 45 |
-
fn=
|
| 46 |
inputs=[
|
| 47 |
gr.Number(label="Expense Ratio"),
|
| 48 |
gr.Number(label="AUM (in crores)"),
|
|
@@ -50,7 +75,10 @@ gr.Interface(
|
|
| 50 |
gr.Number(label="3-Year Return (%)"),
|
| 51 |
gr.Number(label="5-Year Return (%)"),
|
| 52 |
],
|
| 53 |
-
outputs=
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
).launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
from sklearn.pipeline import Pipeline
|
| 5 |
from sklearn.preprocessing import StandardScaler
|
| 6 |
from sklearn.decomposition import PCA
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
|
| 9 |
+
# Sample data
|
| 10 |
data = {
|
| 11 |
'Expense_Ratio': [0.7, 0.5, 1.2, 0.9, 0.6, 1.5, 0.3, 0.8, 1.0, 0.4],
|
| 12 |
'AUM': [1000, 1500, 800, 1200, 1100, 500, 1600, 1400, 700, 1800],
|
|
|
|
| 19 |
X = df.drop("Label", axis=1)
|
| 20 |
y = df["Label"]
|
| 21 |
|
| 22 |
+
# ML pipeline
|
| 23 |
pipeline = Pipeline([
|
| 24 |
('scaler', StandardScaler()),
|
| 25 |
('pca', PCA(n_components=2)),
|
|
|
|
| 27 |
])
|
| 28 |
pipeline.fit(X, y)
|
| 29 |
|
| 30 |
+
# Radar chart function
|
| 31 |
+
def create_radar_chart(input_data):
|
| 32 |
+
categories = list(input_data.columns)
|
| 33 |
+
values = input_data.iloc[0].tolist()
|
| 34 |
+
|
| 35 |
+
fig = go.Figure(data=go.Scatterpolar(
|
| 36 |
+
r=values + [values[0]],
|
| 37 |
+
theta=categories + [categories[0]],
|
| 38 |
+
fill='toself',
|
| 39 |
+
name='Fund Metrics'
|
| 40 |
+
))
|
| 41 |
+
|
| 42 |
+
fig.update_layout(
|
| 43 |
+
polar=dict(
|
| 44 |
+
radialaxis=dict(visible=True)
|
| 45 |
+
),
|
| 46 |
+
showlegend=False,
|
| 47 |
+
margin=dict(t=10, b=10)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return fig
|
| 51 |
+
|
| 52 |
+
# Main Gradio function
|
| 53 |
+
def predict_and_visualize(expense_ratio, aum, risk_score, return_3y, return_5y):
|
| 54 |
input_df = pd.DataFrame([{
|
| 55 |
"Expense_Ratio": expense_ratio,
|
| 56 |
"AUM": aum,
|
|
|
|
| 58 |
"3_Year_Return": return_3y,
|
| 59 |
"5_Year_Return": return_5y
|
| 60 |
}])
|
| 61 |
+
|
| 62 |
prediction = pipeline.predict(input_df)[0]
|
| 63 |
+
label = "High-performing Fund ✅" if prediction == 1 else "Low-performing Fund ❌"
|
| 64 |
+
chart = create_radar_chart(input_df)
|
| 65 |
+
|
| 66 |
+
return label, chart
|
| 67 |
|
| 68 |
+
# Gradio interface
|
| 69 |
gr.Interface(
|
| 70 |
+
fn=predict_and_visualize,
|
| 71 |
inputs=[
|
| 72 |
gr.Number(label="Expense Ratio"),
|
| 73 |
gr.Number(label="AUM (in crores)"),
|
|
|
|
| 75 |
gr.Number(label="3-Year Return (%)"),
|
| 76 |
gr.Number(label="5-Year Return (%)"),
|
| 77 |
],
|
| 78 |
+
outputs=[
|
| 79 |
+
gr.Textbox(label="Prediction"),
|
| 80 |
+
gr.Plot(label="Fund Score Radar Chart")
|
| 81 |
+
],
|
| 82 |
+
title="Morningstar Fund Classifier with Score Chart",
|
| 83 |
+
description="Predict and visualize if a fund is high or low performing based on its key metrics."
|
| 84 |
).launch()
|