Yatheshr's picture
Update app.py
d8ec381 verified
import gradio as gr
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Sample mock data (Morningstar-like)
data = {
"5Y_Return": [14.0, 7.5, 13.2, 6.0, 15.0, 8.0, 12.0, 6.5, 10.5, 7.2],
"Volatility": [8.0, 6.5, 7.8, 9.0, 7.0, 6.2, 7.1, 8.5, 6.8, 7.9],
"Risk_Score": [2, 3, 2, 4, 1, 3, 2, 4, 2, 3],
"Rating": ["Good", "Bad", "Good", "Bad", "Good", "Bad", "Good", "Bad", "Good", "Bad"]
}
df = pd.DataFrame(data)
# Convert labels to binary (Good = 1, Bad = 0)
df['Label'] = df['Rating'].map({'Good': 1, 'Bad': 0})
# Features and labels
X = df[["5Y_Return", "Volatility", "Risk_Score"]]
y = df["Label"]
# Scale features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Train an SVM classifier
model = SVC(kernel="linear", probability=True)
model.fit(X_scaled, y)
# Prediction function for Gradio
def classify_fund(return_5y, volatility, risk_score):
input_data = [[return_5y, volatility, risk_score]]
input_scaled = scaler.transform(input_data)
prediction = model.predict(input_scaled)[0]
confidence = model.predict_proba(input_scaled)[0][prediction]
result = "Good Investment" if prediction == 1 else "Bad Investment"
return f"{result} (Confidence: {confidence:.2f})"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🧠 SVM Classifier: Is this a Good Mutual Fund?")
with gr.Row():
return_input = gr.Number(label="5-Year Return (%)", value=10.0)
vol_input = gr.Number(label="Volatility (%)", value=7.0)
risk_input = gr.Number(label="Risk Score (1=Low, 5=High)", value=3)
output = gr.Textbox(label="Prediction")
classify_btn = gr.Button("Classify Fund")
classify_btn.click(fn=classify_fund, inputs=[return_input, vol_input, risk_input], outputs=output)
# Launch app
if __name__ == "__main__":
demo.launch()