judebebo32 commited on
Commit
476804e
1 Parent(s): 92359c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -61
app.py CHANGED
@@ -1,61 +1,76 @@
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 subprocess
2
+ import sys
3
+
4
+ # Force install scikit-learn if not found
5
+ try:
6
+ import sklearn
7
+ except ModuleNotFoundError:
8
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
9
+ import sklearn # Import again after installation
10
+
11
+ import gradio as gr
12
+ import pandas as pd
13
+ import pickle
14
+
15
+ # Load the pre-trained model
16
+ with open('best_model.pkl', 'rb') as model_file:
17
+ model = pickle.load(model_file)
18
+
19
+ # Load the label encoder
20
+ with open('label_encoder.pkl', 'rb') as label_encoder_file:
21
+ label_encoder = pickle.load(label_encoder_file)
22
+
23
+ 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):
24
+ # Creating input DataFrame for the model
25
+ input_data = pd.DataFrame({
26
+ 'Token_0': [time_of_day],
27
+ 'Token_1': [coffee_strength],
28
+ 'Token_2': [sweetness_level],
29
+ 'Token_3': [milk_type],
30
+ 'Token_4': [coffee_temperature],
31
+ 'Token_5': [flavored_coffee],
32
+ 'Token_6': [caffeine_tolerance],
33
+ 'Token_7': [coffee_bean],
34
+ 'Token_8': [coffee_size],
35
+ 'Token_9': [dietary_preferences]
36
+ })
37
+
38
+ # One-hot encode the input data (ensure it matches the training data)
39
+ input_encoded = pd.get_dummies(input_data)
40
+
41
+ # Align columns with the training data (required columns)
42
+ required_columns = model.feature_names_in_ # Get the feature columns from the model
43
+ for col in required_columns:
44
+ if col not in input_encoded.columns:
45
+ input_encoded[col] = 0
46
+ input_encoded = input_encoded[required_columns]
47
+
48
+ # Make the prediction
49
+ prediction = model.predict(input_encoded)[0]
50
+
51
+ # Reverse the label encoding (map the prediction back to the coffee type)
52
+ coffee_type = label_encoder.inverse_transform([prediction])[0]
53
+
54
+ return coffee_type
55
+
56
+ # Gradio Interface using components
57
+ interface = gr.Interface(
58
+ fn=predict_coffee_type,
59
+ inputs=[
60
+ gr.Dropdown(['morning', 'afternoon', 'evening'], label="Time of Day"),
61
+ gr.Dropdown(['mild', 'regular', 'strong'], label="Coffee Strength"),
62
+ gr.Dropdown(['unsweetened', 'lightly sweetened', 'sweet'], label="Sweetness Level"),
63
+ gr.Dropdown(['none', 'regular', 'skim', 'almond'], label="Milk Type"),
64
+ gr.Dropdown(['hot', 'iced', 'cold brew'], label="Coffee Temperature"),
65
+ gr.Dropdown(['yes', 'no'], label="Flavored Coffee"),
66
+ gr.Dropdown(['low', 'medium', 'high'], label="Caffeine Tolerance"),
67
+ gr.Dropdown(['Arabica', 'Robusta', 'blend'], label="Coffee Bean"),
68
+ gr.Dropdown(['small', 'medium', 'large'], label="Coffee Size"),
69
+ gr.Dropdown(['none', 'vegan', 'lactose-intolerant'], label="Dietary Preferences")
70
+ ],
71
+ outputs=gr.Textbox(label="Recommended Coffee Type"),
72
+ title="Coffee Type Recommendation"
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ interface.launch()