SanaAdeel commited on
Commit
928a5ac
·
verified ·
1 Parent(s): 143d0c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -49
app.py CHANGED
@@ -1,51 +1,37 @@
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,
@@ -60,17 +46,13 @@ def generate_weekly_menu(df, season='All Seasons', budget=6000):
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]
@@ -79,43 +61,33 @@ def generate_weekly_menu(df, season='All Seasons', budget=6000):
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")
 
 
 
 
 
 
1
  import gdown
2
+ import pandas as pd
3
+ import random
4
 
5
+ # If you upload the file directly to Hugging Face, you can access it directly.
6
+ # For example, if the file is uploaded to the Hugging Face space, you can directly reference it like so:
7
+ # df = pd.read_excel('./data.xlsx', engine='openpyxl')
8
 
9
+ # If you're using gdown to download the file from Google Drive, you can keep this:
10
+ # file_id = '1BJFao3C6p8k3_KjLfmDo5KbNMrTs7MRo'
11
+ # url = f'https://drive.google.com/uc?id={file_id}'
12
+ # gdown.download(url, 'data.xlsx', quiet=False)
13
 
14
+ # Step 1: Read the Excel file (adjust file path if necessary)
15
+ df = pd.read_excel('data.xlsx', engine='openpyxl') # Adjust file path if uploaded locally
16
 
17
+ # Step 2: Check the structure of the data
 
 
 
18
  print(df.head())
 
19
  print(df.columns)
20
 
21
+ # Convert 'Cost per 4 persons' to numeric (in case there are invalid values)
22
+ df['Cost per 4 persons'] = pd.to_numeric(df['Cost per 4 persons'], errors='coerce')
23
 
24
+ # Step 3: Filter dishes by season
 
 
 
 
 
25
  def filter_dishes_by_season(df, season='All Seasons'):
26
  """Filters the dataset based on the season."""
 
27
  filtered_df = df[df['Season'].isin([season, 'All Seasons'])]
28
  return filtered_df
29
 
30
+ # Step 4: Generate weekly menu based on constraints
31
  def generate_weekly_menu(df, season='All Seasons', budget=6000):
32
  """Generates a weekly menu based on user preferences."""
 
 
33
  available_dishes = filter_dishes_by_season(df, season)
34
 
 
35
  required_categories = {
36
  'meat': 1,
37
  'chicken': 2,
 
46
  weekly_menu = []
47
  total_cost = 0
48
 
 
49
  for category, count in required_categories.items():
 
50
  if category == 'outing':
51
  category_dishes = available_dishes[available_dishes['Home Made / Outing'].str.contains('Outing', case=False)]
52
  else:
 
53
  category_dishes = available_dishes[available_dishes['Sub-Category'].str.contains(category, case=False)]
54
 
55
  if len(category_dishes) > 0:
 
56
  selected_dishes = random.sample(list(category_dishes.iterrows()), min(count, len(category_dishes)))
57
  for dish in selected_dishes:
58
  dish_info = dish[1]
 
61
  else:
62
  print(f"Not enough dishes found in category '{category}'. Adding none.")
63
 
 
64
  if total_cost > budget:
65
  print(f"Total cost ({total_cost}) exceeds budget. Try adjusting your criteria.")
66
+ weekly_menu = [] # Clear menu and try reducing cost
 
 
67
  total_cost = 0
68
  for category, count in required_categories.items():
69
  category_dishes = available_dishes[available_dishes['Sub-Category'].str.contains(category, case=False)]
 
 
70
  category_dishes_sorted = category_dishes.sort_values(by='Cost per 4 persons')
 
71
  selected_dishes = random.sample(list(category_dishes_sorted.iterrows()), min(count, len(category_dishes_sorted)))
72
  for dish in selected_dishes:
73
  dish_info = dish[1]
74
  weekly_menu.append(dish_info['Name'])
75
  total_cost += dish_info['Cost per 4 persons']
76
+ if total_cost > budget:
 
77
  break
78
 
79
  return weekly_menu, total_cost
80
 
 
81
  # Example Usage
82
+ season = 'Summer'
83
+ budget = 6000
84
 
 
85
  menu, total_cost = generate_weekly_menu(df, season, budget)
86
 
 
87
  print("Weekly Menu:")
88
  if isinstance(menu, list):
89
  for dish in menu:
90
  print(f"- {dish}")
91
  else:
92
  print(menu)
93
+ print(f"Total Cost: {total_cost} PKR")