|
import gradio as gr |
|
import requests |
|
|
|
|
|
API_URL = "http://localhost:7860/predict/" |
|
|
|
|
|
|
|
def predict_return(selected_products, total_customer_purchases, total_customer_returns): |
|
|
|
if total_customer_returns > total_customer_purchases: |
|
return "Error: Total returns cannot be greater than total purchases." |
|
|
|
|
|
models = [] |
|
fabrics = [] |
|
colours = [] |
|
|
|
for selected_product in selected_products: |
|
|
|
model, fabric, color = selected_product.split("-") |
|
models.append(model) |
|
fabrics.append(fabric) |
|
colours.append(color) |
|
|
|
|
|
data = { |
|
"models": models, |
|
"fabrics": fabrics, |
|
"colours": colours, |
|
"total_customer_purchases": total_customer_purchases, |
|
"total_customer_returns": total_customer_returns |
|
} |
|
|
|
print(data) |
|
|
|
try: |
|
|
|
response = requests.post(API_URL, json=data) |
|
response.raise_for_status() |
|
|
|
|
|
result = response.json() |
|
predictions = result.get('predictions', []) |
|
|
|
if not predictions: |
|
return "Error: No predictions found." |
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
combinations = [ |
|
"01CA9T-0130C-922", |
|
"0NG3DT-02003-999", |
|
"3R1F67-1JCYZ-0092", |
|
"211740-3R419-06935", |
|
"6R1J75-1DQSZ-0943" |
|
] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_return, |
|
inputs=[ |
|
gr.CheckboxGroup(choices=combinations, label="Select Products"), |
|
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", |
|
live=True |
|
) |
|
|
|
|
|
interface.launch() |
|
|