kritish205 commited on
Commit
e08b5ce
·
verified ·
1 Parent(s): 70ad81d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +83 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import gradio as gr
4
+ import requests
5
+ import json
6
+
7
+ # --- Configuration ---
8
+ # IMPORTANT: Replace this with the URL of your deployed Flask API
9
+ API_URL = "https://kritish205/supercart-backend/predict"
10
+
11
+ # --- Prediction Function ---
12
+ # This function takes all the inputs from the UI, sends them to the API, and gets a prediction.
13
+ def predict_sales(
14
+ product_weight,
15
+ product_mrp,
16
+ product_sugar_content,
17
+ product_allocated_area,
18
+ product_type,
19
+ store_age,
20
+ store_size,
21
+ store_location_city_type,
22
+ store_type,
23
+ ):
24
+ """Makes a request to the backend API and returns the prediction."""
25
+ if "YOUR_BACKEND_API_URL_HERE" in API_URL:
26
+ return "ERROR: Please update the API_URL in the app.py script."
27
+
28
+ # Create the JSON payload for the API
29
+ payload = {
30
+ "Product_Weight": product_weight,
31
+ "Product_MRP": product_mrp,
32
+ "Product_Sugar_Content": product_sugar_content,
33
+ "Product_Allocated_Area": product_allocated_area,
34
+ "Product_Type": product_type,
35
+ "Store_Age": store_age,
36
+ "Store_Size": store_size,
37
+ "Store_Location_City_Type": store_location_city_type,
38
+ "Store_Type": store_type,
39
+ }
40
+
41
+ try:
42
+ # Send the request to the Flask API
43
+ response = requests.post(API_URL, json=payload)
44
+ response.raise_for_status() # Raise an exception for bad status codes
45
+ result = response.json()
46
+ predicted_sales = result.get('predicted_sales', 'N/A')
47
+ return f"${predicted_sales:,.2f}"
48
+ except requests.exceptions.RequestException as e:
49
+ return f"API Connection Error: {e}"
50
+ except Exception as e:
51
+ return f"An error occurred: {str(e)}"
52
+
53
+ # --- UI Interface ---
54
+ # Define the input and output components for the Gradio UI
55
+ product_type_options = [
56
+ 'Snack Foods', 'Household', 'Frozen Foods', 'Fruits and Vegetables',
57
+ 'Health and Hygiene', 'Dairy', 'Baking Goods', 'Canned', 'Meat',
58
+ 'Soft Drinks', 'Breads', 'Hard Drinks', 'Starchy Foods', 'Breakfast',
59
+ 'Seafood', 'Others'
60
+ ]
61
+
62
+ iface = gr.Interface(
63
+ fn=predict_sales,
64
+ inputs=[
65
+ gr.Number(label="Product Weight (kg)", value=10.0),
66
+ gr.Number(label="Product MRP ($)", value=150.0),
67
+ gr.Radio(["No Sugar", "Low Sugar", "Regular"], label="Product Sugar Content", value="Low Sugar"),
68
+ gr.Slider(0.0, 0.3, value=0.05, label="Product Allocated Area (Ratio)"),
69
+ gr.Dropdown(product_type_options, label="Product Type", value="Snack Foods"),
70
+ gr.Number(label="Store Age (Years)", value=15),
71
+ gr.Radio(["Small", "Medium", "High"], label="Store Size", value="Medium"),
72
+ gr.Radio(["Tier 1", "Tier 2", "Tier 3"], label="Store Location City Type", value="Tier 2"),
73
+ gr.Dropdown(["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"], label="Store Type", value="Supermarket Type1"),
74
+ ],
75
+ outputs=gr.Textbox(label="Predicted Sales"),
76
+ title="🛒 SuperKart Sales Predictor",
77
+ description="This app predicts the total sales for a product in a given store. Fill in the details and click 'Submit'.",
78
+ allow_flagging="never"
79
+ )
80
+
81
+ # Launch the app
82
+ if __name__ == "__main__":
83
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio==4.31.5
2
+ requests==2.31.0