Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
sample = {
|
| 9 |
'Product_Weight': product_weight,
|
| 10 |
'Product_Sugar_Content': product_sugar_content,
|
|
@@ -16,71 +17,58 @@ def predict_sales_single(product_weight, product_sugar_content, product_allocate
|
|
| 16 |
'Store_Location_City_Type': store_location_city_type,
|
| 17 |
'Store_Type': store_type
|
| 18 |
}
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
response = requests.post(backend_url, json=sample)
|
| 22 |
if response.status_code == 200:
|
| 23 |
result = response.json()
|
| 24 |
predicted_sales = result['Predicted Sales Total (in dollars)']
|
| 25 |
-
return f"
|
| 26 |
-
else:
|
| 27 |
-
return f"Backend error: {response.status_code} - {response.text}"
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return f"Error calling backend: {str(e)}"
|
| 30 |
-
|
| 31 |
-
# Function to handle batch prediction via CSV upload
|
| 32 |
-
def predict_sales_batch(csv_file):
|
| 33 |
-
if csv_file is None:
|
| 34 |
-
return "Please upload a CSV file."
|
| 35 |
-
files = {"file": (csv_file.name, csv_file.getvalue(), "text/csv")}
|
| 36 |
-
backend_url = "https://hugo014-totalsalespredictionbackend.hf.space/v1/salesbatch"
|
| 37 |
-
try:
|
| 38 |
-
response = requests.post(backend_url, files=files)
|
| 39 |
-
if response.status_code == 200:
|
| 40 |
-
result = response.json()
|
| 41 |
-
return result # Returns dict like {"0": 123.45, "1": 678.90}
|
| 42 |
else:
|
| 43 |
return f"Backend error: {response.status_code} - {response.text}"
|
| 44 |
except Exception as e:
|
| 45 |
return f"Error calling backend: {str(e)}"
|
| 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 |
-
# inputs=[product_weight, product_sugar_content, product_allocated_area, product_type, product_mrp,
|
| 72 |
-
# store_establishment_year, store_size, store_location_city_type, store_type],
|
| 73 |
-
# outputs=output_single
|
| 74 |
-
# )
|
| 75 |
-
|
| 76 |
-
# with gr.Tab("Batch Prediction"):
|
| 77 |
-
# csv_file = gr.File(label="Upload CSV for Batch Prediction", file_types=[".csv"])
|
| 78 |
-
# output_batch = gr.JSON(label="Batch Prediction Results") # Better for displaying dict output
|
| 79 |
-
# batch_button = gr.Button("Predict Batch")
|
| 80 |
-
# batch_button.click(
|
| 81 |
-
# fn=predict_sales_batch,
|
| 82 |
-
# inputs=csv_file,
|
| 83 |
-
# outputs=output_batch
|
| 84 |
-
# )
|
| 85 |
|
| 86 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
def predict_sales(product_weight, product_sugar_content, product_allocated_area, product_type, product_mrp,
|
| 7 |
+
store_establishment_year, store_size, store_location_city_type, store_type):
|
| 8 |
+
# Create input dictionary
|
| 9 |
sample = {
|
| 10 |
'Product_Weight': product_weight,
|
| 11 |
'Product_Sugar_Content': product_sugar_content,
|
|
|
|
| 17 |
'Store_Location_City_Type': store_location_city_type,
|
| 18 |
'Store_Type': store_type
|
| 19 |
}
|
| 20 |
+
|
| 21 |
+
# Convert to DataFrame
|
| 22 |
+
features_df = pd.DataFrame([sample])
|
| 23 |
+
|
| 24 |
+
# Apply one-hot encoding for nominal columns (matching backend)
|
| 25 |
+
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 26 |
+
|
| 27 |
+
# Apply ordinal encoding (based on backend mappings)
|
| 28 |
+
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 29 |
+
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 30 |
+
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 31 |
+
|
| 32 |
+
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 33 |
+
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 34 |
+
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 35 |
+
|
| 36 |
+
# Call the backend API
|
| 37 |
+
backend_url = "https://Hugo014-TotalSalesPredictionBackend.hf.space/v1/sales"
|
| 38 |
try:
|
| 39 |
response = requests.post(backend_url, json=sample)
|
| 40 |
if response.status_code == 200:
|
| 41 |
result = response.json()
|
| 42 |
predicted_sales = result['Predicted Sales Total (in dollars)']
|
| 43 |
+
return f"The predicted sales total for the product is ${predicted_sales:.2f}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
return f"Backend error: {response.status_code} - {response.text}"
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error calling backend: {str(e)}"
|
| 48 |
|
| 49 |
+
# Gradio interface
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=predict_sales,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Number(label="Product Weight", value=10.0, minimum=0.0, step=0.1),
|
| 54 |
+
gr.Dropdown(label="Product Sugar Content", choices=["No Sugar", "Low Sugar", "Regular"], value="Low Sugar"),
|
| 55 |
+
gr.Number(label="Product Allocated Area (sq ft)", value=500.0, minimum=0.0, step=1.0),
|
| 56 |
+
gr.Dropdown(label="Product Type", choices=[
|
| 57 |
+
"Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables", "Snack Foods", "Household",
|
| 58 |
+
"Frozen Foods", "Baking Goods", "Canned", "Health and Hygiene", "Hard Drinks",
|
| 59 |
+
"Breads", "Starchy Foods", "Breakfast", "Seafood", "Others"
|
| 60 |
+
], value="Dairy"),
|
| 61 |
+
gr.Number(label="Product MRP (price)", value=100.0, minimum=0.0, step=1.0),
|
| 62 |
+
gr.Number(label="Store Establishment Year", value=2000, minimum=1900, maximum=2025, step=1),
|
| 63 |
+
gr.Dropdown(label="Store Size", choices=["Small", "Medium", "High"], value="Medium"),
|
| 64 |
+
gr.Dropdown(label="Store Location City Type", choices=["Tier 3", "Tier 2", "Tier 1"], value="Tier 1"),
|
| 65 |
+
gr.Dropdown(label="Store Type", choices=[
|
| 66 |
+
"Grocery Store", "Supermarket Type1", "Supermarket Type2", "Supermarket Type3"
|
| 67 |
+
], value="Supermarket Type1")
|
| 68 |
+
],
|
| 69 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 70 |
+
title="Super Kart Product Sales Prediction App",
|
| 71 |
+
description="This tool predicts the total sales for a product based on store and product details."
|
| 72 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
demo.launch()
|