opti-alexc commited on
Commit
b0d3d02
·
1 Parent(s): 4fdc102

better food mapping, init stuff

Browse files
Files changed (4) hide show
  1. .gitignore +62 -0
  2. .python-version +1 -0
  3. app.py +151 -29
  4. pyproject.toml +14 -0
.gitignore ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Data files
2
+ *.csv
3
+ *.xlsx
4
+ *.json
5
+ *.parquet
6
+
7
+ # Python
8
+ __pycache__/
9
+ *.py[cod]
10
+ *$py.class
11
+ *.so
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # Virtual environments
32
+ venv/
33
+ env/
34
+ ENV/
35
+ env.bak/
36
+ venv.bak/
37
+
38
+ # IDE
39
+ .vscode/
40
+ .idea/
41
+ *.swp
42
+ *.swo
43
+ *~
44
+
45
+ # OS
46
+ .DS_Store
47
+ .DS_Store?
48
+ ._*
49
+ .Spotlight-V100
50
+ .Trashes
51
+ ehthumbs.db
52
+ Thumbs.db
53
+
54
+ # Gradio cache
55
+ gradio_cached_examples/
56
+ flagged/
57
+
58
+ # Logs
59
+ *.log
60
+ logs/
61
+ /uv.lock
62
+ /.claude/
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
app.py CHANGED
@@ -7,6 +7,7 @@ from functools import lru_cache
7
  from collections import Counter
8
  import requests
9
  import os
 
10
 
11
  # --- Constants and Mappings (Unchanged) ---
12
  BODY_ORDER = ['Very light-bodied', 'Light-bodied', 'Medium-bodied', 'Full-bodied', 'Very full-bodied']
@@ -22,10 +23,49 @@ COUNTRY_FLAGS = {
22
  'Romania': '🇷🇴', 'Georgia': '🇬🇪', 'Moldova': '🇲🇩', 'Switzerland': '🇨🇭', 'England': '🏴'
23
  }
24
  FOOD_EMOJIS = {
25
- 'Beef': '🥩', 'Pork': '🍖', 'Lamb': '🍖', 'Poultry': '🍗', 'Seafood': '🐟', 'Rich Fish': '🐠',
26
- 'Shellfish': '🦐', 'Cheese': '🧀', 'Pasta': '🍝', 'Pizza': '🍕', 'Vegetables': '🥬', 'Vegetarian': '🥬',
27
- 'Veal': '🥩', 'Game Meat': '🦌', 'Barbecue': '🍖', 'Codfish': '🐟', 'Sweet Dessert': '🍰', 'Dessert': '🍰',
28
- 'Appetizers': '🍾', 'Fruit': '🍇', 'Nuts': '🥜', 'Chocolate': '🍫'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  }
30
 
31
 
@@ -60,13 +100,18 @@ def download_data():
60
  @lru_cache(maxsize=1)
61
  def load_and_preprocess_data():
62
  """Loads and performs expensive one-time preprocessing on the dataset."""
 
 
 
63
  csv_filename = download_data()
 
64
 
65
  try:
66
- print("Loading CSV data...")
 
67
  # Use efficient data types and only load needed columns if possible
68
  df = pd.read_csv(csv_filename, low_memory=False)
69
- print(f"Loaded {len(df):,} wine records")
70
  except FileNotFoundError:
71
  raise FileNotFoundError(f"CSV file '{csv_filename}' not found.")
72
 
@@ -83,11 +128,24 @@ def load_and_preprocess_data():
83
  return []
84
 
85
  # Vectorized string processing for better performance
 
 
 
86
  df['grapes_list'] = df['Grapes'].fillna('[]').apply(parse_list_string)
 
 
 
87
  df['harmonize_list'] = df['Harmonize'].fillna('[]').apply(parse_list_string)
 
 
 
88
  df['main_grape'] = df['grapes_list'].apply(lambda x: x[0] if x else 'Unknown')
89
  df['num_grapes'] = df['grapes_list'].apply(len)
90
  df['body_numeric'] = df['Body'].map(BODY_MAPPING)
 
 
 
 
91
  return df
92
 
93
 
@@ -111,6 +169,9 @@ def get_top_food_pairings(harmonize_list, top_n=3):
111
 
112
  def aggregate_wine_data(df, wine_types, max_grape_count, min_samples_choice, regional_grouping):
113
  """Filters and aggregates wine data using efficient, vectorized pandas operations."""
 
 
 
114
  filtered_df = df.copy()
115
 
116
  if wine_types and 'All' not in wine_types:
@@ -122,6 +183,7 @@ def aggregate_wine_data(df, wine_types, max_grape_count, min_samples_choice, reg
122
  if regional_grouping:
123
  group_by_cols.append('Country')
124
 
 
125
  agg_df = filtered_df.groupby(group_by_cols).agg(
126
  count=('ABV', 'size'),
127
  avg_fullness=('body_numeric', 'mean'),
@@ -132,6 +194,7 @@ def aggregate_wine_data(df, wine_types, max_grape_count, min_samples_choice, reg
132
  region_count=('RegionName', 'nunique'),
133
  winery_count=('WineryName', 'nunique')
134
  ).reset_index()
 
135
 
136
  min_samples = SAMPLE_THRESHOLDS[min_samples_choice]
137
  agg_df = agg_df[agg_df['count'] >= min_samples].copy()
@@ -147,27 +210,39 @@ def aggregate_wine_data(df, wine_types, max_grape_count, min_samples_choice, reg
147
  counts = pd.Series(values_list).value_counts(normalize=True) * 100
148
  return {cat: counts.get(cat, 0.0) for cat in categories}
149
 
 
150
  agg_df['body_dist'] = agg_df['body_list'].apply(
151
  lambda x: calc_distribution(x, BODY_ORDER))
152
  agg_df['acid_dist'] = agg_df['acidity_list'].apply(
153
  lambda x: calc_distribution(x, ACIDITY_ORDER))
 
154
 
155
  # Pre-compute food pairings more efficiently
 
156
  pairing_data = []
157
  for harmonize_list in agg_df['harmonize_list']:
158
  pairing_data.append(get_top_food_pairings(harmonize_list))
159
  agg_df['pairing_data'] = pairing_data
160
  agg_df['pairing_emoji'] = agg_df['pairing_data'].apply(lambda x: x['emojis'])
161
  agg_df['pairing_names'] = agg_df['pairing_data'].apply(lambda x: x['names'])
 
 
 
162
  agg_df['wine_type_order'] = agg_df['Type'].map(WINE_TYPE_ORDER)
163
  agg_df = agg_df.sort_values(by=['wine_type_order', 'avg_fullness'], ascending=[False, True])
164
-
 
 
 
165
  return agg_df
166
 
167
 
168
  # --- OPTIMIZATION 3: Efficient & Clean Chart Creation ---
169
  def create_wine_chart(chart_data, regional_grouping):
170
  """Creates the Plotly figure with optimized traces and layout."""
 
 
 
171
  if chart_data.empty:
172
  fig = go.Figure()
173
  fig.add_annotation(text="No data available with current filters.", xref="paper", yref="paper", x=0.5, y=0.5,
@@ -176,15 +251,19 @@ def create_wine_chart(chart_data, regional_grouping):
176
 
177
  num_rows = len(chart_data)
178
 
179
- # Add wine type emoji based on type
 
180
  wine_type_emojis = {'Red': '🍷', 'White': '🥂', 'Rosé': '🌸', 'Sparkling': '🍾'}
 
181
  chart_data['wine_emoji'] = chart_data['Type'].map(wine_type_emojis).fillna('🍷')
182
 
183
  if regional_grouping:
184
  chart_data['flag'] = chart_data['Country'].map(COUNTRY_FLAGS).fillna('🌍')
185
- chart_data['grape_label'] = chart_data.apply(lambda row: f"{row['wine_emoji']} {row['main_grape']} {row['flag']}", axis=1)
 
186
  else:
187
- chart_data['grape_label'] = chart_data.apply(lambda row: f"{row['wine_emoji']} {row['main_grape']}", axis=1)
 
188
 
189
  y_labels = chart_data['grape_label'].tolist()
190
 
@@ -196,14 +275,16 @@ def create_wine_chart(chart_data, regional_grouping):
196
  shared_yaxes=True
197
  )
198
 
199
- hover_texts = chart_data.apply(
200
- lambda row: (
201
- f"<b>{row['main_grape']} ({row.get('Country', 'Global') if regional_grouping else 'Global'})</b><br>"
202
- f"Wineries: {row['winery_count']}<br>"
203
- f"Regions: {row['region_count']}<br>"
204
- f"Total Wines: {row['count']:,}"),
205
- axis=1
206
- )
 
 
207
  fig.add_trace(go.Bar(
208
  y=y_labels, x=[1] * num_rows, orientation='h',
209
  marker_color='rgba(0,0,0,0)', showlegend=False,
@@ -216,28 +297,47 @@ def create_wine_chart(chart_data, regional_grouping):
216
  hoverinfo='none', showlegend=False
217
  ), row=1, col=1)
218
 
 
 
219
  body_colors = {'Very light-bodied': '#FFB6C1', 'Light-bodied': '#CD5C5C', 'Medium-bodied': '#C13636',
220
  'Full-bodied': '#8B0000', 'Very full-bodied': '#4B0000'}
 
 
 
 
 
 
221
  for body_type in BODY_ORDER:
222
- values = chart_data['body_dist'].apply(lambda d: d.get(body_type, 0))
223
  fig.add_trace(go.Bar(
224
- y=y_labels, x=values, name=body_type, orientation='h',
225
  marker_color=body_colors.get(body_type), showlegend=False,
226
  hovertemplate=f"{body_type}: %{{x:.1f}}%<extra></extra>"
227
  ), row=1, col=2)
 
228
 
 
 
229
  acid_colors = {'Low': '#F5F5DC', 'Medium': '#DAA520', 'High': '#B8860B'}
 
 
 
 
 
 
230
  for acid_type in ACIDITY_ORDER:
231
- values = chart_data['acid_dist'].apply(lambda d: d.get(acid_type, 0))
232
  fig.add_trace(go.Bar(
233
- y=y_labels, x=values, name=acid_type, orientation='h',
234
  marker_color=acid_colors.get(acid_type), showlegend=False,
235
  hovertemplate=f"{acid_type} acidity: %{{x:.1f}}%<extra></extra>"
236
  ), row=1, col=3)
 
237
 
238
- # Color box plots by wine type
 
239
  box_colors = {'Red': '#8B0000', 'White': '#DAA520', 'Rosé': '#CD5C5C', 'Sparkling': '#9370DB'}
240
- for idx, (i, row) in enumerate(chart_data.iterrows()):
 
 
241
  abv_values = row['abv_list']
242
  color = box_colors.get(row['Type'], '#6A5ACD')
243
  fig.add_trace(go.Box(
@@ -245,6 +345,7 @@ def create_wine_chart(chart_data, regional_grouping):
245
  showlegend=False, marker_color=color, line_color=color,
246
  hovertemplate=f"ABV: %{{x:.1f}}%<extra></extra>"
247
  ), row=1, col=4)
 
248
 
249
  fig.add_trace(go.Scatter(
250
  y=y_labels, x=[0.5] * num_rows, mode='text',
@@ -265,6 +366,8 @@ def create_wine_chart(chart_data, regional_grouping):
265
  )
266
 
267
  column_titles = ["Wine / Hover for Info", "Body Profile (%)", "Acidity Profile (%)", "Alcohol (ABV %)", "Food Pairing"]
 
 
268
  for i, title in enumerate(column_titles, 1):
269
  domain = fig.layout[f'xaxis{i if i > 1 else ""}'].domain
270
  fig.add_annotation(
@@ -272,18 +375,32 @@ def create_wine_chart(chart_data, regional_grouping):
272
  xref="paper", yref="paper", text=f"<b>{title}</b>",
273
  xanchor='center', showarrow=False, font={'size': 14, 'color': '#2F2F2F'}
274
  )
 
275
 
 
 
276
  for i in range(1, 6):
277
  fig.update_yaxes(showticklabels=False, showgrid=False, zeroline=False, row=1, col=i)
278
  fig.update_xaxes(showticklabels=False, showgrid=False, zeroline=False, title_text="", row=1, col=i)
 
279
 
 
 
280
  fig.update_yaxes(categoryorder="array", categoryarray=y_labels, autorange=False, range=[-0.5, num_rows - 0.5],
281
  row=1, col=1)
282
-
283
- for i in range(num_rows):
284
- if i % 2 == 1:
285
- fig.add_hrect(y0=i - 0.5, y1=i + 0.5, fillcolor="#F0F0F0", layer="below", line_width=0, row=1, col="all")
286
-
 
 
 
 
 
 
 
 
287
  return fig
288
 
289
 
@@ -291,6 +408,9 @@ def create_wine_chart(chart_data, regional_grouping):
291
  def update_dashboard(wine_types, max_grape_count, min_samples_choice, regional_grouping,
292
  progress=gr.Progress(track_tqdm=True)):
293
  """Main function to update dashboard."""
 
 
 
294
  progress(0, desc="Loading and processing data...")
295
  df = load_and_preprocess_data()
296
 
@@ -306,6 +426,8 @@ def update_dashboard(wine_types, max_grape_count, min_samples_choice, regional_g
306
  grouping_type = "grape+region" if regional_grouping else "grape+type"
307
  summary = f"📊 Showing **{total_combinations}** {grouping_type} combinations from **{total_wines:,}** wines (min {min_samples} samples each)"
308
 
 
 
309
  return fig, summary
310
 
311
 
 
7
  from collections import Counter
8
  import requests
9
  import os
10
+ import time
11
 
12
  # --- Constants and Mappings (Unchanged) ---
13
  BODY_ORDER = ['Very light-bodied', 'Light-bodied', 'Medium-bodied', 'Full-bodied', 'Very full-bodied']
 
23
  'Romania': '🇷🇴', 'Georgia': '🇬🇪', 'Moldova': '🇲🇩', 'Switzerland': '🇨🇭', 'England': '🏴'
24
  }
25
  FOOD_EMOJIS = {
26
+ # Meat - Specific Animals
27
+ 'Beef': '🐄', 'Pork': '🐷', 'Lamb': '🐑', 'Veal': '🐄', 'Ham': '🐷',
28
+ 'Poultry': '🐔', 'Chicken': '🐔', 'Duck': '🦆',
29
+ 'Game Meat': '🦌', 'Meat': '🥩',
30
+
31
+ # Cured/Processed Meats
32
+ 'Cured Meat': '🥓', 'Cold Cuts': '🥪', 'Barbecue': '🔥', 'Grilled': '🔥', 'Roast': '🍖',
33
+
34
+ # Fish & Seafood - Specific Types
35
+ 'Rich Fish': '🐟', 'Lean Fish': '🐟', 'Fish': '🐟', 'Codfish': '🐟',
36
+ 'Shellfish': '🦐', 'Seafood': '🦞', 'Sushi': '🍣', 'Sashimi': '🍣',
37
+
38
+ # Cheese - Different Types
39
+ 'Cheese': '🧀', 'Soft Cheese': '🧀', 'Hard Cheese': '🧀', 'Blue Cheese': '🧀',
40
+ 'Goat Cheese': '🐐', 'Maturated Cheese': '🧀', 'Mild Cheese': '🧀', 'Medium-cured Cheese': '🧀',
41
+
42
+ # Pasta & Italian
43
+ 'Pasta': '🍝', 'Tagliatelle': '🍝', 'Lasagna': '🍝', 'Risotto': '🍚',
44
+ 'Pizza': '🍕', 'Eggplant Parmigiana': '🍆',
45
+
46
+ # Asian Food
47
+ 'Asian Food': '🥢', 'Curry Chicken': '🍛', 'Yakissoba': '🍜', 'Paella': '🥘',
48
+
49
+ # Vegetables & Vegetarian
50
+ 'Vegetarian': '🥬', 'Salad': '🥗', 'Mushrooms': '🍄', 'Beans': '🫘',
51
+ 'Baked Potato': '🥔', 'French Fries': '🍟',
52
+
53
+ # Desserts & Sweets
54
+ 'Sweet Dessert': '🍰', 'Dessert': '🍰', 'Fruit Dessert': '🍓',
55
+ 'Cake': '🍰', 'Cookies': '🍪', 'Chocolate': '🍫', 'Cream': '🍨',
56
+ 'Citric Dessert': '🍋', 'Spiced Fruit Cake': '🍰',
57
+
58
+ # Fruits & Nuts
59
+ 'Fruit': '🍇', 'Dried Fruits': '🍇', 'Chestnut': '🌰',
60
+
61
+ # Appetizers & Snacks
62
+ 'Appetizer': '🍤', 'Snack': '🥨', 'Aperitif': '🥂',
63
+
64
+ # Soups & Stews
65
+ 'Light Stews': '🍲', 'Soufflé': '🥄',
66
+
67
+ # Dishes & Preparations
68
+ 'Spicy Food': '🌶️', 'Tomato Dishes': '🍅'
69
  }
70
 
71
 
 
100
  @lru_cache(maxsize=1)
101
  def load_and_preprocess_data():
102
  """Loads and performs expensive one-time preprocessing on the dataset."""
103
+ start_time = time.time()
104
+ print("[TIMING] Starting data loading and preprocessing...")
105
+
106
  csv_filename = download_data()
107
+ print(f"[TIMING] File check/download completed in {time.time() - start_time:.2f}s")
108
 
109
  try:
110
+ csv_start = time.time()
111
+ print("[TIMING] Loading CSV data...")
112
  # Use efficient data types and only load needed columns if possible
113
  df = pd.read_csv(csv_filename, low_memory=False)
114
+ print(f"[TIMING] CSV loaded in {time.time() - csv_start:.2f}s - {len(df):,} wine records")
115
  except FileNotFoundError:
116
  raise FileNotFoundError(f"CSV file '{csv_filename}' not found.")
117
 
 
128
  return []
129
 
130
  # Vectorized string processing for better performance
131
+ parse_start = time.time()
132
+ print("[TIMING] Starting string parsing...")
133
+
134
  df['grapes_list'] = df['Grapes'].fillna('[]').apply(parse_list_string)
135
+ print(f"[TIMING] Grapes parsing completed in {time.time() - parse_start:.2f}s")
136
+
137
+ harmonize_start = time.time()
138
  df['harmonize_list'] = df['Harmonize'].fillna('[]').apply(parse_list_string)
139
+ print(f"[TIMING] Harmonize parsing completed in {time.time() - harmonize_start:.2f}s")
140
+
141
+ derived_start = time.time()
142
  df['main_grape'] = df['grapes_list'].apply(lambda x: x[0] if x else 'Unknown')
143
  df['num_grapes'] = df['grapes_list'].apply(len)
144
  df['body_numeric'] = df['Body'].map(BODY_MAPPING)
145
+ print(f"[TIMING] Derived columns completed in {time.time() - derived_start:.2f}s")
146
+
147
+ total_time = time.time() - start_time
148
+ print(f"[TIMING] Total preprocessing completed in {total_time:.2f}s")
149
  return df
150
 
151
 
 
169
 
170
  def aggregate_wine_data(df, wine_types, max_grape_count, min_samples_choice, regional_grouping):
171
  """Filters and aggregates wine data using efficient, vectorized pandas operations."""
172
+ agg_start = time.time()
173
+ print(f"[TIMING] Starting aggregation with {len(df):,} records...")
174
+
175
  filtered_df = df.copy()
176
 
177
  if wine_types and 'All' not in wine_types:
 
183
  if regional_grouping:
184
  group_by_cols.append('Country')
185
 
186
+ groupby_start = time.time()
187
  agg_df = filtered_df.groupby(group_by_cols).agg(
188
  count=('ABV', 'size'),
189
  avg_fullness=('body_numeric', 'mean'),
 
194
  region_count=('RegionName', 'nunique'),
195
  winery_count=('WineryName', 'nunique')
196
  ).reset_index()
197
+ print(f"[TIMING] GroupBy aggregation completed in {time.time() - groupby_start:.2f}s")
198
 
199
  min_samples = SAMPLE_THRESHOLDS[min_samples_choice]
200
  agg_df = agg_df[agg_df['count'] >= min_samples].copy()
 
210
  counts = pd.Series(values_list).value_counts(normalize=True) * 100
211
  return {cat: counts.get(cat, 0.0) for cat in categories}
212
 
213
+ dist_start = time.time()
214
  agg_df['body_dist'] = agg_df['body_list'].apply(
215
  lambda x: calc_distribution(x, BODY_ORDER))
216
  agg_df['acid_dist'] = agg_df['acidity_list'].apply(
217
  lambda x: calc_distribution(x, ACIDITY_ORDER))
218
+ print(f"[TIMING] Distribution calculations completed in {time.time() - dist_start:.2f}s")
219
 
220
  # Pre-compute food pairings more efficiently
221
+ pairing_start = time.time()
222
  pairing_data = []
223
  for harmonize_list in agg_df['harmonize_list']:
224
  pairing_data.append(get_top_food_pairings(harmonize_list))
225
  agg_df['pairing_data'] = pairing_data
226
  agg_df['pairing_emoji'] = agg_df['pairing_data'].apply(lambda x: x['emojis'])
227
  agg_df['pairing_names'] = agg_df['pairing_data'].apply(lambda x: x['names'])
228
+ print(f"[TIMING] Food pairing calculations completed in {time.time() - pairing_start:.2f}s")
229
+
230
+ final_start = time.time()
231
  agg_df['wine_type_order'] = agg_df['Type'].map(WINE_TYPE_ORDER)
232
  agg_df = agg_df.sort_values(by=['wine_type_order', 'avg_fullness'], ascending=[False, True])
233
+ print(f"[TIMING] Final sorting completed in {time.time() - final_start:.2f}s")
234
+
235
+ total_agg_time = time.time() - agg_start
236
+ print(f"[TIMING] Total aggregation completed in {total_agg_time:.2f}s - {len(agg_df)} combinations")
237
  return agg_df
238
 
239
 
240
  # --- OPTIMIZATION 3: Efficient & Clean Chart Creation ---
241
  def create_wine_chart(chart_data, regional_grouping):
242
  """Creates the Plotly figure with optimized traces and layout."""
243
+ chart_start = time.time()
244
+ print(f"[TIMING] Starting chart creation with {len(chart_data)} rows...")
245
+
246
  if chart_data.empty:
247
  fig = go.Figure()
248
  fig.add_annotation(text="No data available with current filters.", xref="paper", yref="paper", x=0.5, y=0.5,
 
251
 
252
  num_rows = len(chart_data)
253
 
254
+ # Pre-compute labels more efficiently
255
+ prep_start = time.time()
256
  wine_type_emojis = {'Red': '🍷', 'White': '🥂', 'Rosé': '🌸', 'Sparkling': '🍾'}
257
+ chart_data = chart_data.copy() # Avoid SettingWithCopyWarning
258
  chart_data['wine_emoji'] = chart_data['Type'].map(wine_type_emojis).fillna('🍷')
259
 
260
  if regional_grouping:
261
  chart_data['flag'] = chart_data['Country'].map(COUNTRY_FLAGS).fillna('🌍')
262
+ # Vectorized string concatenation instead of apply
263
+ chart_data['grape_label'] = chart_data['wine_emoji'] + ' ' + chart_data['main_grape'] + ' ' + chart_data['flag']
264
  else:
265
+ chart_data['grape_label'] = chart_data['wine_emoji'] + ' ' + chart_data['main_grape']
266
+ print(f"[TIMING] Label preparation completed in {time.time() - prep_start:.2f}s")
267
 
268
  y_labels = chart_data['grape_label'].tolist()
269
 
 
275
  shared_yaxes=True
276
  )
277
 
278
+ # Pre-compute hover texts more efficiently
279
+ hover_start = time.time()
280
+ country_part = chart_data['Country'] if regional_grouping else 'Global'
281
+ hover_texts = (
282
+ '<b>' + chart_data['main_grape'] + ' (' + country_part.astype(str) + ')</b><br>' +
283
+ 'Wineries: ' + chart_data['winery_count'].astype(str) + '<br>' +
284
+ 'Regions: ' + chart_data['region_count'].astype(str) + '<br>' +
285
+ 'Total Wines: ' + chart_data['count'].apply(lambda x: f"{x:,}")
286
+ ).tolist()
287
+ print(f"[TIMING] Hover text preparation completed in {time.time() - hover_start:.2f}s")
288
  fig.add_trace(go.Bar(
289
  y=y_labels, x=[1] * num_rows, orientation='h',
290
  marker_color='rgba(0,0,0,0)', showlegend=False,
 
297
  hoverinfo='none', showlegend=False
298
  ), row=1, col=1)
299
 
300
+ # Optimize body distribution traces
301
+ body_start = time.time()
302
  body_colors = {'Very light-bodied': '#FFB6C1', 'Light-bodied': '#CD5C5C', 'Medium-bodied': '#C13636',
303
  'Full-bodied': '#8B0000', 'Very full-bodied': '#4B0000'}
304
+
305
+ # Pre-extract all body values at once
306
+ body_data = {}
307
+ for body_type in BODY_ORDER:
308
+ body_data[body_type] = [d.get(body_type, 0) for d in chart_data['body_dist']]
309
+
310
  for body_type in BODY_ORDER:
 
311
  fig.add_trace(go.Bar(
312
+ y=y_labels, x=body_data[body_type], name=body_type, orientation='h',
313
  marker_color=body_colors.get(body_type), showlegend=False,
314
  hovertemplate=f"{body_type}: %{{x:.1f}}%<extra></extra>"
315
  ), row=1, col=2)
316
+ print(f"[TIMING] Body traces completed in {time.time() - body_start:.2f}s")
317
 
318
+ # Optimize acidity distribution traces
319
+ acid_start = time.time()
320
  acid_colors = {'Low': '#F5F5DC', 'Medium': '#DAA520', 'High': '#B8860B'}
321
+
322
+ # Pre-extract all acidity values at once
323
+ acid_data = {}
324
+ for acid_type in ACIDITY_ORDER:
325
+ acid_data[acid_type] = [d.get(acid_type, 0) for d in chart_data['acid_dist']]
326
+
327
  for acid_type in ACIDITY_ORDER:
 
328
  fig.add_trace(go.Bar(
329
+ y=y_labels, x=acid_data[acid_type], name=acid_type, orientation='h',
330
  marker_color=acid_colors.get(acid_type), showlegend=False,
331
  hovertemplate=f"{acid_type} acidity: %{{x:.1f}}%<extra></extra>"
332
  ), row=1, col=3)
333
+ print(f"[TIMING] Acidity traces completed in {time.time() - acid_start:.2f}s")
334
 
335
+ # Optimize box plot creation
336
+ box_start = time.time()
337
  box_colors = {'Red': '#8B0000', 'White': '#DAA520', 'Rosé': '#CD5C5C', 'Sparkling': '#9370DB'}
338
+
339
+ # Create box plots more efficiently
340
+ for idx, (_, row) in enumerate(chart_data.iterrows()):
341
  abv_values = row['abv_list']
342
  color = box_colors.get(row['Type'], '#6A5ACD')
343
  fig.add_trace(go.Box(
 
345
  showlegend=False, marker_color=color, line_color=color,
346
  hovertemplate=f"ABV: %{{x:.1f}}%<extra></extra>"
347
  ), row=1, col=4)
348
+ print(f"[TIMING] Box plot traces completed in {time.time() - box_start:.2f}s")
349
 
350
  fig.add_trace(go.Scatter(
351
  y=y_labels, x=[0.5] * num_rows, mode='text',
 
366
  )
367
 
368
  column_titles = ["Wine / Hover for Info", "Body Profile (%)", "Acidity Profile (%)", "Alcohol (ABV %)", "Food Pairing"]
369
+ # Add column titles
370
+ title_start = time.time()
371
  for i, title in enumerate(column_titles, 1):
372
  domain = fig.layout[f'xaxis{i if i > 1 else ""}'].domain
373
  fig.add_annotation(
 
375
  xref="paper", yref="paper", text=f"<b>{title}</b>",
376
  xanchor='center', showarrow=False, font={'size': 14, 'color': '#2F2F2F'}
377
  )
378
+ print(f"[TIMING] Column titles completed in {time.time() - title_start:.2f}s")
379
 
380
+ # Update axes formatting
381
+ axes_start = time.time()
382
  for i in range(1, 6):
383
  fig.update_yaxes(showticklabels=False, showgrid=False, zeroline=False, row=1, col=i)
384
  fig.update_xaxes(showticklabels=False, showgrid=False, zeroline=False, title_text="", row=1, col=i)
385
+ print(f"[TIMING] Axes formatting completed in {time.time() - axes_start:.2f}s")
386
 
387
+ # Final axis configuration
388
+ final_axes_start = time.time()
389
  fig.update_yaxes(categoryorder="array", categoryarray=y_labels, autorange=False, range=[-0.5, num_rows - 0.5],
390
  row=1, col=1)
391
+ print(f"[TIMING] Final axis configuration completed in {time.time() - final_axes_start:.2f}s")
392
+
393
+ # Skip alternating row backgrounds for better performance
394
+ # The rectangles were causing major slowdown (3+ seconds)
395
+ # bg_start = time.time()
396
+ # for i in range(num_rows):
397
+ # if i % 2 == 1:
398
+ # fig.add_hrect(y0=i - 0.5, y1=i + 0.5, fillcolor="#F0F0F0", layer="below", line_width=0, row=1, col="all")
399
+ # print(f"[TIMING] Background rectangles completed in {time.time() - bg_start:.2f}s")
400
+ print("[TIMING] Skipped background rectangles for performance")
401
+
402
+ chart_time = time.time() - chart_start
403
+ print(f"[TIMING] Chart creation completed in {chart_time:.2f}s")
404
  return fig
405
 
406
 
 
408
  def update_dashboard(wine_types, max_grape_count, min_samples_choice, regional_grouping,
409
  progress=gr.Progress(track_tqdm=True)):
410
  """Main function to update dashboard."""
411
+ dashboard_start = time.time()
412
+ print(f"[TIMING] Starting dashboard update...")
413
+
414
  progress(0, desc="Loading and processing data...")
415
  df = load_and_preprocess_data()
416
 
 
426
  grouping_type = "grape+region" if regional_grouping else "grape+type"
427
  summary = f"📊 Showing **{total_combinations}** {grouping_type} combinations from **{total_wines:,}** wines (min {min_samples} samples each)"
428
 
429
+ total_dashboard_time = time.time() - dashboard_start
430
+ print(f"[TIMING] Total dashboard update completed in {total_dashboard_time:.2f}s")
431
  return fig, summary
432
 
433
 
pyproject.toml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "wine-analysis"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "pandas",
9
+ "plotly",
10
+ "gradio",
11
+ "numpy",
12
+ "scipy",
13
+ "requests"
14
+ ]