Spaces:
Runtime error
Runtime error
judebebo32
commited on
Commit
•
175bdbc
1
Parent(s):
ce4c0b8
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,80 @@
|
|
1 |
-
import
|
2 |
-
import pandas as pd
|
3 |
-
import pickle
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|