judebebo32 commited on
Commit
c5c0350
1 Parent(s): 7b10259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -3,7 +3,7 @@ import pandas as pd
3
  import pickle
4
  import os
5
 
6
- # Helper function to load pickle files
7
  def load_pickle_file(file_name):
8
  file_path = os.path.join(os.path.dirname(__file__), file_name)
9
  try:
@@ -15,19 +15,16 @@ def load_pickle_file(file_name):
15
  except Exception as e:
16
  return f"An error occurred while loading {file_name}: {e}"
17
 
18
- # Load the pre-trained model and label encoder
19
  model = load_pickle_file('best_model.pkl')
20
  label_encoder = load_pickle_file('label_encoder.pkl')
21
 
22
- # Check if the model and label encoder were loaded successfully
23
  if isinstance(model, str) or isinstance(label_encoder, str):
24
- # Print the error message for debugging
25
- print(f"Error loading model or label encoder: {model} | {label_encoder}")
26
  raise Exception(f"Error loading model or label encoder: {model} | {label_encoder}")
27
 
28
- # Define the prediction function
29
  def predict_coffee_type(time_of_day, coffee_strength, sweetness_level, milk_type, coffee_temperature, flavored_coffee, caffeine_tolerance, coffee_bean, coffee_size, dietary_preferences):
30
- # Input Data
31
  input_data = pd.DataFrame({
32
  'Token_0': [time_of_day],
33
  'Token_1': [coffee_strength],
@@ -41,21 +38,18 @@ def predict_coffee_type(time_of_day, coffee_strength, sweetness_level, milk_type
41
  'Token_9': [dietary_preferences]
42
  })
43
 
44
- # One-hot encode the input data (ensure it matches the training data)
45
  input_encoded = pd.get_dummies(input_data)
46
- required_columns = model.feature_names_in_ # Ensure that the input has the correct columns
47
  for col in required_columns:
48
  if col not in input_encoded.columns:
49
- input_encoded[col] = 0 # Add missing columns as 0
50
-
51
- input_encoded = input_encoded[required_columns] # Ensure the order of columns matches the training data
52
-
53
- # Make prediction
54
  prediction = model.predict(input_encoded)[0]
55
-
56
- # Decode the label
57
  coffee_type = label_encoder.inverse_transform([prediction])[0]
58
-
59
  return f"Recommended Coffee: {coffee_type}"
60
 
61
  # Set up Gradio interface
 
3
  import pickle
4
  import os
5
 
6
+ # Load pickle files
7
  def load_pickle_file(file_name):
8
  file_path = os.path.join(os.path.dirname(__file__), file_name)
9
  try:
 
15
  except Exception as e:
16
  return f"An error occurred while loading {file_name}: {e}"
17
 
18
+ # Load model and label encoder
19
  model = load_pickle_file('best_model.pkl')
20
  label_encoder = load_pickle_file('label_encoder.pkl')
21
 
 
22
  if isinstance(model, str) or isinstance(label_encoder, str):
 
 
23
  raise Exception(f"Error loading model or label encoder: {model} | {label_encoder}")
24
 
25
+ # Prediction function
26
  def predict_coffee_type(time_of_day, coffee_strength, sweetness_level, milk_type, coffee_temperature, flavored_coffee, caffeine_tolerance, coffee_bean, coffee_size, dietary_preferences):
27
+ # Prepare input data
28
  input_data = pd.DataFrame({
29
  'Token_0': [time_of_day],
30
  'Token_1': [coffee_strength],
 
38
  'Token_9': [dietary_preferences]
39
  })
40
 
41
+ # One-hot encode the input data
42
  input_encoded = pd.get_dummies(input_data)
43
+ required_columns = model.feature_names_in_ # Ensure columns match training data
44
  for col in required_columns:
45
  if col not in input_encoded.columns:
46
+ input_encoded[col] = 0 # Add missing columns
47
+
48
+ input_encoded = input_encoded[required_columns] # Reorder columns to match model training
49
+
50
+ # Predict the coffee type
51
  prediction = model.predict(input_encoded)[0]
 
 
52
  coffee_type = label_encoder.inverse_transform([prediction])[0]
 
53
  return f"Recommended Coffee: {coffee_type}"
54
 
55
  # Set up Gradio interface