time-prediction / app.py
alperugurcan's picture
Update app.py
8618cef verified
raw
history blame
2.25 kB
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)