alperugurcan commited on
Commit
a373624
·
verified ·
1 Parent(s): 8618cef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -32
app.py CHANGED
@@ -12,30 +12,40 @@ feature_names_path = hf_hub_download(repo_id="alperugurcan/mercedes", filename="
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]
@@ -46,20 +56,20 @@ def predict(*features):
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)
 
12
  model = joblib.load(model_path)
13
  feature_names = joblib.load(feature_names_path)
14
 
15
+ # Most common X0 values with their frequencies
16
+ FEATURE_OPTIONS = {
17
+ "z (Most Common - 360 cases)": "z",
18
+ "ak (349 cases)": "ak",
19
+ "y (324 cases)": "y",
20
+ "ay (313 cases)": "ay",
21
+ "t (306 cases)": "t",
22
+ "x (300 cases)": "x",
23
+ "o (269 cases)": "o",
24
+ "f (227 cases)": "f",
25
+ "n (195 cases)": "n",
26
+ "w (182 cases)": "w"
27
+ }
28
 
29
+ # Default values for other features
30
+ DEFAULT_VALUES = {name: 0.0 for name in feature_names}
31
+
32
+ def predict(selected_option):
33
  try:
34
+ # Create a dictionary with all features set to default values
35
+ input_dict = DEFAULT_VALUES.copy()
36
+
37
+ # Get the actual value from the selected option
38
+ selected_value = FEATURE_OPTIONS[selected_option]
39
 
40
+ # Create dummy variable columns for X0
41
+ for val in set(FEATURE_OPTIONS.values()):
42
+ col_name = f'X0_{val}'
43
+ input_dict[col_name] = 1 if val == selected_value else 0
44
 
45
+ # Create DataFrame with all features
46
+ df = pd.DataFrame([input_dict])
47
+
48
+ # Make prediction
49
  if hasattr(model, '_Booster'):
50
  booster = model._Booster
51
  prediction = booster.predict(df)[0]
 
56
  except Exception as e:
57
  return f"Error in prediction: {str(e)}"
58
 
59
+ # Create interface with dropdown
 
 
 
 
60
  interface = gr.Interface(
61
  fn=predict,
62
+ inputs=gr.Dropdown(
63
+ choices=list(FEATURE_OPTIONS.keys()),
64
+ label="Select Manufacturing Configuration (X0)",
65
+ value=list(FEATURE_OPTIONS.keys())[0]
66
+ ),
67
+ outputs=gr.Textbox(label="Prediction Result"),
68
+ title="Mercedes-Benz Manufacturing Time Predictor",
69
+ 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.",
70
+ examples=[[list(FEATURE_OPTIONS.keys())[0]]],
71
+ cache_examples=True,
72
+ theme=gr.themes.Soft()
73
  )
74
 
 
75
  interface.launch(debug=True)