File size: 2,541 Bytes
b43d7f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import requests

# FastAPI endpoint URL
API_URL = "http://localhost:7860/predict/"


# Gradio Interface function
def predict_return(selected_products, total_customer_purchases, total_customer_returns):
    # Input validation for returns (must be <= purchases)
    if total_customer_returns > total_customer_purchases:
        return "Error: Total returns cannot be greater than total purchases."

    # Prepare the request data
    models = []
    fabrics = []
    colours = []

    for selected_product in selected_products:
        # Split each selected product into model, fabric, and color
        model, fabric, color = selected_product.split("-")
        models.append(model)
        fabrics.append(fabric)
        colours.append(color)

    # Prepare the data to send to the API
    data = {
        "models": models,
        "fabrics": fabrics,
        "colours": colours,
        "total_customer_purchases": total_customer_purchases,
        "total_customer_returns": total_customer_returns
    }

    print(data)

    try:
        # Make the POST request to the FastAPI endpoint
        response = requests.post(API_URL, json=data)
        response.raise_for_status()  # Raise an error for bad responses

        # Get the predictions and return them
        result = response.json()
        predictions = result.get('predictions', [])

        if not predictions:
            return "Error: No predictions found."

        # Format the output to display nicely
        formatted_result = "\n".join([f"Product: {pred['product']} | Prediction: {pred['prediction']} | Confidence: {pred['confidence']}%" for pred in predictions])
        return formatted_result

    except requests.exceptions.RequestException as e:
        return f"Error: {str(e)}"


# Predefined list of model-fabric-color combinations
combinations = [
    "01CA9T-0130C-922",
    "0NG3DT-02003-999",
    "3R1F67-1JCYZ-0092",
    "211740-3R419-06935",
    "6R1J75-1DQSZ-0943"
]

# Gradio interface elements
interface = gr.Interface(
    fn=predict_return,  # Function that handles the prediction logic
    inputs=[
        gr.CheckboxGroup(choices=combinations, label="Select Products"),  # Allow multiple product selections
        gr.Slider(0, 10, step=1, label="Total Customer Purchases", value=0),
        gr.Slider(0, 10, step=1, label="Total Customer Returns", value=0)
    ],
    outputs="text",  # Display predictions as text
    live=True  # To enable the interface to interact live
)

# Launch the Gradio interface
interface.launch()