vitorbborges commited on
Commit
2f1c2d1
·
1 Parent(s): 7880721

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.ipynb +225 -0
  2. app.py +1 -0
app.ipynb ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "metadata": {
7
+ "ExecuteTime": {
8
+ "end_time": "2022-12-07T02:48:57.993204Z",
9
+ "start_time": "2022-12-07T02:48:57.959327Z"
10
+ }
11
+ },
12
+ "outputs": [
13
+ {
14
+ "name": "stdout",
15
+ "output_type": "stream",
16
+ "text": [
17
+ "Running on local URL: http://127.0.0.1:7860\n",
18
+ "\n",
19
+ "To create a public link, set `share=True` in `launch()`.\n"
20
+ ]
21
+ },
22
+ {
23
+ "data": {
24
+ "text/html": [
25
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
26
+ ],
27
+ "text/plain": [
28
+ "<IPython.core.display.HTML object>"
29
+ ]
30
+ },
31
+ "metadata": {},
32
+ "output_type": "display_data"
33
+ },
34
+ {
35
+ "name": "stderr",
36
+ "output_type": "stream",
37
+ "text": [
38
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
39
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
40
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
41
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
42
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
43
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
44
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
45
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
46
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
47
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
48
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
49
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
50
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
51
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
52
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
53
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
54
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
55
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
56
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
57
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
58
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
59
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
60
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
61
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
62
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
63
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
64
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
65
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
66
+ "C:\\Users\\vitor\\AppData\\Local\\Temp\\ipykernel_5048\\977487457.py:41: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
67
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n"
68
+ ]
69
+ }
70
+ ],
71
+ "source": [
72
+ "import gradio as gr\n",
73
+ "import random\n",
74
+ "import pandas as pd\n",
75
+ "import numpy as np\n",
76
+ "\n",
77
+ "opo = pd.read_csv('oportunidades_results.csv', lineterminator='\\n')\n",
78
+ "opo = opo.iloc[np.where(opo['opo_brazil']=='Y')]\n",
79
+ "opo.index = range(len(opo))\n",
80
+ "simulation = pd.read_csv('simulation2.csv')\n",
81
+ "userID = max(simulation['userID']) + 1\n",
82
+ "\n",
83
+ "def build_display_text(opo_n):\n",
84
+ " \n",
85
+ " title = opo.loc[opo_n]['opo_titulo']\n",
86
+ " link = opo.loc[opo_n]['link']\n",
87
+ " summary = opo.loc[opo_n]['facebook-bart-large-cnn_results']\n",
88
+ "\n",
89
+ " display_text = f\"**{title}**\\n\\nURL:\\n{link}\\n\\nSUMMARY:\\n{summary}\"\n",
90
+ "\n",
91
+ " return display_text\n",
92
+ "\n",
93
+ "opo_n_one = random.randrange(len(opo))\n",
94
+ "opo_n_two = random.randrange(len(opo))\n",
95
+ "opo_n_three = random.randrange(len(opo))\n",
96
+ "opo_n_four = random.randrange(len(opo))\n",
97
+ "\n",
98
+ "evaluated = []\n",
99
+ "\n",
100
+ "def predict_next(option, nota):\n",
101
+ " global userID\n",
102
+ " global opo_n_one\n",
103
+ " global opo_n_two\n",
104
+ " global opo_n_three\n",
105
+ " global opo_n_four\n",
106
+ " global evaluated\n",
107
+ " global opo\n",
108
+ " global simulation\n",
109
+ "\n",
110
+ " selected = [opo_n_one, opo_n_two, opo_n_three, opo_n_four][int(option)-1]\n",
111
+ "\n",
112
+ " simulation = simulation.append({'userID': userID, 'itemID': selected, 'rating': nota}, ignore_index=True)\n",
113
+ " evaluated.append(selected)\n",
114
+ " \n",
115
+ " from surprise import Reader\n",
116
+ " reader = Reader(rating_scale=(1, 5))\n",
117
+ "\n",
118
+ " from surprise import Dataset\n",
119
+ " data = Dataset.load_from_df(simulation[['userID', 'itemID', 'rating']], reader)\n",
120
+ " trainset = data.build_full_trainset()\n",
121
+ "\n",
122
+ " from surprise import SVDpp\n",
123
+ " svdpp = SVDpp()\n",
124
+ " svdpp.fit(trainset)\n",
125
+ "\n",
126
+ " items = list()\n",
127
+ " est = list()\n",
128
+ "\n",
129
+ " for i in range(len(opo)):\n",
130
+ " if i not in evaluated:\n",
131
+ " items.append(i)\n",
132
+ " est.append(svdpp.predict(userID, i).est)\n",
133
+ "\n",
134
+ " opo_n_one = items[est.index(sorted(est)[-1])]\n",
135
+ " opo_n_two = items[est.index(sorted(est)[-2])]\n",
136
+ " opo_n_three = items[est.index(sorted(est)[-3])]\n",
137
+ " opo_n_four = items[est.index(sorted(est)[-4])]\n",
138
+ "\n",
139
+ " return build_display_text(opo_n_one), build_display_text(opo_n_two), build_display_text(opo_n_three), build_display_text(opo_n_four)\n",
140
+ "\n",
141
+ "\n",
142
+ "with gr.Blocks() as demo:\n",
143
+ " with gr.Row():\n",
144
+ " one_opo = gr.Textbox(build_display_text(opo_n_one), label='Oportunidade 1')\n",
145
+ " two_opo = gr.Textbox(build_display_text(opo_n_two), label='Oportunidade 2')\n",
146
+ "\n",
147
+ " with gr.Row():\n",
148
+ " three_opo = gr.Textbox(build_display_text(opo_n_three), label='Oportunidade 3')\n",
149
+ " four_opo = gr.Textbox(build_display_text(opo_n_four), label='Oportunidade 4')\n",
150
+ "\n",
151
+ " with gr.Row():\n",
152
+ " option = gr.Radio(['1', '2', '3', '4'], label='Opção', value = '1')\n",
153
+ "\n",
154
+ " with gr.Row():\n",
155
+ " nota = gr.Slider(1,5,step=1,label=\"Nota 1\")\n",
156
+ "\n",
157
+ " with gr.Row():\n",
158
+ " confirm = gr.Button(\"Confirmar\")\n",
159
+ "\n",
160
+ " confirm.click(fn=predict_next,\n",
161
+ " inputs=[option, nota],\n",
162
+ " outputs=[one_opo, two_opo, three_opo, four_opo])\n",
163
+ "\n",
164
+ "if __name__ == \"__main__\":\n",
165
+ " demo.launch() "
166
+ ]
167
+ }
168
+ ],
169
+ "metadata": {
170
+ "hide_input": false,
171
+ "kernelspec": {
172
+ "display_name": "Python 3",
173
+ "language": "python",
174
+ "name": "python3"
175
+ },
176
+ "language_info": {
177
+ "codemirror_mode": {
178
+ "name": "ipython",
179
+ "version": 3
180
+ },
181
+ "file_extension": ".py",
182
+ "mimetype": "text/x-python",
183
+ "name": "python",
184
+ "nbconvert_exporter": "python",
185
+ "pygments_lexer": "ipython3",
186
+ "version": "3.10.4"
187
+ },
188
+ "varInspector": {
189
+ "cols": {
190
+ "lenName": 16,
191
+ "lenType": 16,
192
+ "lenVar": 40
193
+ },
194
+ "kernels_config": {
195
+ "python": {
196
+ "delete_cmd_postfix": "",
197
+ "delete_cmd_prefix": "del ",
198
+ "library": "var_list.py",
199
+ "varRefreshCmd": "print(var_dic_list())"
200
+ },
201
+ "r": {
202
+ "delete_cmd_postfix": ") ",
203
+ "delete_cmd_prefix": "rm(",
204
+ "library": "var_list.r",
205
+ "varRefreshCmd": "cat(var_dic_list()) "
206
+ }
207
+ },
208
+ "types_to_exclude": [
209
+ "module",
210
+ "function",
211
+ "builtin_function_or_method",
212
+ "instance",
213
+ "_Feature"
214
+ ],
215
+ "window_display": false
216
+ },
217
+ "vscode": {
218
+ "interpreter": {
219
+ "hash": "5fe3e6f0cdaab8afdc61c52912fda83f7c0a71baaea1897dd7498e2df01e69ec"
220
+ }
221
+ }
222
+ },
223
+ "nbformat": 4,
224
+ "nbformat_minor": 2
225
+ }
app.py CHANGED
@@ -3,6 +3,7 @@ import random
3
  import pandas as pd
4
 
5
  opo = pd.read_csv('oportunidades_results.csv', lineterminator='\n')
 
6
  simulation = pd.read_csv('simulation2.csv')
7
  userID = max(simulation['userID']) + 1
8
 
 
3
  import pandas as pd
4
 
5
  opo = pd.read_csv('oportunidades_results.csv', lineterminator='\n')
6
+ # opo = opo.iloc[np.where(opo['opo_brazil']=='Y')]
7
  simulation = pd.read_csv('simulation2.csv')
8
  userID = max(simulation['userID']) + 1
9