File size: 3,607 Bytes
374f2ba
 
8c05f1f
374f2ba
 
8c05f1f
 
 
374f2ba
 
 
 
 
 
 
 
 
 
 
8c05f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374f2ba
 
 
 
 
8c05f1f
374f2ba
 
 
 
 
8c05f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374f2ba
8c05f1f
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
import gradio as gr
import pandas as pd
import numpy as np
import requests

def predict_sales(product_weight, product_sugar_content, product_allocated_area, product_type, product_mrp, 
                  store_establishment_year, store_size, store_location_city_type, store_type):
    # Create input dictionary
    sample = {
        'Product_Weight': product_weight,
        'Product_Sugar_Content': product_sugar_content,
        'Product_Allocated_Area': product_allocated_area,
        'Product_Type': product_type,
        'Product_MRP': product_mrp,
        'Store_Establishment_Year': store_establishment_year,
        'Store_Size': store_size,
        'Store_Location_City_Type': store_location_city_type,
        'Store_Type': store_type
    }

    # Convert to DataFrame
    features_df = pd.DataFrame([sample])

    # Apply one-hot encoding for nominal columns (matching backend)
    features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)

    # Apply ordinal encoding (based on backend mappings)
    sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
    size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
    city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}

    features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
    features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
    features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)

    # Call the backend API
    backend_url = "https://Hugo014-TotalSalesPredictionBackend.hf.space/v1/sales"
    try:
        response = requests.post(backend_url, json=sample)
        if response.status_code == 200:
            result = response.json()
            predicted_sales = result['Predicted Sales Total (in dollars)']
            return f"The predicted sales total for the product is ${predicted_sales:.2f}."
        else:
            return f"Backend error: {response.status_code} - {response.text}"
    except Exception as e:
        return f"Error calling backend: {str(e)}"

# Gradio interface
demo = gr.Interface(
    fn=predict_sales,
    inputs=[
        gr.Number(label="Product Weight", value=10.0, minimum=0.0, step=0.1),
        gr.Dropdown(label="Product Sugar Content", choices=["No Sugar", "Low Sugar", "Regular"], value="Low Sugar"),
        gr.Number(label="Product Allocated Area (sq ft)", value=500.0, minimum=0.0, step=1.0),
        gr.Dropdown(label="Product Type", choices=[
            "Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables", "Snack Foods", "Household",
            "Frozen Foods", "Baking Goods", "Canned", "Health and Hygiene", "Hard Drinks",
            "Breads", "Starchy Foods", "Breakfast", "Seafood", "Others"
        ], value="Dairy"),
        gr.Number(label="Product MRP (price)", value=100.0, minimum=0.0, step=1.0),
        gr.Number(label="Store Establishment Year", value=2000, minimum=1900, maximum=2025, step=1),
        gr.Dropdown(label="Store Size", choices=["Small", "Medium", "High"], value="Medium"),
        gr.Dropdown(label="Store Location City Type", choices=["Tier 3", "Tier 2", "Tier 1"], value="Tier 1"),
        gr.Dropdown(label="Store Type", choices=[
            "Grocery Store", "Supermarket Type1", "Supermarket Type2", "Supermarket Type3"
        ], value="Supermarket Type1")
    ],
    outputs=gr.Textbox(label="Prediction Result"),
    title="Super Kart Product Sales Prediction App",
    description="This tool predicts the total sales for a product based on store and product details."
)

demo.launch()