judebebo32 commited on
Commit
3d4a909
1 Parent(s): ab8c0ac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
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
+ 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):
14
+ # Creating input DataFrame for the model
15
+ input_data = pd.DataFrame({
16
+ 'Token_0': [time_of_day],
17
+ 'Token_1': [coffee_strength],
18
+ 'Token_2': [sweetness_level],
19
+ 'Token_3': [milk_type],
20
+ 'Token_4': [coffee_temperature],
21
+ 'Token_5': [flavored_coffee],
22
+ 'Token_6': [caffeine_tolerance],
23
+ 'Token_7': [coffee_bean],
24
+ 'Token_8': [coffee_size],
25
+ 'Token_9': [dietary_preferences]
26
+ })
27
+
28
+ # One-hot encode the input data (ensure it matches the training data)
29
+ input_encoded = pd.get_dummies(input_data)
30
+
31
+ # Align columns with the training data (required columns)
32
+ required_columns = model.feature_names_in_ # Get the feature columns from the model
33
+ for col in required_columns:
34
+ if col not in input_encoded.columns:
35
+ input_encoded[col] = 0
36
+ input_encoded = input_encoded[required_columns]
37
+
38
+ # Make the prediction
39
+ prediction = model.predict(input_encoded)[0]
40
+
41
+ # Reverse the label encoding (map the prediction back to the coffee type)
42
+ coffee_type = label_encoder.inverse_transform([prediction])[0]
43
+
44
+ return coffee_type
45
+
46
+ # Gradio Interface
47
+ interface = gr.Interface(
48
+ fn=predict_coffee_type,
49
+ inputs=[
50
+ gr.inputs.Dropdown(['morning', 'afternoon', 'evening'], label="Time of Day"),
51
+ gr.inputs.Dropdown(['mild', 'regular', 'strong'], label="Coffee Strength"),
52
+ gr.inputs.Dropdown(['unsweetened', 'lightly sweetened', 'sweet'], label="Sweetness Level"),
53
+ gr.inputs.Dropdown(['none', 'regular', 'skim', 'almond'], label="Milk Type"),
54
+ gr.inputs.Dropdown(['hot', 'iced', 'cold brew'], label="Coffee Temperature"),
55
+ gr.inputs.Dropdown(['yes', 'no'], label="Flavored Coffee"),
56
+ gr.inputs.Dropdown(['low', 'medium', 'high'], label="Caffeine Tolerance"),
57
+ gr.inputs.Dropdown(['Arabica', 'Robusta', 'blend'], label="Coffee Bean"),
58
+ gr.inputs.Dropdown(['small', 'medium', 'large'], label="Coffee Size"),
59
+ gr.inputs.Dropdown(['none', 'vegan', 'lactose-intolerant'], label="Dietary Preferences")
60
+ ],
61
+ outputs=gr.outputs.Textbox(label="Recommended Coffee Type"),
62
+ title="Coffee Type Recommendation"
63
+ )
64
+
65
+ if __name__ == "__main__":
66
+ interface.launch()