import gradio as gr import pandas as pd import joblib from huggingface_hub import hf_hub_download import numpy as np # Download model and feature names from Hugging Face model_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="mercedes_model.joblib") feature_names_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="feature_names.joblib") # Load the saved model and feature names model = joblib.load(model_path) feature_names = joblib.load(feature_names_path) # Most common X0 values with their frequencies FEATURE_OPTIONS = { "z (Most Common - 360 cases)": "z", "ak (349 cases)": "ak", "y (324 cases)": "y", "ay (313 cases)": "ay", "t (306 cases)": "t", "x (300 cases)": "x", "o (269 cases)": "o", "f (227 cases)": "f", "n (195 cases)": "n", "w (182 cases)": "w" } # Default values for other features DEFAULT_VALUES = {name: 0.0 for name in feature_names} def predict(selected_option): try: # Create a dictionary with all features set to default values input_dict = DEFAULT_VALUES.copy() # Get the actual value from the selected option selected_value = FEATURE_OPTIONS[selected_option] # Create dummy variable columns for X0 for val in set(FEATURE_OPTIONS.values()): col_name = f'X0_{val}' input_dict[col_name] = 1 if val == selected_value else 0 # Create DataFrame with all features df = pd.DataFrame([input_dict]) # Make prediction if hasattr(model, '_Booster'): booster = model._Booster prediction = booster.predict(df)[0] else: prediction = model.predict(df)[0] return f"Predicted manufacturing time: {prediction:.2f} seconds" except Exception as e: return f"Error in prediction: {str(e)}" # Create interface with dropdown interface = gr.Interface( fn=predict, inputs=gr.Dropdown( choices=list(FEATURE_OPTIONS.keys()), label="Select Manufacturing Configuration (X0)", value=list(FEATURE_OPTIONS.keys())[0] ), outputs=gr.Textbox(label="Prediction Result"), title="Mercedes-Benz Manufacturing Time Predictor", description="Select one of the most common manufacturing configurations to predict the production time. The options are sorted by frequency of occurrence in the training data.", examples=[[list(FEATURE_OPTIONS.keys())[0]]], cache_examples=True, theme=gr.themes.Soft() ) interface.launch(debug=True)