Prince Vaviya
upload models
5e787b5
import gradio as gr
import pandas as pd
import numpy as np
import pickle
# Load models
model = pickle.load(open("logistic_model.pkl", "rb"))
scaler = pickle.load(open("scaler_lr.pkl", "rb"))
kmeans = pickle.load(open("kmeans_model.pkl", "rb"))
feature_columns = pickle.load(open("feature_columns.pkl", "rb"))
# Prediction function
def predict(price, rating, discount, category):
input_df = pd.DataFrame(columns=feature_columns)
input_df.loc[0] = 0
input_df["discount_price"] = price
input_df["ratings"] = rating
input_df["discount_percent"] = discount
cluster = kmeans.predict([[price, rating, 3, discount]])[0]
cluster_col = f"cluster_{cluster}"
if cluster_col in input_df.columns:
input_df[cluster_col] = 1
category_col = f"main_category_{category}"
if category_col in input_df.columns:
input_df[category_col] = 1
input_scaled = scaler.transform(input_df)
prob = model.predict_proba(input_scaled)[0][1]
prob_percent = round(prob * 100, 2)
if prob > 0.7:
recommendation = "Strong demand expected. Pricing is optimal."
elif prob > 0.4:
recommendation = "Moderate demand. Consider adjusting price or discount."
else:
recommendation = "Low demand probability. Consider lowering price."
return f"{prob_percent}%", recommendation
# Interface
interface = gr.Interface(
fn=predict,
inputs=[
gr.Number(label="Product Price (₹)", value=1000),
gr.Slider(0,5,value=4,label="Rating"),
gr.Slider(0,90,value=30,label="Discount %"),
gr.Dropdown(
[
"beauty & health",
"grocery & gourmet foods",
"home & kitchen",
"kids' fashion",
"men's shoes",
"stores",
"toys & baby products",
"tv, audio & cameras"
],
label="Category"
)
],
outputs=[
gr.Text(label="High Demand Probability"),
gr.Text(label="Pricing Recommendation")
],
title="Amazon Pricing Optimization System",
description="Predict product demand probability based on pricing, rating, discount, and category."
)
interface.launch()