File size: 22,461 Bytes
cb1153d
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
3d58904
cb1153d
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
 
 
 
 
 
cb1153d
3d58904
 
 
 
 
cb1153d
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
3d58904
 
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d58904
cb1153d
 
 
3d58904
 
 
 
 
 
 
 
 
 
cb1153d
 
 
 
3d58904
cb1153d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import requests \n",
    "import datetime as dt\n",
    "import re\n",
    "import json\n",
    "from tqdm import tqdm\n",
    "import os\n",
    "\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### SciencesPoC extract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('extract_sciences_po.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>item_id</th>\n",
       "      <th>date_published</th>\n",
       "      <th>url</th>\n",
       "      <th>titre</th>\n",
       "      <th>description</th>\n",
       "      <th>type</th>\n",
       "      <th>author</th>\n",
       "      <th>section</th>\n",
       "      <th>subhead</th>\n",
       "      <th>premium</th>\n",
       "      <th>image_url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>I4OEKQ6MHRBP3LQVVYDDXW6T6U</td>\n",
       "      <td>2024-10-07 15:33:34</td>\n",
       "      <td>https://www.liberation.fr/societe/familles/a-m...</td>\n",
       "      <td>Centre de loisirs à Marseille : sept plaintes ...</td>\n",
       "      <td>Une enquête pour violences sur mineurs et diff...</td>\n",
       "      <td>article</td>\n",
       "      <td>Caroline Delabroy</td>\n",
       "      <td>Familles</td>\n",
       "      <td>Justice</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/E6tv2-_N7qhg...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>LVA4MZBQOBECNPZD323NV6O7K4</td>\n",
       "      <td>2024-10-09 14:53:55</td>\n",
       "      <td>https://www.liberation.fr/sports/jeux-olympiqu...</td>\n",
       "      <td>JO de Paris 2024 : pour la santé mentale des a...</td>\n",
       "      <td>Avec la libération de la parole sur la santé m...</td>\n",
       "      <td>article</td>\n",
       "      <td>Marie Thimonnier</td>\n",
       "      <td>Jeux olympiques et paralympiques</td>\n",
       "      <td>Ecriture</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/uAqwfrqGpk93...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>4FAEHUUZ5ZFAJKLFEV2LT5CBAQ</td>\n",
       "      <td>2024-10-10 15:49:02</td>\n",
       "      <td>https://www.liberation.fr/international/afriqu...</td>\n",
       "      <td>Au Burkina Faso, la conscription pour «punir d...</td>\n",
       "      <td>Au moins sept magistrats ont été réquisitionné...</td>\n",
       "      <td>article</td>\n",
       "      <td>Agnès Faivre</td>\n",
       "      <td>Afrique</td>\n",
       "      <td>Répression</td>\n",
       "      <td>False</td>\n",
       "      <td>https://www.liberation.fr/resizer/_A-QaSGPPcHu...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>4S4G6BKFRNER3LB22CLPAEWWKY</td>\n",
       "      <td>2024-10-11 15:28:25</td>\n",
       "      <td>https://www.liberation.fr/economie/social/ferm...</td>\n",
       "      <td>Fermeture de l’usine automobile MA France : «O...</td>\n",
       "      <td>Le 13 mai, l’entreprise sous-traitante de Stel...</td>\n",
       "      <td>article</td>\n",
       "      <td>Eléna Roney</td>\n",
       "      <td>Social</td>\n",
       "      <td>Lutte sociale</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/6hlzuHlwTJFQ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>ZAFHRNAHJVC6THXRSBMCB4A24I</td>\n",
       "      <td>2024-10-09 10:55:05</td>\n",
       "      <td>https://www.liberation.fr/lifestyle/design/pre...</td>\n",
       "      <td>Près de Rouen, jardiner dans «un jeu subtil av...</td>\n",
       "      <td>Imaginé par Patrick et Sylvie Quibel il y a tr...</td>\n",
       "      <td>article</td>\n",
       "      <td>['Florian Bardou, envoyé spécial à Rouen', 'ph...</td>\n",
       "      <td>Design</td>\n",
       "      <td>Reportage</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/goppJdUChU09...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>511</th>\n",
       "      <td>516</td>\n",
       "      <td>AOT254SA2VDIDNF4YW7XPLWJ5E</td>\n",
       "      <td>2024-10-12 15:46:00</td>\n",
       "      <td>https://www.liberation.fr/culture/musique/char...</td>\n",
       "      <td>Charlie Dalin : «Mon oncle était le chanteur d...</td>\n",
       "      <td>Charlie Dalin, skipper qui s’apprête à embarqu...</td>\n",
       "      <td>article</td>\n",
       "      <td>Alexis Bernier</td>\n",
       "      <td>Musique</td>\n",
       "      <td>Casque t'écoutes?</td>\n",
       "      <td>False</td>\n",
       "      <td>https://www.liberation.fr/resizer/Oum9ZxntR4pt...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>512</th>\n",
       "      <td>517</td>\n",
       "      <td>GUOUKHLPFZBK7GVR5XU7MXVD5A</td>\n",
       "      <td>2024-10-12 16:32:37</td>\n",
       "      <td>https://www.liberation.fr/societe/droits-des-f...</td>\n",
       "      <td>Violences sexuelles : à la recherche de «co-vi...</td>\n",
       "      <td>Avec un système de «matchs», le site Coabuse r...</td>\n",
       "      <td>article</td>\n",
       "      <td>Hélène Coutard</td>\n",
       "      <td>Droits des femmes</td>\n",
       "      <td>«Match»</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/F5PeR7sIedRA...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>513</th>\n",
       "      <td>518</td>\n",
       "      <td>5HT6C24ZBVDOBFXPLA4HNVOTT4</td>\n",
       "      <td>2024-10-12 16:33:57</td>\n",
       "      <td>https://www.liberation.fr/environnement/agricu...</td>\n",
       "      <td>«Plus ils habitent près des champs, plus les t...</td>\n",
       "      <td>Des traces de pesticides, dont certains interd...</td>\n",
       "      <td>article</td>\n",
       "      <td>[' LIBERATION', ' AFP']</td>\n",
       "      <td>Agriculture</td>\n",
       "      <td>Contamination</td>\n",
       "      <td>False</td>\n",
       "      <td>https://www.liberation.fr/resizer/VzMm-X_AuAhQ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>514</th>\n",
       "      <td>519</td>\n",
       "      <td>VLV6RSQ6U5E6XJ6AIRV26AEKO4</td>\n",
       "      <td>2024-10-12 16:43:33</td>\n",
       "      <td>https://www.liberation.fr/economie/annonces-de...</td>\n",
       "      <td>Annonces de la Chine contre la crise économiqu...</td>\n",
       "      <td>Face aux problèmes structurels de l’économie, ...</td>\n",
       "      <td>article</td>\n",
       "      <td>Arnaud Vaulerin</td>\n",
       "      <td>Economie</td>\n",
       "      <td>Analyse</td>\n",
       "      <td>True</td>\n",
       "      <td>https://www.liberation.fr/resizer/ZKreJUwCgQPk...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>515</th>\n",
       "      <td>520</td>\n",
       "      <td>FVCJ6DQ5HVDNDGC4F6F276NVFM</td>\n",
       "      <td>2024-10-12 16:53:20</td>\n",
       "      <td>https://www.liberation.fr/economie/medias/budg...</td>\n",
       "      <td>Budget 2025 : les radios associatives dénoncen...</td>\n",
       "      <td>Les radios associatives se sont alarmées vendr...</td>\n",
       "      <td>article</td>\n",
       "      <td>[' LIBERATION', ' AFP']</td>\n",
       "      <td>Médias</td>\n",
       "      <td>Mauvaises ondes</td>\n",
       "      <td>False</td>\n",
       "      <td>https://www.liberation.fr/resizer/KVsBqITY61oN...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>516 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Unnamed: 0                     item_id       date_published  \\\n",
       "0             0  I4OEKQ6MHRBP3LQVVYDDXW6T6U  2024-10-07 15:33:34   \n",
       "1             1  LVA4MZBQOBECNPZD323NV6O7K4  2024-10-09 14:53:55   \n",
       "2             2  4FAEHUUZ5ZFAJKLFEV2LT5CBAQ  2024-10-10 15:49:02   \n",
       "3             3  4S4G6BKFRNER3LB22CLPAEWWKY  2024-10-11 15:28:25   \n",
       "4             4  ZAFHRNAHJVC6THXRSBMCB4A24I  2024-10-09 10:55:05   \n",
       "..          ...                         ...                  ...   \n",
       "511         516  AOT254SA2VDIDNF4YW7XPLWJ5E  2024-10-12 15:46:00   \n",
       "512         517  GUOUKHLPFZBK7GVR5XU7MXVD5A  2024-10-12 16:32:37   \n",
       "513         518  5HT6C24ZBVDOBFXPLA4HNVOTT4  2024-10-12 16:33:57   \n",
       "514         519  VLV6RSQ6U5E6XJ6AIRV26AEKO4  2024-10-12 16:43:33   \n",
       "515         520  FVCJ6DQ5HVDNDGC4F6F276NVFM  2024-10-12 16:53:20   \n",
       "\n",
       "                                                   url  \\\n",
       "0    https://www.liberation.fr/societe/familles/a-m...   \n",
       "1    https://www.liberation.fr/sports/jeux-olympiqu...   \n",
       "2    https://www.liberation.fr/international/afriqu...   \n",
       "3    https://www.liberation.fr/economie/social/ferm...   \n",
       "4    https://www.liberation.fr/lifestyle/design/pre...   \n",
       "..                                                 ...   \n",
       "511  https://www.liberation.fr/culture/musique/char...   \n",
       "512  https://www.liberation.fr/societe/droits-des-f...   \n",
       "513  https://www.liberation.fr/environnement/agricu...   \n",
       "514  https://www.liberation.fr/economie/annonces-de...   \n",
       "515  https://www.liberation.fr/economie/medias/budg...   \n",
       "\n",
       "                                                 titre  \\\n",
       "0    Centre de loisirs à Marseille : sept plaintes ...   \n",
       "1    JO de Paris 2024 : pour la santé mentale des a...   \n",
       "2    Au Burkina Faso, la conscription pour «punir d...   \n",
       "3    Fermeture de l’usine automobile MA France : «O...   \n",
       "4    Près de Rouen, jardiner dans «un jeu subtil av...   \n",
       "..                                                 ...   \n",
       "511  Charlie Dalin : «Mon oncle était le chanteur d...   \n",
       "512  Violences sexuelles : à la recherche de «co-vi...   \n",
       "513  «Plus ils habitent près des champs, plus les t...   \n",
       "514  Annonces de la Chine contre la crise économiqu...   \n",
       "515  Budget 2025 : les radios associatives dénoncen...   \n",
       "\n",
       "                                           description     type  \\\n",
       "0    Une enquête pour violences sur mineurs et diff...  article   \n",
       "1    Avec la libération de la parole sur la santé m...  article   \n",
       "2    Au moins sept magistrats ont été réquisitionné...  article   \n",
       "3    Le 13 mai, l’entreprise sous-traitante de Stel...  article   \n",
       "4    Imaginé par Patrick et Sylvie Quibel il y a tr...  article   \n",
       "..                                                 ...      ...   \n",
       "511  Charlie Dalin, skipper qui s’apprête à embarqu...  article   \n",
       "512  Avec un système de «matchs», le site Coabuse r...  article   \n",
       "513  Des traces de pesticides, dont certains interd...  article   \n",
       "514  Face aux problèmes structurels de l’économie, ...  article   \n",
       "515  Les radios associatives se sont alarmées vendr...  article   \n",
       "\n",
       "                                                author  \\\n",
       "0                                    Caroline Delabroy   \n",
       "1                                     Marie Thimonnier   \n",
       "2                                         Agnès Faivre   \n",
       "3                                          Eléna Roney   \n",
       "4    ['Florian Bardou, envoyé spécial à Rouen', 'ph...   \n",
       "..                                                 ...   \n",
       "511                                     Alexis Bernier   \n",
       "512                                     Hélène Coutard   \n",
       "513                            [' LIBERATION', ' AFP']   \n",
       "514                                    Arnaud Vaulerin   \n",
       "515                            [' LIBERATION', ' AFP']   \n",
       "\n",
       "                              section            subhead  premium  \\\n",
       "0                            Familles            Justice     True   \n",
       "1    Jeux olympiques et paralympiques           Ecriture     True   \n",
       "2                             Afrique         Répression    False   \n",
       "3                              Social      Lutte sociale     True   \n",
       "4                              Design          Reportage     True   \n",
       "..                                ...                ...      ...   \n",
       "511                           Musique  Casque t'écoutes?    False   \n",
       "512                 Droits des femmes            «Match»     True   \n",
       "513                       Agriculture      Contamination    False   \n",
       "514                          Economie            Analyse     True   \n",
       "515                            Médias    Mauvaises ondes    False   \n",
       "\n",
       "                                             image_url  \n",
       "0    https://www.liberation.fr/resizer/E6tv2-_N7qhg...  \n",
       "1    https://www.liberation.fr/resizer/uAqwfrqGpk93...  \n",
       "2    https://www.liberation.fr/resizer/_A-QaSGPPcHu...  \n",
       "3    https://www.liberation.fr/resizer/6hlzuHlwTJFQ...  \n",
       "4    https://www.liberation.fr/resizer/goppJdUChU09...  \n",
       "..                                                 ...  \n",
       "511  https://www.liberation.fr/resizer/Oum9ZxntR4pt...  \n",
       "512  https://www.liberation.fr/resizer/F5PeR7sIedRA...  \n",
       "513  https://www.liberation.fr/resizer/VzMm-X_AuAhQ...  \n",
       "514  https://www.liberation.fr/resizer/ZKreJUwCgQPk...  \n",
       "515  https://www.liberation.fr/resizer/KVsBqITY61oN...  \n",
       "\n",
       "[516 rows x 12 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Connect to Deepseek endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_path = 'save'\n",
    "content_path = 'extract_sciences_po'\n",
    "\n",
    "\n",
    "def retrieve_classifications(name, mapping_prompt):\n",
    "\n",
    "\n",
    "    if os.path.exists(f\"{save_path}/output_{name}.txt\"):\n",
    "        with open(f\"{save_path}/output_{name}.txt\", 'r') as f : \n",
    "            out_dict = json.loads(f.read())\n",
    "        out_df = pd.DataFrame.from_dict(out_dict)\n",
    "        out = out_dict\n",
    "    else : \n",
    "        out_df = pd.DataFrame(columns = ['item_id', 'categorie_principale', 'categorie_secondaire'])\n",
    "        out = []\n",
    "\n",
    "    df_to_process = df.loc[~df.item_id.isin(out_df.item_id)]\n",
    "\n",
    "    if mapping_prompt[name]['client']=='deepseek':\n",
    "        client = OpenAI(api_key=os.environ[\"DEEPSEEK_API_KEY\"], base_url=\"https://api.deepseek.com\")\n",
    "    else:\n",
    "        client=OpenAI()\n",
    "\n",
    "    df_to_process = df.loc[~df.item_id.isin(out_df.item_id)]\n",
    "\n",
    "\n",
    "    with open(mapping_prompt[name]['path_prompt'], 'r') as f:\n",
    "        prompt = f.read()\n",
    "\n",
    "    with tqdm(total=df_to_process.shape[0]) as pbar:\n",
    "        for i, row in df_to_process.iterrows():\n",
    "            titre_brut = f\"{row.item_id}_\"+row.titre.lower().strip().replace(f\"\\xa0\", ' ').replace(' : ', ':').replace(' ', '_').replace('/', '')\n",
    "            \n",
    "            with open(f'{content_path}/{titre_brut}.txt', 'r') as f:\n",
    "                text = f.read()\n",
    "\n",
    "            messages = [{\"role\": \"system\", \"content\": prompt},\n",
    "                        {\"role\": \"user\", \"content\": text}]\n",
    "\n",
    "            response = client.chat.completions.create(\n",
    "                model=\"deepseek-chat\",\n",
    "                messages=messages,\n",
    "                response_format={\n",
    "                    'type': 'json_object'\n",
    "                }\n",
    "            )\n",
    "            try : \n",
    "                cat_json = json.loads(response.choices[0].message.content)\n",
    "\n",
    "                out.append({\n",
    "                    'item_id':row.item_id, \n",
    "                    'categorie_principale': cat_json['categorie_principale'],\n",
    "                    'categorie_secondaire': cat_json['categorie_secondaire'],\n",
    "                })\n",
    "                \n",
    "                with open(f'{save_path}/output_{name}.txt', 'w+') as f : \n",
    "                    f.write(json.dumps(out))\n",
    "\n",
    "            except Exception as e : \n",
    "                print(f'Error with article {row.item_id}')\n",
    "                pass\n",
    "\n",
    "            \n",
    "            pbar.update(1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'DEEPSEEK_API_KEY'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyError\u001b[39m                                  Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[5]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[33m'\u001b[39m\u001b[33mmapping_prompts.txt\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mr\u001b[39m\u001b[33m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f : \n\u001b[32m      2\u001b[39m     mapping = json.loads(f.read())\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[43mretrieve_classifications\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43msans_titre_1\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmapping\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 20\u001b[39m, in \u001b[36mretrieve_classifications\u001b[39m\u001b[34m(name, mapping_prompt)\u001b[39m\n\u001b[32m     17\u001b[39m df_to_process = df.loc[~df.item_id.isin(out_df.item_id)]\n\u001b[32m     19\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m mapping_prompt[name][\u001b[33m'\u001b[39m\u001b[33mclient\u001b[39m\u001b[33m'\u001b[39m]==\u001b[33m'\u001b[39m\u001b[33mdeepseek\u001b[39m\u001b[33m'\u001b[39m:\n\u001b[32m---> \u001b[39m\u001b[32m20\u001b[39m     client = OpenAI(api_key=\u001b[43mos\u001b[49m\u001b[43m.\u001b[49m\u001b[43menviron\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mDEEPSEEK_API_KEY\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m, base_url=\u001b[33m\"\u001b[39m\u001b[33mhttps://api.deepseek.com\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m     21\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m     22\u001b[39m     client=OpenAI()\n",
      "\u001b[36mFile \u001b[39m\u001b[32m<frozen os>:679\u001b[39m, in \u001b[36m__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n",
      "\u001b[31mKeyError\u001b[39m: 'DEEPSEEK_API_KEY'"
     ]
    }
   ],
   "source": [
    "with open('mapping_prompts.txt', 'r') as f : \n",
    "    mapping = json.loads(f.read())\n",
    "retrieve_classifications('sans_titre_1', mapping)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Ajouter images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}