File size: 3,332 Bytes
0eedb7e d5787cd 0eedb7e f1e321f 8712f8a 0eedb7e f1e321f d5787cd a380fda d5787cd f1e321f 8a98a0c f1e321f 0eedb7e a97c2f2 0eedb7e 8a98a0c 0eedb7e 8a98a0c a380fda f1e321f 8c47187 8a98a0c 9222c92 8a98a0c f1e321f 8a98a0c a97c2f2 6579b4c b2f3264 8a98a0c 77e6014 8a98a0c a97c2f2 8a98a0c e0af3d8 ee45237 a380fda e0af3d8 0eedb7e f1e321f 0eedb7e 8a98a0c f1e321f 8a98a0c d5787cd 2484028 f1e321f 0eedb7e f1e321f 0eedb7e e0af3d8 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import gradio as gr
import requests
import pandas as pd
import os
from datasets import load_dataset
API_URL = "https://molinari135-product-return-prediction-api.hf.space/predict/"
hf_token = os.getenv("inventory_data")
dataset = load_dataset("molinari135/armani-inventory", token=hf_token, data_files="inventory.tsv")
inventory = pd.DataFrame(dataset['train']).sample(n=15, random_state=42)
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 = []
descriptions = []
total_value = 0
for selected_product in selected_products:
product_info = selected_product.split(" \t\t")
model_fabric_colour = product_info[0]
model, fabric, color = model_fabric_colour.split("-")
models.append(model)
fabrics.append(fabric)
colours.append(color)
product_details = inventory[(
inventory['Item Brand Model'] == model) &
(inventory['Item Brand Fabric'] == fabric) &
(inventory['Item Brand Colour'] == color)
]
if not product_details.empty:
product_value = product_details['Net Sales (FA)'].values[0]
total_value += product_value
description = (
f"Model: {model}, Fabric: {fabric}, Colour: {color} \tSales Value: {product_value} USD"
)
descriptions.append(description)
else:
descriptions.append(f"{model}-{fabric}-{color}: Not Found")
data = {
"models": models,
"fabrics": fabrics,
"colours": colours,
"total_customer_purchases": total_customer_purchases,
"total_customer_returns": total_customer_returns
}
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."
cart_output = "\n".join(descriptions) + f"\n\nTotal Cart Value: {round(total_value, 2)} USD"
formatted_result = "\n".join([f"Product: {pred['product']} \t Prediction: {pred['prediction']} \t Confidence: {pred['confidence']}" for pred in predictions])
return cart_output, formatted_result
except requests.exceptions.RequestException as e:
return f"Error: {str(e)}"
checkbox_choices = [
f"{row['Item Brand Model']}-{row['Item Brand Fabric']}-{row['Item Brand Colour']}"
f" \t\tType: {row['Product Type']} \t\tMaterial: {row['Main Material']} \t\tSales: {round(row['Net Sales (FA)'], 2)} USD"
for _, row in inventory.iterrows()
]
interface = gr.Interface(
fn=predict_return,
inputs=[
gr.CheckboxGroup(
choices=checkbox_choices,
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=[
gr.Textbox(label="Cart Details"),
gr.Textbox(label="Prediction Results")
],
live=False
)
interface.launch() |