molinari135 commited on
Commit
2484028
·
verified ·
1 Parent(s): 21b2b8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -13
app.py CHANGED
@@ -4,7 +4,6 @@ import requests
4
  # FastAPI endpoint URL
5
  API_URL = "https://molinari135-product-return-prediction-api.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)
@@ -32,8 +31,6 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
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)
@@ -47,11 +44,16 @@ def predict_return(selected_products, total_customer_purchases, total_customer_r
47
  return "Error: No predictions found."
48
 
49
  # Format the output to display nicely
50
- formatted_result = "\n".join([f"Product: {pred['product']} \t Prediction: {pred['prediction']} \t 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
@@ -64,16 +66,44 @@ combinations = [
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()
 
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)
 
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)
 
44
  return "Error: No predictions found."
45
 
46
  # Format the output to display nicely
47
+ formatted_result = []
48
+ for pred in predictions:
49
+ formatted_result.append(f"{pred['product']} - {pred['prediction']} (Confidence: {pred['confidence']}%)")
50
+
51
+ # Calculate total products in cart
52
+ total = len(predictions)
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
 
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 to process predictions
95
  inputs=[
96
+ inventory_checkbox_group, # Left side: Inventory
97
+ total_purchases_slider, # Total purchases
98
+ total_returns_slider # Total returns
99
+ ],
100
+ outputs=[
101
+ predictions_output, # Right side: Cart & Predictions
102
+ user_info_output, # Right side: User Info
103
+ cart_output # Right side: Cart
104
  ],
105
+ live=True # Enable live interaction
 
106
  )
107
 
108
  # Launch the Gradio interface
109
+ interface.launch()