molinari135 commited on
Commit
0eedb7e
·
verified ·
1 Parent(s): 25d6f86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -79
app.py CHANGED
@@ -1,79 +1,79 @@
1
- import gradio as gr
2
- import requests
3
-
4
- # FastAPI endpoint URL
5
- API_URL = "https://molinari135-product-return-prediction.hf.space/predict/"
6
-
7
-
8
- # Gradio Interface function
9
- def predict_return(selected_products, total_customer_purchases, total_customer_returns):
10
- # Input validation for returns (must be <= purchases)
11
- if total_customer_returns > total_customer_purchases:
12
- return "Error: Total returns cannot be greater than total purchases."
13
-
14
- # Prepare the request data
15
- models = []
16
- fabrics = []
17
- colours = []
18
-
19
- for selected_product in selected_products:
20
- # Split each selected product into model, fabric, and color
21
- model, fabric, color = selected_product.split("-")
22
- models.append(model)
23
- fabrics.append(fabric)
24
- colours.append(color)
25
-
26
- # Prepare the data to send to the API
27
- data = {
28
- "models": models,
29
- "fabrics": fabrics,
30
- "colours": colours,
31
- "total_customer_purchases": total_customer_purchases,
32
- "total_customer_returns": total_customer_returns
33
- }
34
-
35
- print(data)
36
-
37
- try:
38
- # Make the POST request to the FastAPI endpoint
39
- response = requests.post(API_URL, json=data)
40
- response.raise_for_status() # Raise an error for bad responses
41
-
42
- # Get the predictions and return them
43
- result = response.json()
44
- predictions = result.get('predictions', [])
45
-
46
- if not predictions:
47
- return "Error: No predictions found."
48
-
49
- # Format the output to display nicely
50
- formatted_result = "\n".join([f"Product: {pred['product']} | Prediction: {pred['prediction']} | Confidence: {pred['confidence']}%" for pred in predictions])
51
- return formatted_result
52
-
53
- except requests.exceptions.RequestException as e:
54
- return f"Error: {str(e)}"
55
-
56
-
57
- # Predefined list of model-fabric-color combinations
58
- combinations = [
59
- "01CA9T-0130C-922",
60
- "0NG3DT-02003-999",
61
- "3R1F67-1JCYZ-0092",
62
- "211740-3R419-06935",
63
- "6R1J75-1DQSZ-0943"
64
- ]
65
-
66
- # Gradio interface elements
67
- interface = gr.Interface(
68
- fn=predict_return, # Function that handles the prediction logic
69
- inputs=[
70
- gr.CheckboxGroup(choices=combinations, label="Select Products"), # Allow multiple product selections
71
- gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
72
- gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
73
- ],
74
- outputs="text", # Display predictions as text
75
- live=True # To enable the interface to interact live
76
- )
77
-
78
- # Launch the Gradio interface
79
- interface.launch()
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # FastAPI endpoint URL
5
+ API_URL = "https://molinari135-product-return-prediction-api.hf.space"
6
+
7
+
8
+ # Gradio Interface function
9
+ def predict_return(selected_products, total_customer_purchases, total_customer_returns):
10
+ # Input validation for returns (must be <= purchases)
11
+ if total_customer_returns > total_customer_purchases:
12
+ return "Error: Total returns cannot be greater than total purchases."
13
+
14
+ # Prepare the request data
15
+ models = []
16
+ fabrics = []
17
+ colours = []
18
+
19
+ for selected_product in selected_products:
20
+ # Split each selected product into model, fabric, and color
21
+ model, fabric, color = selected_product.split("-")
22
+ models.append(model)
23
+ fabrics.append(fabric)
24
+ colours.append(color)
25
+
26
+ # Prepare the data to send to the API
27
+ data = {
28
+ "models": models,
29
+ "fabrics": fabrics,
30
+ "colours": colours,
31
+ "total_customer_purchases": total_customer_purchases,
32
+ "total_customer_returns": total_customer_returns
33
+ }
34
+
35
+ print(data)
36
+
37
+ try:
38
+ # Make the POST request to the FastAPI endpoint
39
+ response = requests.post(API_URL, json=data)
40
+ response.raise_for_status() # Raise an error for bad responses
41
+
42
+ # Get the predictions and return them
43
+ result = response.json()
44
+ predictions = result.get('predictions', [])
45
+
46
+ if not predictions:
47
+ return "Error: No predictions found."
48
+
49
+ # Format the output to display nicely
50
+ formatted_result = "\n".join([f"Product: {pred['product']} | Prediction: {pred['prediction']} | Confidence: {pred['confidence']}%" for pred in predictions])
51
+ return formatted_result
52
+
53
+ except requests.exceptions.RequestException as e:
54
+ return f"Error: {str(e)}"
55
+
56
+
57
+ # Predefined list of model-fabric-color combinations
58
+ combinations = [
59
+ "01CA9T-0130C-922",
60
+ "0NG3DT-02003-999",
61
+ "3R1F67-1JCYZ-0092",
62
+ "211740-3R419-06935",
63
+ "6R1J75-1DQSZ-0943"
64
+ ]
65
+
66
+ # Gradio interface elements
67
+ interface = gr.Interface(
68
+ fn=predict_return, # Function that handles the prediction logic
69
+ inputs=[
70
+ gr.CheckboxGroup(choices=combinations, label="Select Products"), # Allow multiple product selections
71
+ gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
72
+ gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
73
+ ],
74
+ outputs="text", # Display predictions as text
75
+ live=True # To enable the interface to interact live
76
+ )
77
+
78
+ # Launch the Gradio interface
79
+ interface.launch()