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()