Spaces:
Sleeping
Sleeping
Hemanth-Thaluru
commited on
Commit
•
d2e44a9
1
Parent(s):
e0c4a6d
added files
Browse files- file.py +116 -0
- main.py +59 -5
- requirements.txt +3 -1
file.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("heelo world!!")
|
2 |
+
|
3 |
+
|
4 |
+
#---------------My creation of starbucks drink recommendation system----------------------#
|
5 |
+
|
6 |
+
|
7 |
+
#---------------Importing all necessary libraries ----------------------#
|
8 |
+
|
9 |
+
|
10 |
+
from typing import List
|
11 |
+
from scipy.spatial import distance
|
12 |
+
import pandas as pd
|
13 |
+
#from pyscript import Element
|
14 |
+
from scipy.spatial.distance import euclidean
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
#---------------Initial setup and loading of data ----------------------#
|
19 |
+
|
20 |
+
file_path = 'starbucks_drinks.csv'
|
21 |
+
sbdrinks_data = pd.read_csv(file_path)
|
22 |
+
|
23 |
+
## i renamed the column coz its doesnt name sense for a cloumn name to be "490"
|
24 |
+
|
25 |
+
sbdrinks_data.rename(columns={'490': 'Drink_name'}, inplace=True)
|
26 |
+
|
27 |
+
## creating a new column Drink_id and adding it as first column
|
28 |
+
|
29 |
+
sbdrinks_data['Drink_id'] = range(1, len(sbdrinks_data) + 1)
|
30 |
+
columns = ['Drink_id'] + sbdrinks_data.columns[:-1].tolist()
|
31 |
+
sbdrinks_data = sbdrinks_data[columns]
|
32 |
+
print(sbdrinks_data.dtypes)
|
33 |
+
|
34 |
+
|
35 |
+
#---------------Turning data into float data types ----------------------#
|
36 |
+
|
37 |
+
columns_to_convert = sbdrinks_data.columns.difference(['Drink_id', 'Drink_name'])
|
38 |
+
sbdrinks_data[columns_to_convert] = sbdrinks_data[columns_to_convert].astype(float)
|
39 |
+
|
40 |
+
# Print the data types after conversion
|
41 |
+
print("\nData types after conversion:")
|
42 |
+
print(sbdrinks_data.dtypes)
|
43 |
+
|
44 |
+
# Verify the DataFrame structure
|
45 |
+
# print("\nDataFrame:")
|
46 |
+
# print(sbdrinks_data.head())
|
47 |
+
drinks_aslist = sbdrinks_data['Drink_name']
|
48 |
+
|
49 |
+
|
50 |
+
#---------------Main Logic ----------------------#
|
51 |
+
|
52 |
+
|
53 |
+
def mean_selected_drink(s_drinks):
|
54 |
+
mean_vector = s_drinks.drop(columns=['Drink_id', 'Drink_name']).mean().values
|
55 |
+
return mean_vector
|
56 |
+
|
57 |
+
# Function to find closest drinks based on mean vector
|
58 |
+
def find_closest_drinks(mean_vector, all_drinks_data, n_recommendations=5):
|
59 |
+
# Calculate Euclidean distances
|
60 |
+
all_drinks_data['distance'] = all_drinks_data.apply(lambda row: euclidean(mean_vector, row.drop(['Drink_id', 'Drink_name'])), axis=1)
|
61 |
+
# Sort drinks by distance and get top recommendations
|
62 |
+
closest_drinks = all_drinks_data.sort_values(by='distance').head(n_recommendations)
|
63 |
+
return closest_drinks[['Drink_id', 'Drink_name', 'distance']]
|
64 |
+
|
65 |
+
# Function to get recommendations based on selected drink names
|
66 |
+
def get_drink_recommendations(selected_drinks_names, all_drinks_data, include_all=True, include_coffee=True, include_tea=True, include_neither=False, include_hot=True, include_cold=True, include_frozen=True, n_recommendations=5):
|
67 |
+
selected_drinks = all_drinks_data[all_drinks_data['Drink_name'].isin(selected_drinks_names)]
|
68 |
+
|
69 |
+
if selected_drinks.empty:
|
70 |
+
print("No drinks selected")
|
71 |
+
return pd.DataFrame(columns=['Drink_id', 'Drink_name', 'distance'])
|
72 |
+
|
73 |
+
# Calculating mean vector of selected drinks
|
74 |
+
mean_vector = mean_selected_drink(selected_drinks)
|
75 |
+
|
76 |
+
|
77 |
+
#applying Filters on all_drinks_data based on user's preference
|
78 |
+
filtered_drinks = pd.DataFrame(columns=all_drinks_data.columns)
|
79 |
+
|
80 |
+
for index, row in all_drinks_data.iterrows():
|
81 |
+
if include_all:
|
82 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
83 |
+
else:
|
84 |
+
if include_coffee and row['is_coffee'] == 1:
|
85 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
86 |
+
if include_tea and row['is_tea'] == 1:
|
87 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
88 |
+
if include_neither and row['is_not_coffee_or_tea'] == 1:
|
89 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
90 |
+
if include_hot and row['is_hot'] == 1:
|
91 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
92 |
+
if include_cold and row['is_cold'] == 1:
|
93 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
94 |
+
if include_frozen and row['is_frozen'] == 1:
|
95 |
+
filtered_drinks = pd.concat([filtered_drinks, pd.DataFrame([row])], ignore_index=True, sort=False)
|
96 |
+
|
97 |
+
if filtered_drinks.empty:
|
98 |
+
print("No drinks matched the filters.")
|
99 |
+
return pd.DataFrame(columns=['Drink_id', 'Drink_name', 'distance'])
|
100 |
+
|
101 |
+
recommendations = find_closest_drinks(mean_vector,filtered_drinks, n_recommendations)
|
102 |
+
print(recommendations)
|
103 |
+
print(type(recommendations))
|
104 |
+
return recommendations
|
105 |
+
|
106 |
+
# Example usage
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
#--------------- Functions for clearing active_drinks and toggling drinks ----------------------#
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
main.py
CHANGED
@@ -1,9 +1,63 @@
|
|
1 |
-
from flask import Flask
|
2 |
-
|
|
|
|
|
3 |
app = Flask(__name__)
|
4 |
|
|
|
|
|
5 |
|
6 |
@app.route('/')
|
7 |
-
|
8 |
-
|
9 |
-
return render_template('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request,jsonify
|
2 |
+
import pandas as pd
|
3 |
+
from file import get_drink_recommendations, sbdrinks_data
|
4 |
+
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Assuming sbdrinks_data is loaded or imported correctly
|
8 |
+
drinks_aslist = sbdrinks_data['Drink_name'].tolist()
|
9 |
|
10 |
@app.route('/')
|
11 |
+
def index():
|
12 |
+
drinks_aslist = sbdrinks_data['Drink_name'].tolist()
|
13 |
+
return render_template('index.html', drinks=drinks_aslist)
|
14 |
+
|
15 |
+
@app.route('/recommend', methods=['POST'])
|
16 |
+
def recommend():
|
17 |
+
try:
|
18 |
+
selected_drinks_names = request.form.getlist('selected_drinks')
|
19 |
+
|
20 |
+
include_all = request.form.get('include_all') == '1'
|
21 |
+
include_coffee = request.form.get('include_coffee') == '1'
|
22 |
+
include_tea = request.form.get('include_tea') == '1'
|
23 |
+
include_neither = request.form.get('include_neither') == '1'
|
24 |
+
include_hot = request.form.get('include_hot') == '1'
|
25 |
+
include_cold = request.form.get('include_cold') == '1'
|
26 |
+
include_frozen = request.form.get('include_frozen') == '1'
|
27 |
+
n_recommendations = int(request.form.get('n_recommendations', 5))
|
28 |
+
|
29 |
+
print("Include All:", include_all)
|
30 |
+
print("Include Coffee:", include_coffee)
|
31 |
+
print("Include Tea:", include_tea)
|
32 |
+
print("Include Neither:", include_neither)
|
33 |
+
print("Include Hot:", include_hot)
|
34 |
+
print("Include Cold:", include_cold)
|
35 |
+
print("Include Frozen:", include_frozen)
|
36 |
+
print("Number of Recommendations:", n_recommendations)
|
37 |
+
|
38 |
+
# Call your recommendation function
|
39 |
+
recommendations_df = get_drink_recommendations(
|
40 |
+
selected_drinks_names,
|
41 |
+
sbdrinks_data,
|
42 |
+
include_all=include_all,
|
43 |
+
include_coffee=include_coffee,
|
44 |
+
include_tea=include_tea,
|
45 |
+
include_neither=include_neither,
|
46 |
+
include_hot=include_hot,
|
47 |
+
include_cold=include_cold,
|
48 |
+
include_frozen=include_frozen,
|
49 |
+
n_recommendations=n_recommendations
|
50 |
+
)
|
51 |
+
|
52 |
+
print(recommendations_df)
|
53 |
+
|
54 |
+
# Convert to list of dictionaries for JSON response
|
55 |
+
recommendations_list = recommendations_df.to_dict(orient='records')
|
56 |
+
|
57 |
+
return jsonify(recommendations=recommendations_list)
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error: {e}")
|
60 |
+
return jsonify({"error": str(e)}), 500
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
app.run(debug=True)
|
requirements.txt
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
flask
|
2 |
-
gunicorn
|
|
|
|
|
|
1 |
flask
|
2 |
+
gunicorn
|
3 |
+
scipy
|
4 |
+
pandas
|