molinari135 commited on
Commit
b43d7f1
β€’
1 Parent(s): 9d103e2

Added Gradio

Browse files
Files changed (3) hide show
  1. README.md +1 -0
  2. app.py +79 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -4,6 +4,7 @@ emoji: πŸƒ
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
 
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: docker
7
+ app_file: app.py
8
  pinned: false
9
  ---
10
 
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # FastAPI endpoint URL
5
+ API_URL = "http://localhost:7860/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()
requirements.txt CHANGED
@@ -2,6 +2,7 @@ black
2
  codecarbon
3
  fastapi
4
  flake8
 
5
  ipython
6
  isort
7
  jupyterlab
 
2
  codecarbon
3
  fastapi
4
  flake8
5
+ gradio
6
  ipython
7
  isort
8
  jupyterlab