File size: 2,248 Bytes
c9dff70
 
 
 
8618cef
c9dff70
 
 
 
 
 
 
 
 
8618cef
 
 
 
 
 
 
 
 
 
 
 
 
 
c9dff70
8618cef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9dff70
8618cef
 
 
c9dff70
8618cef
c9dff70
 
 
 
 
8618cef
 
 
c9dff70
 
8618cef
 
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
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)

def create_input_components():
    # Create a more organized input interface
    numeric_inputs = []
    for i, name in enumerate(feature_names):
        # Create a number input with a more descriptive label
        input_component = gr.Number(
            label=f"{name}",
            value=0.0,  # default value
            minimum=-1000,  # adjust these limits as needed
            maximum=1000
        )
        numeric_inputs.append(input_component)
    return numeric_inputs

def predict(*features):
    try:
        # Convert inputs to float and create DataFrame
        features = [float(f) if f is not None else 0.0 for f in features]
        df = pd.DataFrame([features], columns=feature_names)
        
        # Ensure all data types are float
        df = df.astype(float)
        
        # Make prediction using booster directly
        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 organized inputs
inputs = create_input_components()
output = gr.Textbox(label="Prediction Result")

# Create the interface
interface = gr.Interface(
    fn=predict,
    inputs=inputs,
    outputs=output,
    title="Mercedes-Benz Manufacturing Time Prediction",
    description="Enter feature values to predict the manufacturing time. All features should be numerical values.",
    examples=[[0.0] * len(feature_names)],  # Add an example with all zeros
    cache_examples=True
)

# Launch with debugging enabled
interface.launch(debug=True)