market-pred / app.py
Hemg's picture
Update app.py
2c78a39 verified
import gradio as gr
import joblib
import numpy as np
import pandas as pd
from huggingface_hub import hf_hub_download
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
# Load the trained model and scaler objects from file
REPO_ID = "Hemg/marketpredict" # Hugging Face repo ID
MoDEL_FILENAME = "stx.joblib" # Model file name
SCALER_FILENAME = "scaler.joblib" # Scaler file name
model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
def encode_categorical_columns(df):
label_encoder = LabelEncoder()
ordinal_columns = df.select_dtypes(include=['object']).columns
for col in ordinal_columns:
df[col] = label_encoder.fit_transform(df[col])
nominal_columns = df.select_dtypes(include=['object']).columns.difference(ordinal_columns)
df = pd.get_dummies(df, columns=nominal_columns, drop_first=True)
return df
# Define the prediction function
def predict_performance(Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses):
# Prepare input data (represents independent variables for house prediction)
input_data = [[Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses]]
# Get the feature names from the Gradio interface inputs
feature_names = ["Year", "Instagram_Advertising", "Facebook_Advertising", "Event_Expenses", "Internet_Expenses"]
# Create a Pandas DataFrame with the input data and feature names
input_df = pd.DataFrame(input_data, columns=feature_names)
input_df = encode_categorical_columns(input_df)
# Scale the input data using the loaded scaler
scaled_input = scaler.transform(input_df)
# Make predictions using the loaded model
prediction = model.predict(scaled_input)[0]
# Return the result as HTML with custom styling (green color and larger font)
return f'<p style="font-size: 24px; color: green;">Forecast no of. Students admission: {prediction:,.0f}</p>'
# Create the Gradio app
iface = gr.Interface(
fn=predict_performance,
inputs=[
gr.Slider(minimum=2024, maximum=2025, step=1, label="Year",info="The forecasted Year"),
gr.Slider(minimum=10000, maximum=45000, step=500, label="Instagram_Advertising", info="How much do you spend on Instagram ads Yearly($)?"),
gr.Slider(minimum=10000, maximum=75000, step=500, label="Facebook_Advertising", info="How much do you spend on Facebook ads Yearly($)?"),
gr.Slider(minimum=20000, maximum=100000, step=500, label="Event_Expenses", info="What’s your typical budget for events($)?"),
gr.Slider(minimum=5000, maximum=45000, step=500, label="Internet_Expenses", info="How much do you spend on internet Yearly($)?")
],
outputs=gr.HTML(), # Specify the output as HTML
title="Student Admission Forecast",
description="Forecast of chances of student admission based on marketing expenditures"
)
# Run the app
if __name__ == "__main__":
iface.launch(share=True)