File size: 3,438 Bytes
f7ad21e
 
 
 
 
 
 
 
b99fdee
f7ad21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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()