Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
import joblib
|
4 |
from huggingface_hub import hf_hub_download
|
|
|
5 |
|
6 |
# Download model and feature names from Hugging Face
|
7 |
model_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="mercedes_model.joblib")
|
@@ -11,24 +12,54 @@ feature_names_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="
|
|
11 |
model = joblib.load(model_path)
|
12 |
feature_names = joblib.load(feature_names_path)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def predict(*features):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
# Create
|
23 |
-
inputs =
|
24 |
-
output = gr.Textbox(label="Prediction")
|
25 |
|
|
|
26 |
interface = gr.Interface(
|
27 |
fn=predict,
|
28 |
inputs=inputs,
|
29 |
outputs=output,
|
30 |
title="Mercedes-Benz Manufacturing Time Prediction",
|
31 |
-
description="Enter feature values to predict manufacturing time"
|
|
|
|
|
32 |
)
|
33 |
|
34 |
-
|
|
|
|
2 |
import pandas as pd
|
3 |
import joblib
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
+
import numpy as np
|
6 |
|
7 |
# Download model and feature names from Hugging Face
|
8 |
model_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="mercedes_model.joblib")
|
|
|
12 |
model = joblib.load(model_path)
|
13 |
feature_names = joblib.load(feature_names_path)
|
14 |
|
15 |
+
def create_input_components():
|
16 |
+
# Create a more organized input interface
|
17 |
+
numeric_inputs = []
|
18 |
+
for i, name in enumerate(feature_names):
|
19 |
+
# Create a number input with a more descriptive label
|
20 |
+
input_component = gr.Number(
|
21 |
+
label=f"{name}",
|
22 |
+
value=0.0, # default value
|
23 |
+
minimum=-1000, # adjust these limits as needed
|
24 |
+
maximum=1000
|
25 |
+
)
|
26 |
+
numeric_inputs.append(input_component)
|
27 |
+
return numeric_inputs
|
28 |
+
|
29 |
def predict(*features):
|
30 |
+
try:
|
31 |
+
# Convert inputs to float and create DataFrame
|
32 |
+
features = [float(f) if f is not None else 0.0 for f in features]
|
33 |
+
df = pd.DataFrame([features], columns=feature_names)
|
34 |
+
|
35 |
+
# Ensure all data types are float
|
36 |
+
df = df.astype(float)
|
37 |
+
|
38 |
+
# Make prediction using booster directly
|
39 |
+
if hasattr(model, '_Booster'):
|
40 |
+
booster = model._Booster
|
41 |
+
prediction = booster.predict(df)[0]
|
42 |
+
else:
|
43 |
+
prediction = model.predict(df)[0]
|
44 |
+
|
45 |
+
return f"Predicted manufacturing time: {prediction:.2f} seconds"
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error in prediction: {str(e)}"
|
48 |
|
49 |
+
# Create interface with organized inputs
|
50 |
+
inputs = create_input_components()
|
51 |
+
output = gr.Textbox(label="Prediction Result")
|
52 |
|
53 |
+
# Create the interface
|
54 |
interface = gr.Interface(
|
55 |
fn=predict,
|
56 |
inputs=inputs,
|
57 |
outputs=output,
|
58 |
title="Mercedes-Benz Manufacturing Time Prediction",
|
59 |
+
description="Enter feature values to predict the manufacturing time. All features should be numerical values.",
|
60 |
+
examples=[[0.0] * len(feature_names)], # Add an example with all zeros
|
61 |
+
cache_examples=True
|
62 |
)
|
63 |
|
64 |
+
# Launch with debugging enabled
|
65 |
+
interface.launch(debug=True)
|