Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,10 @@ import requests
|
|
4 |
# FastAPI endpoint URL
|
5 |
API_URL = "https://molinari135-product-return-prediction-api.hf.space/predict/"
|
6 |
|
7 |
-
# Gradio Interface function
|
8 |
def predict_return(selected_products, total_customer_purchases, total_customer_returns):
|
9 |
# Input validation for returns (must be <= purchases)
|
10 |
if total_customer_returns > total_customer_purchases:
|
11 |
-
return "Error: Total returns cannot be greater than total purchases."
|
12 |
|
13 |
# Prepare the request data
|
14 |
models = []
|
@@ -31,6 +30,8 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
|
|
31 |
"total_customer_returns": total_customer_returns
|
32 |
}
|
33 |
|
|
|
|
|
34 |
try:
|
35 |
# Make the POST request to the FastAPI endpoint
|
36 |
response = requests.post(API_URL, json=data)
|
@@ -41,19 +42,18 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
|
|
41 |
predictions = result.get('predictions', [])
|
42 |
|
43 |
if not predictions:
|
44 |
-
return "Error: No predictions found."
|
45 |
|
46 |
# Format the output to display nicely
|
47 |
-
formatted_result = []
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
return formatted_result, f"Total Products in Cart: {total}"
|
54 |
|
55 |
except requests.exceptions.RequestException as e:
|
56 |
-
return f"Error: {str(e)}", ""
|
57 |
|
58 |
|
59 |
# Predefined list of model-fabric-color combinations
|
|
|
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 = []
|
|
|
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 |
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 |
+
total_products_in_cart = f"Total Products in Cart: {len(predictions)}"
|
51 |
|
52 |
+
# Return all the required values
|
53 |
+
return formatted_result, total_products_in_cart, ""
|
|
|
54 |
|
55 |
except requests.exceptions.RequestException as e:
|
56 |
+
return f"Error: {str(e)}", "", ""
|
57 |
|
58 |
|
59 |
# Predefined list of model-fabric-color combinations
|