SanaAdeel commited on
Commit
44ff7e1
·
verified ·
1 Parent(s): fb85e16

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 2: Authenticate the user (this will open a prompt)
2
+ # from google.colab import auth
3
+ # auth.authenticate_user() # This will prompt you to log in and authenticate
4
+
5
+ # Step 3: Download the .xlsx file from Google Drive using its ID
6
+ import gdown
7
+
8
+ # Extracted file ID from your Google Drive URL
9
+ file_id = '1BJFao3C6p8k3_KjLfmDo5KbNMrTs7MRo' # Use your actual file ID from the URL
10
+ url = f'https://drive.google.com/uc?id={file_id}'
11
+
12
+ # Download the file
13
+ gdown.download(url, 'data.xlsx', quiet=False) # Save it as data.xlsx
14
+
15
+ # Step 4: Read the .xlsx file using pandas
16
+ import pandas as pd
17
+
18
+ # Read the Excel file
19
+ df = pd.read_excel('data.xlsx', engine='openpyxl') # Using openpyxl to read .xlsx files
20
+
21
+ # Display the first 5 rows of the data
22
+ print(df.head())
23
+ # Check the column names and basic structure
24
+ print(df.columns)
25
+
26
+ # Check for any missing data
27
+ print(df.isnull().sum())
28
+
29
+ # Display summary statistics
30
+ print(df.describe())
31
+ import random
32
+ import pandas as pd
33
+
34
+ # Step 5: Filter dishes by season
35
+ def filter_dishes_by_season(df, season='All Seasons'):
36
+ """Filters the dataset based on the season."""
37
+ # Filter by the specified season (Summer, Winter, All Seasons)
38
+ filtered_df = df[df['Season'].isin([season, 'All Seasons'])]
39
+ return filtered_df
40
+
41
+ # Step 6: Generate weekly menu based on constraints
42
+ def generate_weekly_menu(df, season='All Seasons', budget=6000):
43
+ """Generates a weekly menu based on user preferences."""
44
+
45
+ # Filter dishes by the selected season
46
+ available_dishes = filter_dishes_by_season(df, season)
47
+
48
+ # Constraints for the weekly menu
49
+ required_categories = {
50
+ 'meat': 1,
51
+ 'chicken': 2,
52
+ 'fish': 1,
53
+ 'daal': 4,
54
+ 'sabzi': 4,
55
+ 'outing': 1,
56
+ 'dessert': 2,
57
+ 'snack': 2
58
+ }
59
+
60
+ weekly_menu = []
61
+ total_cost = 0
62
+
63
+ # Generate the menu by selecting dishes that meet the constraints
64
+ for category, count in required_categories.items():
65
+ # Special handling for 'outing' category
66
+ if category == 'outing':
67
+ category_dishes = available_dishes[available_dishes['Home Made / Outing'].str.contains('Outing', case=False)]
68
+ else:
69
+ # Filter dishes based on the sub-category (for all other categories)
70
+ category_dishes = available_dishes[available_dishes['Sub-Category'].str.contains(category, case=False)]
71
+
72
+ if len(category_dishes) > 0:
73
+ # If there are dishes in the category, select as many as possible
74
+ selected_dishes = random.sample(list(category_dishes.iterrows()), min(count, len(category_dishes)))
75
+ for dish in selected_dishes:
76
+ dish_info = dish[1]
77
+ weekly_menu.append(dish_info['Name'])
78
+ total_cost += dish_info['Cost per 4 persons']
79
+ else:
80
+ print(f"Not enough dishes found in category '{category}'. Adding none.")
81
+
82
+ # If the total cost exceeds the budget, we can suggest reducing high-cost items or adjust constraints
83
+ if total_cost > budget:
84
+ print(f"Total cost ({total_cost}) exceeds budget. Try adjusting your criteria.")
85
+ # Example of dynamic adjustment: remove expensive dishes or reduce number of items
86
+ # Try reducing the cost by taking fewer dishes (example: reduce number of chicken or sabzi)
87
+ weekly_menu = [] # Clear the current menu to attempt adjustment
88
+ total_cost = 0
89
+ for category, count in required_categories.items():
90
+ category_dishes = available_dishes[available_dishes['Sub-Category'].str.contains(category, case=False)]
91
+
92
+ # Sort dishes by cost (lowest first) for budget-friendly selection
93
+ category_dishes_sorted = category_dishes.sort_values(by='Cost per 4 persons')
94
+
95
+ selected_dishes = random.sample(list(category_dishes_sorted.iterrows()), min(count, len(category_dishes_sorted)))
96
+ for dish in selected_dishes:
97
+ dish_info = dish[1]
98
+ weekly_menu.append(dish_info['Name'])
99
+ total_cost += dish_info['Cost per 4 persons']
100
+
101
+ if total_cost > budget: # If the total cost still exceeds, stop and return the result
102
+ break
103
+
104
+ return weekly_menu, total_cost
105
+
106
+
107
+ # Example Usage
108
+ season = 'Summer' # Can be 'Summer', 'Winter', or 'All Seasons'
109
+ budget = 6000 # Monthly budget in PKR
110
+
111
+ # Generate weekly menu
112
+ menu, total_cost = generate_weekly_menu(df, season, budget)
113
+
114
+ # Print the weekly menu and total cost
115
+ print("Weekly Menu:")
116
+ if isinstance(menu, list):
117
+ for dish in menu:
118
+ print(f"- {dish}")
119
+ else:
120
+ print(menu)
121
+ print(f"Total Cost: {total_cost} PKR")