Spaces:
Sleeping
Sleeping
judebebo32
commited on
Commit
•
476804e
1
Parent(s):
92359c5
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,76 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
input_encoded =
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|