Spaces:
Sleeping
Sleeping
import gradio as gr | |
import joblib | |
import pandas as pd | |
import xgboost as xgb | |
import numpy as np | |
from sklearn.preprocessing import LabelEncoder | |
# Load the trained model | |
model = joblib.load(r"mutual_fund_model.pkl") | |
def predict_mutual_fund(min_sip, min_lumpsum, expense_ratio, fund_size_cr, fund_age_yr, | |
fund_manager, sortino, alpha, sd, amc_name, rating, | |
category, sub_category): | |
# Add the missing features with default values | |
input_data = pd.DataFrame({ | |
'scheme_name': ['N/A'], # Default placeholder value | |
'min_sip': [min_sip], | |
'min_lumpsum': [min_lumpsum], | |
'expense_ratio': [expense_ratio], | |
'fund_size_cr': [fund_size_cr], | |
'fund_age_yr': [fund_age_yr], | |
'fund_manager': [fund_manager], | |
'sortino': [sortino], | |
'alpha': [alpha], | |
'sd': [sd], | |
'beta': [0.0], # Default placeholder value | |
'sharpe': [0.0], # Default placeholder value | |
'risk_level': ['low'], # Default placeholder value | |
'amc_name': [amc_name], | |
'rating': [rating], | |
'category': [category], | |
'sub_category': [sub_category], | |
'returns_3yr': [0.0], # Default placeholder value | |
'returns_5yr': [0.0] # Default placeholder value | |
}) | |
# Convert categorical columns to numeric | |
label_encoder = LabelEncoder() | |
input_data['scheme_name'] = label_encoder.fit_transform(input_data['scheme_name']) | |
input_data['risk_level'] = label_encoder.fit_transform(input_data['risk_level']) | |
# Use the model to make predictions | |
predictions = model.predict(input_data) | |
# Handle multi-dimensional predictions | |
if isinstance(predictions, np.ndarray) and predictions.ndim == 2: | |
rounded_predictions = predictions[0] # Take the first row (single input case) | |
rounded_predictions = [round(float(val), 4) for val in rounded_predictions] | |
else: | |
raise ValueError("Unexpected output from model. Expected a 2D array.") | |
# Print the result (for debugging purposes) | |
print("Rounded Predictions:", rounded_predictions) | |
# Return the rounded predictions in the desired format | |
return { | |
'1M Return': rounded_predictions[0], | |
'3M Return': rounded_predictions[1], | |
'6M Return': rounded_predictions[2], | |
'1Y Return': rounded_predictions[3] | |
} | |
# Create the Gradio interface | |
iface = gr.Interface( | |
fn=predict_mutual_fund, | |
inputs=[ | |
gr.Number(label="Minimum SIP Amount (₹)"), | |
gr.Number(label="Minimum Lump Sum Amount (₹)"), | |
gr.Number(label="Expense Ratio (%)"), | |
gr.Number(label="Fund Size (in Cr)"), | |
gr.Number(label="Fund Age (in Years)"), | |
gr.Number(label="Fund Manager ID"), | |
gr.Number(label="Sortino Ratio"), | |
gr.Number(label="Alpha"), | |
gr.Number(label="Standard Deviation (SD)"), | |
gr.Number(label="AMC Name ID"), | |
gr.Number(label="Rating (1-5)"), | |
gr.Number(label="Category ID"), | |
gr.Number(label="Sub-Category ID") | |
], | |
outputs=[ | |
gr.JSON(label="Predicted Returns (1M, 3M, 6M, 1Y)") | |
], | |
live=False, | |
title="Mutual Fund Return Prediction", | |
description="Enter the details of the mutual fund to predict its returns over 1M, 3M, 6M, and 1Y." | |
) | |
# Launch the interface | |
iface.launch() | |