Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
3 |
|
4 |
# FastAPI endpoint URL
|
5 |
API_URL = "https://molinari135-product-return-prediction-api.hf.space/predict/"
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def predict_return(selected_products, total_customer_purchases, total_customer_returns):
|
8 |
# Input validation for returns (must be <= purchases)
|
9 |
if total_customer_returns > total_customer_purchases:
|
10 |
-
return "Error: Total returns cannot be greater than total purchases."
|
11 |
|
12 |
# Prepare the request data
|
13 |
models = []
|
14 |
fabrics = []
|
15 |
colours = []
|
|
|
|
|
16 |
|
17 |
for selected_product in selected_products:
|
18 |
# Split each selected product into model, fabric, and color
|
@@ -20,6 +31,23 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
|
|
20 |
models.append(model)
|
21 |
fabrics.append(fabric)
|
22 |
colours.append(color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Prepare the data to send to the API
|
25 |
data = {
|
@@ -30,8 +58,6 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
|
|
30 |
"total_customer_returns": total_customer_returns
|
31 |
}
|
32 |
|
33 |
-
print(data)
|
34 |
-
|
35 |
try:
|
36 |
# Make the POST request to the FastAPI endpoint
|
37 |
response = requests.post(API_URL, json=data)
|
@@ -42,67 +68,36 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
|
|
42 |
predictions = result.get('predictions', [])
|
43 |
|
44 |
if not predictions:
|
45 |
-
return "Error: No predictions found."
|
46 |
-
|
47 |
-
# Format the output to display nicely
|
48 |
-
formatted_result = "\n".join([f"{pred['product']} - {pred['prediction']} (Confidence: {pred['confidence']}%)" for pred in predictions])
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
# Return all the required values
|
53 |
-
return formatted_result, total_products_in_cart, ""
|
54 |
|
55 |
-
|
56 |
-
|
57 |
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
"01CA9T-0130C-922",
|
62 |
-
"0NG3DT-02003-999",
|
63 |
-
"3R1F67-1JCYZ-0092",
|
64 |
-
"211740-3R419-06935",
|
65 |
-
"6R1J75-1DQSZ-0943"
|
66 |
-
]
|
67 |
|
68 |
# Gradio interface elements
|
69 |
-
inventory_checkbox_group = gr.CheckboxGroup(choices=combinations, label="Select Products", type="value")
|
70 |
-
|
71 |
-
# Slider elements for total purchases and returns
|
72 |
-
total_purchases_slider = gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0)
|
73 |
-
total_returns_slider = gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
|
74 |
-
|
75 |
-
# Output elements for predictions and cart details
|
76 |
-
cart_output = gr.Textbox(value="", label="Cart", interactive=False)
|
77 |
-
predictions_output = gr.Textbox(value="", label="Prediction Results", interactive=False)
|
78 |
-
|
79 |
-
# User information output
|
80 |
-
user_info_output = gr.Textbox(value="User Information\nTotal Purchases: 0\nTotal Returns: 0", label="User Info", interactive=False)
|
81 |
-
|
82 |
-
# Layout with two main columns: Left (Inventory) and Right (User Info + Cart)
|
83 |
-
with gr.Row():
|
84 |
-
with gr.Column():
|
85 |
-
inventory_checkbox_group # Left side: Inventory
|
86 |
-
with gr.Column():
|
87 |
-
user_info_output # Right side: User Info
|
88 |
-
cart_output # Right side: Cart & Predictions
|
89 |
-
predictions_output # Right side: Prediction Results
|
90 |
-
|
91 |
-
|
92 |
-
# Gradio Interface
|
93 |
interface = gr.Interface(
|
94 |
-
fn=predict_return, # Function
|
95 |
inputs=[
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
99 |
],
|
100 |
outputs=[
|
101 |
-
|
102 |
-
|
103 |
-
cart_output # Right side: Cart
|
104 |
],
|
105 |
-
live=True #
|
106 |
)
|
107 |
|
108 |
# Launch the Gradio interface
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
from datasets import load_dataset
|
6 |
|
7 |
# FastAPI endpoint URL
|
8 |
API_URL = "https://molinari135-product-return-prediction-api.hf.space/predict/"
|
9 |
|
10 |
+
# Load the inventory dataset from Hugging Face
|
11 |
+
hf_token = os.getenv("inventory_data")
|
12 |
+
dataset = load_dataset("molinari135/armani-inventory", token=hf_token, data_files="inventory.tsv")
|
13 |
+
inventory = pd.DataFrame(dataset['train'])
|
14 |
+
|
15 |
+
# Gradio Interface function
|
16 |
def predict_return(selected_products, total_customer_purchases, total_customer_returns):
|
17 |
# Input validation for returns (must be <= purchases)
|
18 |
if total_customer_returns > total_customer_purchases:
|
19 |
+
return "Error: Total returns cannot be greater than total purchases."
|
20 |
|
21 |
# Prepare the request data
|
22 |
models = []
|
23 |
fabrics = []
|
24 |
colours = []
|
25 |
+
descriptions = []
|
26 |
+
total_value = 0
|
27 |
|
28 |
for selected_product in selected_products:
|
29 |
# Split each selected product into model, fabric, and color
|
|
|
31 |
models.append(model)
|
32 |
fabrics.append(fabric)
|
33 |
colours.append(color)
|
34 |
+
|
35 |
+
# Get the product details from the inventory
|
36 |
+
product_details = inventory[
|
37 |
+
(inventory['Item Brand Model'] == model) &
|
38 |
+
(inventory['Item Brand Fabric'] == fabric) &
|
39 |
+
(inventory['Item Brand Colour'] == color)
|
40 |
+
]
|
41 |
+
|
42 |
+
if not product_details.empty:
|
43 |
+
# Calculate the product value and add it to the total
|
44 |
+
product_value = product_details['Total Order Value'].values[0]
|
45 |
+
total_value += product_value
|
46 |
+
|
47 |
+
# Add description to the cart
|
48 |
+
descriptions.append(f"{model}-{fabric}-{color}: {product_value} USD")
|
49 |
+
else:
|
50 |
+
descriptions.append(f"{model}-{fabric}-{color}: Not Found")
|
51 |
|
52 |
# Prepare the data to send to the API
|
53 |
data = {
|
|
|
58 |
"total_customer_returns": total_customer_returns
|
59 |
}
|
60 |
|
|
|
|
|
61 |
try:
|
62 |
# Make the POST request to the FastAPI endpoint
|
63 |
response = requests.post(API_URL, json=data)
|
|
|
68 |
predictions = result.get('predictions', [])
|
69 |
|
70 |
if not predictions:
|
71 |
+
return "Error: No predictions found."
|
|
|
|
|
|
|
72 |
|
73 |
+
# Format the cart output
|
74 |
+
cart_output = "\n".join(descriptions) + f"\nTotal Cart Value: {total_value} USD"
|
|
|
|
|
75 |
|
76 |
+
# Format the prediction results
|
77 |
+
formatted_result = "\n".join([f"Product: {pred['product']} \t Prediction: {pred['prediction']} \t Confidence: {pred['confidence']}%" for pred in predictions])
|
78 |
|
79 |
+
return cart_output, formatted_result
|
80 |
|
81 |
+
except requests.exceptions.RequestException as e:
|
82 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Gradio interface elements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
interface = gr.Interface(
|
86 |
+
fn=predict_return, # Function that handles the prediction logic
|
87 |
inputs=[
|
88 |
+
gr.CheckboxGroup(
|
89 |
+
choices=[f"{row['Item Brand Model']}-{row['Item Brand Fabric']}-{row['Item Brand Colour']}"
|
90 |
+
for _, row in inventory.iterrows()],
|
91 |
+
label="Select Products"
|
92 |
+
),
|
93 |
+
gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
|
94 |
+
gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
|
95 |
],
|
96 |
outputs=[
|
97 |
+
gr.Textbox(label="Cart Details"), # Display cart details
|
98 |
+
gr.Textbox(label="Prediction Results") # Display prediction results
|
|
|
99 |
],
|
100 |
+
live=True # To enable the interface to interact live
|
101 |
)
|
102 |
|
103 |
# Launch the Gradio interface
|