Spaces:
Sleeping
Sleeping
ingtech
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
|
7 |
+
|
8 |
+
# Load the trained model and scaler objects from file
|
9 |
+
REPO_ID = "Hemg/marketpredict" # Hugging Face repo ID
|
10 |
+
MoDEL_FILENAME = "stx.joblib" # Model file name
|
11 |
+
SCALER_FILENAME = "scaler.joblib" # Scaler file name
|
12 |
+
|
13 |
+
model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
|
14 |
+
scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
|
15 |
+
|
16 |
+
def encode_categorical_columns(df):
|
17 |
+
label_encoder = LabelEncoder()
|
18 |
+
ordinal_columns = df.select_dtypes(include=['object']).columns
|
19 |
+
|
20 |
+
for col in ordinal_columns:
|
21 |
+
df[col] = label_encoder.fit_transform(df[col])
|
22 |
+
|
23 |
+
nominal_columns = df.select_dtypes(include=['object']).columns.difference(ordinal_columns)
|
24 |
+
df = pd.get_dummies(df, columns=nominal_columns, drop_first=True)
|
25 |
+
|
26 |
+
return df
|
27 |
+
|
28 |
+
# Define the prediction function
|
29 |
+
def predict_performance(Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses):
|
30 |
+
# Prepare input data (represents independent variables for house prediction)
|
31 |
+
input_data = [[Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses]]
|
32 |
+
|
33 |
+
# Get the feature names from the Gradio interface inputs
|
34 |
+
feature_names = ["Year", "Instagram_Advertising", "Facebook_Advertising", "Event_Expenses", "Internet_Expenses"]
|
35 |
+
|
36 |
+
# Create a Pandas DataFrame with the input data and feature names
|
37 |
+
input_df = pd.DataFrame(input_data, columns=feature_names)
|
38 |
+
|
39 |
+
input_df = encode_categorical_columns(input_df)
|
40 |
+
|
41 |
+
# Scale the input data using the loaded scaler
|
42 |
+
scaled_input = scaler.transform(input_df)
|
43 |
+
|
44 |
+
# Make predictions using the loaded model
|
45 |
+
prediction = model.predict(scaled_input)[0]
|
46 |
+
|
47 |
+
# Return the result as HTML with custom styling (green color and larger font)
|
48 |
+
return f'<p style="font-size: 24px; color: green;">Forecast no of. Students admission: {prediction:,.0f}</p>'
|
49 |
+
|
50 |
+
# Create the Gradio app
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=predict_performance,
|
53 |
+
inputs=[
|
54 |
+
gr.Slider(minimum=2024, maximum=2025, step=1, label="Year",info="The forecasted Year"),
|
55 |
+
gr.Slider(minimum=10000, maximum=45000, step=500, label="Instagram_Advertising", info="How much do you spend on Instagram ads Yearly($)?"),
|
56 |
+
gr.Slider(minimum=10000, maximum=75000, step=500, label="Facebook_Advertising", info="How much do you spend on Facebook ads Yearly($)?"),
|
57 |
+
gr.Slider(minimum=20000, maximum=100000, step=500, label="Event_Expenses", info="What’s your typical budget for events($)?"),
|
58 |
+
gr.Slider(minimum=5000, maximum=45000, step=500, label="Internet_Expenses", info="How much do you spend on internet Yearly($)?")
|
59 |
+
],
|
60 |
+
outputs=gr.HTML(), # Specify the output as HTML
|
61 |
+
title="Student Admission Forecast",
|
62 |
+
description="Forecast of chances of student admission based on marketing expenditures"
|
63 |
+
)
|
64 |
+
|
65 |
+
# Run the app
|
66 |
+
if __name__ == "__main__":
|
67 |
+
iface.launch(share=True)
|