molinari135 commited on
Commit
8a98a0c
·
verified ·
1 Parent(s): 6579b4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -37
app.py CHANGED
@@ -13,57 +13,95 @@ dataset = load_dataset("molinari135/armani-inventory", token=hf_token, data_file
13
  inventory = pd.DataFrame(dataset['train']).head(50)
14
 
15
  # Gradio Interface function
16
- def predict_return(selected_indices, 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
- # Selezionare i prodotti scelti usando gli indici
22
- selected_products = inventory.iloc[selected_indices]
23
-
24
- # Preparare i dati per la predizione
25
- models = selected_products['Item Brand Model'].tolist()
26
- fabrics = selected_products['Item Brand Fabric'].tolist()
27
- colours = selected_products['Item Brand Colour'].tolist()
28
-
29
- # Preparazione per il calcolo del carrello
30
- cart_details = "\n".join([
31
- f"{row['Item Brand Model']} - {row['Product Type']}, {row['Main Material']}, Sales: {row['Net Sales (FA)']} USD"
32
- for _, row in selected_products.iterrows()
33
- ])
34
-
35
- # Calcolare il totale del carrello
36
- total_value = selected_products['Net Sales (FA)'].sum()
37
-
38
- # Simula la predizione
39
- # (In un'applicazione reale, invia questi dati a un endpoint API per fare la predizione)
40
- predictions = [
41
- f"{model} - No Return (Confidence: 0.95%)" for model in models
42
- ]
43
-
44
- # Formatta il risultato per visualizzazione
45
- formatted_result = "\n".join(predictions)
46
- total_cart = f"Total Cart Value: {total_value} USD"
47
-
48
- return cart_details, formatted_result, total_cart
49
 
50
- # Gradio interface elements
51
- checkboxes = [
52
- gr.Checkbox(label=f"{row['Item Brand Model']} - {row['Item Brand Fabric']} - {row['Item Brand Colour']}", value=False)
53
- for _, row in inventory.iterrows()
54
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  interface = gr.Interface(
57
  fn=predict_return, # Funzione per la logica di predizione
58
  inputs=[
59
- gr.Column([*checkboxes], label="Select Products"), # Aggiungi tutte le checkbox
 
 
 
 
60
  gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
61
  gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
62
  ],
63
  outputs=[
64
  gr.Textbox(label="Cart Details"), # Dettagli del carrello
65
- gr.Textbox(label="Prediction Results"), # Risultati della predizione
66
- gr.Textbox(label="Total Cart") # Totale del carrello
67
  ],
68
  live=True # Interattività in tempo reale
69
  )
 
13
  inventory = pd.DataFrame(dataset['train']).head(50)
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
30
+ model, fabric, color = selected_product.split("-")
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['Net Sales (FA)'].values[0]
45
+ total_value += product_value
46
+
47
+ # Add description to the cart
48
+ description = (
49
+ f"Model: {model}, Fabric: {fabric}, Colour: {color}, "
50
+ f"Product Type: {product_details['Product Type'].values[0]}, "
51
+ f"Material: {product_details['Main Material'].values[0]}, "
52
+ f"Sales Value: {product_value} USD"
53
+ )
54
+ descriptions.append(description)
55
+ else:
56
+ descriptions.append(f"{model}-{fabric}-{color}: Not Found")
57
+
58
+ # Prepare the data to send to the API
59
+ data = {
60
+ "models": models,
61
+ "fabrics": fabrics,
62
+ "colours": colours,
63
+ "total_customer_purchases": total_customer_purchases,
64
+ "total_customer_returns": total_customer_returns
65
+ }
66
+
67
+ try:
68
+ # Make the POST request to the FastAPI endpoint
69
+ response = requests.post(API_URL, json=data)
70
+ response.raise_for_status() # Raise an error for bad responses
71
+
72
+ # Get the predictions and return them
73
+ result = response.json()
74
+ predictions = result.get('predictions', [])
75
+
76
+ if not predictions:
77
+ return "Error: No predictions found."
78
 
79
+ # Format the cart output
80
+ cart_output = "\n".join(descriptions) + f"\nTotal Cart Value: {total_value} USD"
81
+
82
+ # Format the prediction results
83
+ formatted_result = "\n".join([f"Product: {pred['product']} \t Prediction: {pred['prediction']} \t Confidence: {pred['confidence']}%" for pred in predictions])
84
+
85
+ return cart_output, formatted_result
86
+
87
+ except requests.exceptions.RequestException as e:
88
+ return f"Error: {str(e)}"
89
+
90
+ # Gradio interface elements
91
  interface = gr.Interface(
92
  fn=predict_return, # Funzione per la logica di predizione
93
  inputs=[
94
+ gr.CheckboxGroup(
95
+ choices=[f"{row['Item Brand Model']}-{row['Item Brand Fabric']}-{row['Item Brand Colour']}"
96
+ for _, row in inventory.iterrows()],
97
+ label="Select Products"
98
+ ),
99
  gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
100
  gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
101
  ],
102
  outputs=[
103
  gr.Textbox(label="Cart Details"), # Dettagli del carrello
104
+ gr.Textbox(label="Prediction Results") # Risultati della predizione
 
105
  ],
106
  live=True # Interattività in tempo reale
107
  )