File size: 17,754 Bytes
41173b4
 
 
 
e3c8762
41173b4
 
a53f165
 
 
 
e3c8762
a53f165
 
 
 
 
41173b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3c8762
41173b4
 
 
 
a53f165
 
 
 
 
5a9e05e
a53f165
 
 
 
 
 
 
e3c8762
 
 
 
 
 
 
41173b4
 
 
 
 
 
 
d0bb838
41173b4
 
 
8724d8b
41173b4
 
 
 
 
 
 
 
 
 
 
e3c8762
 
 
 
 
 
41173b4
 
 
25272b8
3a4e096
41173b4
8724d8b
 
a53f165
a729a18
 
 
 
 
a53f165
 
 
 
8724d8b
d0bb838
 
 
 
823be8e
d0bb838
8724d8b
 
 
4b0e12e
 
31c70a6
 
 
 
 
4b0e12e
 
 
31c70a6
 
 
4b0e12e
 
 
31c70a6
4b0e12e
d0bb838
4b0e12e
d0bb838
 
 
 
 
4b0e12e
 
 
 
d0bb838
 
 
4b0e12e
25272b8
 
4b0e12e
 
 
25272b8
4b0e12e
d0bb838
4b0e12e
d0bb838
4b0e12e
d0bb838
 
 
 
 
4b0e12e
 
 
d0bb838
 
4b0e12e
 
d0bb838
 
4b0e12e
 
 
d0bb838
4b0e12e
d0bb838
4b0e12e
d0bb838
4b0e12e
d0bb838
823be8e
 
 
 
4b0e12e
 
 
823be8e
 
 
4b0e12e
 
823be8e
4b0e12e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823be8e
26485d2
823be8e
 
 
 
 
26485d2
823be8e
 
 
26485d2
9889912
823be8e
5a9e05e
823be8e
9889912
823be8e
9889912
823be8e
9889912
823be8e
9889912
 
 
 
 
823be8e
 
 
26485d2
823be8e
 
 
 
 
 
 
 
bd7b6e0
823be8e
 
 
 
26485d2
 
823be8e
 
a729a18
 
 
 
 
 
 
 
7214c18
 
41173b4
 
 
 
 
 
 
 
 
 
7214c18
 
41173b4
 
 
 
8724d8b
41173b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8724d8b
5a9e05e
 
 
 
 
 
 
 
 
 
 
 
7214c18
a53f165
a729a18
a53f165
 
41173b4
5a9e05e
a53f165
 
 
 
8724d8b
7214c18
 
 
a729a18
 
 
7214c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0bb838
 
 
7214c18
 
5a9e05e
41173b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/b4/lwfgccm95kqd2skcwvrt2fr00000gn/T/ipykernel_289/426293969.py:99: FutureWarning:\n",
      "\n",
      "Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"650\"\n",
       "            src=\"http://0.0.0.0:8050/\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x2a9f7d000>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ruimelo/Documents/GitHub/eda/app/core.py:21: SettingWithCopyWarning:\n",
      "\n",
      "\n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Text Value: None\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import plotly.express as px\n",
    "import plotly.graph_objects as go\n",
    "import pandas as pd\n",
    "from dash import Dash, html, dcc, Input, Output, callback, dash_table\n",
    "import plotly.express as px\n",
    "import numpy as np\n",
    "from plotly.subplots import make_subplots\n",
    "import core\n",
    "\n",
    "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv')\n",
    "debug = False\n",
    "\n",
    "external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']\n",
    "\n",
    "app = Dash(__name__, external_stylesheets=external_stylesheets)\n",
    "\n",
    "app.layout = html.Div([\n",
    "    dcc.Location(id='url', refresh=False),\n",
    "    html.Div(id='page-content')\n",
    "], style={\n",
    "    'background-color': '#333',  # Dark background color\n",
    "    'color': '#fff',  # Text color\n",
    "    'min-height': '100vh',  # Ensure it covers the full viewport height\n",
    "    'padding': '20px'  # Add some padding\n",
    "})\n",
    "\n",
    "server = app.server\n",
    "\n",
    "raw_wine_similarity_df = pd.read_csv('data/sogrape_wine.csv')\n",
    "raw_wine_similarity_df = raw_wine_similarity_df.iloc[:3, :]\n",
    "\n",
    "wine_similarity_df = pd.read_csv('data/wine_similarity.csv')\n",
    "wine_list = wine_similarity_df['NAME'].unique()\n",
    "# wine_list.sort()\n",
    "geo = pd.read_csv('data/processed_wineyards.csv')\n",
    "wine_regions=list(geo['Region'].unique()) + ['all']\n",
    "wine_country = list(geo['Country'].unique()) + ['all']\n",
    "# local_type = list(geo['LOCAL TYPE'].unique()) + ['all']\n",
    "\n",
    "user_rating_df = pd.read_csv('data/simulated_ratings.csv')\n",
    "user_rating_df.set_index('user', inplace=True)\n",
    "user_rating_df['user'] = user_rating_df.index\n",
    "user_ids = user_rating_df['user']\n",
    "\n",
    "raw_ratings = pd.read_csv('data/raw_simulated_ratings.csv')\n",
    "# crop to 5 rows and 7 columns\n",
    "raw_ratings = raw_ratings.iloc[:5, :7]\n",
    "\n",
    "TEMPORARY_WINE_RECOMMENDATION_FORM_INFO = {}\n",
    "\n",
    "\n",
    "\n",
    "## Layout ##\n",
    "dashboard_layout = html.Div([\n",
    "    html.H1(children='Wineyards around the world', style={'textAlign': 'center', 'color': '#fff', 'padding': '20px'}),\n",
    "    \n",
    "    html.Div(\n",
    "        [\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.H4(children='Country', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "                    dcc.Dropdown(wine_country, wine_country[-1], id='dropdown-wc', style={'color': '#000'}),\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            ),\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.H4(children='Region', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "                    dcc.Dropdown(['all'], 'all', id='dropdown-wr', style={'color': '#000'}),\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            )\n",
    "    ], className='row', style={'background-color': '#444', 'border-radius': '10px', 'margin': '10px'}),\n",
    "    \n",
    "    dcc.Graph(id='world-map-fig', style={'margin': '20px'}),\n",
    "    \n",
    "    html.Div(\n",
    "        [\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.H4(children='Wine Recommender', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "                    dcc.Dropdown(wine_list, wine_list[0], id='dropdown-selection', style={'color': '#000'}),\n",
    "                    html.P(id='wine-recommendation', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "                ], className='three columns', style={'padding': '10px'}\n",
    "            ),\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.P(children='Data Example', style={'textAlign': 'left', 'color': '#fff'}),\n",
    "                    dash_table.DataTable(\n",
    "                        data=raw_wine_similarity_df.to_dict('records'),\n",
    "                        columns=[{'id': c, 'name': c} for c in raw_wine_similarity_df.columns],\n",
    "                        style_header={'backgroundColor': '#444', 'color': '#fff'},\n",
    "                        style_cell={'backgroundColor': '#333', 'color': '#fff'}\n",
    "                    )\n",
    "                ], className='nine columns', style={'padding': '10px'}\n",
    "            )\n",
    "    ], className='row', style={'background-color': '#444', 'border-radius': '10px', 'margin': '10px'}),\n",
    "    \n",
    "    html.H4(children=\"Wine Recommender based on users' feedback\", style={'textAlign': 'center', 'color': '#fff', 'padding': '20px'}),\n",
    "    \n",
    "    html.Div(\n",
    "        [\n",
    "            html.Div(\n",
    "                [\n",
    "                    dcc.Dropdown(user_ids, user_ids[0], id='dropdown-selection-user', style={'color': '#000'}),\n",
    "                    html.P(id='wine-recommendation-from-user', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            ),\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.P(children='Data Example', style={'textAlign': 'left', 'color': '#fff'}),\n",
    "                    dash_table.DataTable(\n",
    "                        data=raw_ratings.to_dict('records'),\n",
    "                        columns=[{'id': c, 'name': c} for c in raw_ratings.columns],\n",
    "                        style_header={'backgroundColor': '#444', 'color': '#fff'},\n",
    "                        style_cell={'backgroundColor': '#333', 'color': '#fff'}\n",
    "                    )\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            ),\n",
    "    ], className='row', style={'background-color': '#444', 'border-radius': '10px', 'margin': '10px'}),\n",
    "    \n",
    "    html.H4(children=\"Wine Preferences Form\", style={'textAlign': 'center', 'color': '#fff', 'padding': '20px'}),\n",
    "    \n",
    "    html.Div(\n",
    "        [\n",
    "            html.Div(\n",
    "                [\n",
    "                    dcc.Dropdown(wine_list, wine_list[0], id='dropdown-selection-wine-form', style={'color': '#000'}),\n",
    "                    dcc.Input(id='input-wine-rating', type='number', placeholder='Enter wine rating', min=1, max=5, style={'margin-top': '10px'}),\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            ),\n",
    "            html.Div(\n",
    "                [\n",
    "                    html.Button('Submit', id='submit-button', n_clicks=0, style={'margin-top': '10px', 'background-color': '#444', 'color': '#fff'}),\n",
    "                ], className='six columns', style={'padding': '10px'}\n",
    "            ),\n",
    "        ], className='row', style={'background-color': '#444', 'border-radius': '10px', 'margin': '10px'}),\n",
    "    \n",
    "    html.Div(id='recommended-wine-rating-info', style={'textAlign': 'center', 'color': '#fff', 'padding': '10px'}),\n",
    "    html.P(id='recommend-wine-from-form-text', style={'textAlign': 'center', 'color': '#fff'}),\n",
    "    \n",
    "    html.Div(\n",
    "        [\n",
    "            html.Button('Reset', id='recommend-wine-from-form-reset', n_clicks=0, style={'margin': '10px', 'background-color': '#444', 'color': '#fff'}),\n",
    "            html.Button('Recommend Wine', id='recommend-wine-from-form', n_clicks=0, style={'margin': '10px', 'background-color': '#444', 'color': '#fff'}),\n",
    "        ], style={'textAlign': 'center'}\n",
    "    ),\n",
    "    \n",
    "    html.Div(id='recommended-wine-form-output', style={'textAlign': 'center', 'color': '#fff', 'padding': '10px'}),\n",
    "], style={\n",
    "    'background-image': 'url(\"/assets/background.jpg\")',\n",
    "    'background-size': 'cover',\n",
    "    'font-family': 'Fantasy',\n",
    "    'color': '#999',\n",
    "    'padding': '20px'\n",
    "})\n",
    "@app.callback(\n",
    "    Output('recommend-wine-from-form-reset', 'n_clicks'),\n",
    "    Input('recommend-wine-from-form-reset', 'n_clicks'),\n",
    ")\n",
    "def reset_form(n_clicks):\n",
    "    if n_clicks > 0:\n",
    "        TEMPORARY_WINE_RECOMMENDATION_FORM_INFO.clear()\n",
    "        return 0\n",
    "\n",
    "@app.callback(\n",
    "    Output('recommended-wine-rating-info', 'children'),\n",
    "    Output('submit-button', 'n_clicks'),\n",
    "    Output('recommend-wine-from-form-text', 'children'),\n",
    "    Input('submit-button', 'n_clicks'),\n",
    "    Input('dropdown-selection-wine-form', 'value'),\n",
    "    Input('input-wine-rating', 'value'),\n",
    "    Input('recommend-wine-from-form-text', 'children'),\n",
    ")\n",
    "def update_output(n_clicks, wine_name, rating, text_value):\n",
    "    if n_clicks > 0:\n",
    "        print(f\"Text Value: {text_value}\")\n",
    "        TEMPORARY_WINE_RECOMMENDATION_FORM_INFO[wine_name] = rating\n",
    "        if not text_value:\n",
    "            text_value = \"|\"\n",
    "        text_value += f\"{wine_name}: {rating} | \"\n",
    "        return f'You rated {wine_name} with a score of {rating}', 0, text_value\n",
    "    return '', 0, text_value\n",
    "\n",
    "@app.callback(\n",
    "    Output('recommended-wine-form-output', 'children'),\n",
    "    Output('recommend-wine-from-form', 'n_clicks'),\n",
    "    Input('recommend-wine-from-form', 'n_clicks'),\n",
    ")\n",
    "def recommend_wine_from_form(n_clicks):\n",
    "    if n_clicks > 0:\n",
    "        user = \"temporary_user\"\n",
    "        user_rating_df.loc[user] = TEMPORARY_WINE_RECOMMENDATION_FORM_INFO\n",
    "        user_rating_df.fillna(0, inplace=True)\n",
    "        user_rating_df['user'] = user_rating_df.index\n",
    "        user_rating_df['cluster'] = core.get_most_similar_user_clust(user_rating_df, user)\n",
    "\n",
    "\n",
    "        wine_recommendation_from_user = core.recommend_wine_from_users(user_rating_df, user, 3)\n",
    "        wine_recommendation_from_user = f\"Based on user form, we recommend: \"+\"; \".join(wine_recommendation_from_user)\n",
    "        return wine_recommendation_from_user, 0\n",
    "    return '', 0\n",
    "\n",
    "\n",
    "@app.callback(\n",
    "    Output('dropdown-wr', 'options'),\n",
    "    Input('dropdown-wc', 'value')\n",
    ")\n",
    "def set_wine_type_options(selected_wine_type):\n",
    "    # Return the options based on the selected wine country\n",
    "    pattern = r'.*' if selected_wine_type == 'all' else rf'{selected_wine_type}'\n",
    "    return list(geo[geo['Country'].str.match(pattern, na=False)]['Region'].unique()) + ['all']\n",
    "\n",
    "\n",
    "wiki_layout = html.Div([\n",
    "    dcc.Link('Dashboard', href='/'),\n",
    "\n",
    "    html.H1('About this project'),\n",
    "\n",
    "    html.Div([\n",
    "        html.Div([\n",
    "\n",
    "                    html.H3('What is this project about?'),\n",
    "\n",
    "                    html.P('We are a group of 4 Computer Science Engineering Students with a solid Artificial Intelligence background.'),\n",
    "                    html.P('This project aims to showcase AI applications for improving Wine Tourism for SOGRAPE.'),\n",
    "\n",
    "\n",
    "                    html.H3('\\'Bout us'),\n",
    "                    html.Img(src='/assets/tourdevino_logo.webp', style={'width': '40%', 'height': 'auto', 'display': 'block', 'margin-left': 'auto', 'margin-right': 'auto'}),\n",
    "                    html.P('This project was developed by a team of 4, in the context of the SOGRAPE 2024 hackathon.'),\n",
    "                    html.P('The team members are:'),\n",
    "                    html.H4('Rui Melo'),\n",
    "                    html.H4('André Catarino'),\n",
    "                    html.H4('Dinis Costa'),\n",
    "                    html.H4('Paulo Fidalgo'),\n",
    "\n",
    "\n",
    "        ], className='six columns'),], className='row'),\n",
    "],\n",
    "style={'background-color': '#333', 'font-family': 'Fantasy', 'color': '#999', 'padding': '10px'}\n",
    "\n",
    ")\n",
    "\n",
    "# Update the index\n",
    "@callback(Output('page-content', 'children'), Input('url', 'pathname'))\n",
    "def display_page(pathname):\n",
    "    if pathname == '/':\n",
    "        return dashboard_layout\n",
    "    elif pathname == '/wiki':\n",
    "        return wiki_layout\n",
    "    else:\n",
    "        return '404'\n",
    "    # You could also return a 404 \"URL not found\" page here\n",
    "\n",
    "@app.callback(\n",
    "    Output('wine-recommendation', 'children'),\n",
    "    Input('dropdown-selection', 'value')\n",
    ")\n",
    "def update_recommendation(value):\n",
    "    if value:\n",
    "        recommended_wines = core.get_top_5_similar_wines(value, wine_similarity_df)[1:]\n",
    "        return f\"Based on ´{value}´, we recommend: \"+\"; \".join(recommended_wines)\n",
    "    return ''\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "@app.callback(\n",
    "    Output('world-map-fig', 'figure'),\n",
    "    Output('wine-recommendation-from-user', 'children'),\n",
    "    Input('dropdown-wr', 'value'),\n",
    "    Input('dropdown-wc', 'value'),\n",
    "    Input('dropdown-selection-user', 'value')\n",
    ")\n",
    "def update_graph(wr,wc, user_value):\n",
    "    ## Wine Recommendation from users feedback\n",
    "\n",
    "    wine_recommendation_from_user = core.recommend_wine_from_users(user_rating_df, user_value, 3)\n",
    "    wine_recommendation_from_user = f\"Based on user information, we recommend: \"+\"; \".join(wine_recommendation_from_user)\n",
    "\n",
    "    ### World Map of wineyards ###\n",
    "\n",
    "    geo_df = pd.read_csv('data/processed_wineyards.csv')\n",
    "    wr = r'.*' if wr == 'all' else rf'{wr}'\n",
    "    wc = r'.*' if wc == 'all' else rf'{wc}'\n",
    "    geo_df = geo_df[geo_df['Region'].str.contains(wr, case=False, na=False, regex=True) & geo_df['Country'].str.contains(wc, case=False, na=False, regex=True)]\n",
    "\n",
    "\n",
    "    world_map_fig = px.scatter_map(geo_df,\n",
    "                            lat=geo_df['coord_x'],\n",
    "                            lon=geo_df['coord_y'],\n",
    "                            hover_name=geo_df['name'],\n",
    "                            zoom=4,\n",
    "                            hover_data={\n",
    "                                'IsTouristic': True,\n",
    "                                'Wine Type': True,\n",
    "                                'Country': True,\n",
    "                                'Region': True,\n",
    "                                'Address': True,\n",
    "\n",
    "                            },\n",
    "                            title='Wineyards around the world',\n",
    "                            )\n",
    "    \n",
    "    world_map_fig.update_layout(height=800, width=1200)\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "    return world_map_fig, wine_recommendation_from_user\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run_server(host=\"0.0.0.0\", port=\"8050\", debug=debug)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "atc-smart-shower-YhjpRjjr-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}