Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.svm import SVC
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
|
| 7 |
+
# Sample mock data (Morningstar-like)
|
| 8 |
+
data = {
|
| 9 |
+
"5Y_Return": [14.0, 7.5, 13.2, 6.0, 15.0, 8.0, 12.0, 6.5, 10.5, 7.2],
|
| 10 |
+
"Volatility": [8.0, 6.5, 7.8, 9.0, 7.0, 6.2, 7.1, 8.5, 6.8, 7.9],
|
| 11 |
+
"Risk_Score": [2, 3, 2, 4, 1, 3, 2, 4, 2, 3],
|
| 12 |
+
"Rating": ["Good", "Bad", "Good", "Bad", "Good", "Bad", "Good", "Bad", "Good", "Bad"]
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
df = pd.DataFrame(data)
|
| 16 |
+
|
| 17 |
+
# Convert labels to binary (Good = 1, Bad = 0)
|
| 18 |
+
df['Label'] = df['Rating'].map({'Good': 1, 'Bad': 0})
|
| 19 |
+
|
| 20 |
+
# Features and labels
|
| 21 |
+
X = df[["5Y_Return", "Volatility", "Risk_Score"]]
|
| 22 |
+
y = df["Label"]
|
| 23 |
+
|
| 24 |
+
# Scale features
|
| 25 |
+
scaler = StandardScaler()
|
| 26 |
+
X_scaled = scaler.fit_transform(X)
|
| 27 |
+
|
| 28 |
+
# Train an SVM classifier
|
| 29 |
+
model = SVC(kernel="linear", probability=True)
|
| 30 |
+
model.fit(X_scaled, y)
|
| 31 |
+
|
| 32 |
+
# Prediction function for Gradio
|
| 33 |
+
def classify_fund(return_5y, volatility, risk_score):
|
| 34 |
+
input_data = [[return_5y, volatility, risk_score]]
|
| 35 |
+
input_scaled = scaler.transform(input_data)
|
| 36 |
+
prediction = model.predict(input_scaled)[0]
|
| 37 |
+
confidence = model.predict_proba(input_scaled)[0][prediction]
|
| 38 |
+
result = "Good Investment" if prediction == 1 else "Bad Investment"
|
| 39 |
+
return f"{result} (Confidence: {confidence:.2f})"
|
| 40 |
+
|
| 41 |
+
# Gradio UI
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## 🧠 SVM Classifier: Is this a Good Mutual Fund?")
|
| 44 |
+
with gr.Row():
|
| 45 |
+
return_input = gr.Number(label="5-Year Return (%)", value=10.0)
|
| 46 |
+
vol_input = gr.Number(label="Volatility (%)", value=7.0)
|
| 47 |
+
risk_input = gr.Number(label="Risk Score (1=Low, 5=High)", value=3)
|
| 48 |
+
output = gr.Textbox(label="Prediction")
|
| 49 |
+
|
| 50 |
+
classify_btn = gr.Button("Classify Fund")
|
| 51 |
+
classify_btn.click(fn=classify_fund, inputs=[return_input, vol_input, risk_input], outputs=output)
|
| 52 |
+
|
| 53 |
+
# Launch app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
demo.launch()
|