judebebo32 commited on
Commit
175bdbc
1 Parent(s): ce4c0b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -61
app.py CHANGED
@@ -1,61 +1,80 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import pickle
4
-
5
- # Load the pre-trained model
6
- with open('best_model.pkl', 'rb') as model_file:
7
- model = pickle.load(model_file)
8
-
9
- # Load the label encoder
10
- with open('label_encoder.pkl', 'rb') as label_encoder_file:
11
- label_encoder = pickle.load(label_encoder_file)
12
-
13
- # Title of the app
14
- st.title("Coffee Type Prediction")
15
-
16
- # Sidebar inputs for user preferences
17
- st.sidebar.header("User Preferences")
18
-
19
- time_of_day = st.sidebar.selectbox("Time of Day", ['morning', 'afternoon', 'evening'])
20
- coffee_strength = st.sidebar.selectbox("Coffee Strength", ['mild', 'regular', 'strong'])
21
- sweetness_level = st.sidebar.selectbox("Sweetness Level", ['unsweetened', 'lightly sweetened', 'sweet'])
22
- milk_type = st.sidebar.selectbox("Milk Type", ['none', 'regular', 'skim', 'almond'])
23
- coffee_temperature = st.sidebar.selectbox("Coffee Temperature", ['hot', 'iced', 'cold brew'])
24
- flavored_coffee = st.sidebar.selectbox("Flavored Coffee", ['yes', 'no'])
25
- caffeine_tolerance = st.sidebar.selectbox("Caffeine Tolerance", ['low', 'medium', 'high'])
26
- coffee_bean = st.sidebar.selectbox("Coffee Bean", ['Arabica', 'Robusta', 'blend'])
27
- coffee_size = st.sidebar.selectbox("Coffee Size", ['small', 'medium', 'large'])
28
- dietary_preferences = st.sidebar.selectbox("Dietary Preferences", ['none', 'vegan', 'lactose-intolerant'])
29
-
30
- # Encoding the inputs manually (same encoding as in your training data)
31
- input_data = pd.DataFrame({
32
- 'Token_0': [time_of_day],
33
- 'Token_1': [coffee_strength],
34
- 'Token_2': [sweetness_level],
35
- 'Token_3': [milk_type],
36
- 'Token_4': [coffee_temperature],
37
- 'Token_5': [flavored_coffee],
38
- 'Token_6': [caffeine_tolerance],
39
- 'Token_7': [coffee_bean],
40
- 'Token_8': [coffee_size],
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
-
47
- # Align columns with the training data (required columns)
48
- required_columns = [...] # Include all columns from the original model training data
49
- for col in required_columns:
50
- if col not in input_encoded.columns:
51
- input_encoded[col] = 0
52
- input_encoded = input_encoded[required_columns]
53
-
54
- # Make the prediction
55
- prediction = model.predict(input_encoded)[0]
56
-
57
- # Reverse the label encoding (map the prediction back to the coffee type)
58
- coffee_type = label_encoder.inverse_transform([prediction])[0]
59
-
60
- # Display the prediction
61
- st.subheader(f"Recommended Coffee: {coffee_type}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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:
10
+ if os.path.exists(file_path):
11
+ with open(file_path, 'rb') as file:
12
+ return pickle.load(file)
13
+ else:
14
+ return f"File {file_name} not found."
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
+ # Ensure model and label encoder are loaded correctly
23
+ if isinstance(model, str) or isinstance(label_encoder, str):
24
+ raise Exception(f"Error loading model or label encoder: {model} | {label_encoder}")
25
+
26
+ # Define the prediction function
27
+ 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):
28
+ # Input Data
29
+ input_data = pd.DataFrame({
30
+ 'Token_0': [time_of_day],
31
+ 'Token_1': [coffee_strength],
32
+ 'Token_2': [sweetness_level],
33
+ 'Token_3': [milk_type],
34
+ 'Token_4': [coffee_temperature],
35
+ 'Token_5': [flavored_coffee],
36
+ 'Token_6': [caffeine_tolerance],
37
+ 'Token_7': [coffee_bean],
38
+ 'Token_8': [coffee_size],
39
+ 'Token_9': [dietary_preferences]
40
+ })
41
+
42
+ # One-hot encode the input data (ensure it matches the training data)
43
+ input_encoded = pd.get_dummies(input_data)
44
+ required_columns = model.feature_names_in_ # Ensure that the input has the correct columns
45
+ for col in required_columns:
46
+ if col not in input_encoded.columns:
47
+ input_encoded[col] = 0 # Add missing columns as 0
48
+
49
+ input_encoded = input_encoded[required_columns] # Ensure the order of columns matches the training data
50
+
51
+ # Make prediction
52
+ prediction = model.predict(input_encoded)[0]
53
+
54
+ # Decode the label
55
+ coffee_type = label_encoder.inverse_transform([prediction])[0]
56
+
57
+ return f"Recommended Coffee: {coffee_type}"
58
+
59
+ # Set up Gradio interface
60
+ interface = gr.Interface(
61
+ fn=predict_coffee_type,
62
+ inputs=[
63
+ gr.inputs.Dropdown(choices=['morning', 'afternoon', 'evening'], label="Time of Day"),
64
+ gr.inputs.Dropdown(choices=['mild', 'regular', 'strong'], label="Coffee Strength"),
65
+ gr.inputs.Dropdown(choices=['unsweetened', 'lightly sweetened', 'sweet'], label="Sweetness Level"),
66
+ gr.inputs.Dropdown(choices=['none', 'regular', 'skim', 'almond'], label="Milk Type"),
67
+ gr.inputs.Dropdown(choices=['hot', 'iced', 'cold brew'], label="Coffee Temperature"),
68
+ gr.inputs.Dropdown(choices=['yes', 'no'], label="Flavored Coffee"),
69
+ gr.inputs.Dropdown(choices=['low', 'medium', 'high'], label="Caffeine Tolerance"),
70
+ gr.inputs.Dropdown(choices=['Arabica', 'Robusta', 'blend'], label="Coffee Bean"),
71
+ gr.inputs.Dropdown(choices=['small', 'medium', 'large'], label="Coffee Size"),
72
+ gr.inputs.Dropdown(choices=['none', 'vegan', 'lactose-intolerant'], label="Dietary Preferences")
73
+ ],
74
+ outputs="text",
75
+ title="Coffee Type Prediction"
76
+ )
77
+
78
+ # Launch the Gradio app
79
+ if __name__ == "__main__":
80
+ interface.launch()