Spaces:
Sleeping
Sleeping
Finished implementation of AI reviews
Browse files- notebook/Archive/RECO_04_old.ipynb +29 -26
- notebook/RECO_04.ipynb +160 -67
- pages/2_🎥_Animes.py +4 -1
- utils/common.py +119 -20
notebook/Archive/RECO_04_old.ipynb
CHANGED
|
@@ -55,7 +55,7 @@
|
|
| 55 |
},
|
| 56 |
{
|
| 57 |
"cell_type": "code",
|
| 58 |
-
"execution_count":
|
| 59 |
"id": "8494eb94",
|
| 60 |
"metadata": {},
|
| 61 |
"outputs": [],
|
|
@@ -120,7 +120,7 @@
|
|
| 120 |
},
|
| 121 |
{
|
| 122 |
"cell_type": "code",
|
| 123 |
-
"execution_count":
|
| 124 |
"id": "a571db87",
|
| 125 |
"metadata": {},
|
| 126 |
"outputs": [],
|
|
@@ -131,7 +131,7 @@
|
|
| 131 |
},
|
| 132 |
{
|
| 133 |
"cell_type": "code",
|
| 134 |
-
"execution_count":
|
| 135 |
"id": "267e5fb9",
|
| 136 |
"metadata": {},
|
| 137 |
"outputs": [
|
|
@@ -250,7 +250,7 @@
|
|
| 250 |
"4 https://myanimelist.net/reviews.php?id=291149 "
|
| 251 |
]
|
| 252 |
},
|
| 253 |
-
"execution_count":
|
| 254 |
"metadata": {},
|
| 255 |
"output_type": "execute_result"
|
| 256 |
}
|
|
@@ -321,7 +321,7 @@
|
|
| 321 |
},
|
| 322 |
{
|
| 323 |
"cell_type": "code",
|
| 324 |
-
"execution_count":
|
| 325 |
"id": "a5ef2896",
|
| 326 |
"metadata": {},
|
| 327 |
"outputs": [],
|
|
@@ -332,14 +332,10 @@
|
|
| 332 |
"\n",
|
| 333 |
"#nlp.max_length = 1500000\n",
|
| 334 |
"\n",
|
| 335 |
-
"def
|
| 336 |
-
" text = re.sub(\"[\\n\\r]\", \"\", text)\n",
|
| 337 |
-
" text = re.sub(\"[^A-Za-
|
| 338 |
-
"
|
| 339 |
-
" text_splited = re.findall('.{512}', text)\n",
|
| 340 |
-
" text_splited.append(text[512 * len(text_splited):])\n",
|
| 341 |
-
"\n",
|
| 342 |
-
" return text_splited\n"
|
| 343 |
]
|
| 344 |
},
|
| 345 |
{
|
|
@@ -389,27 +385,34 @@
|
|
| 389 |
},
|
| 390 |
{
|
| 391 |
"cell_type": "code",
|
| 392 |
-
"execution_count":
|
| 393 |
"id": "ae96b303",
|
| 394 |
"metadata": {},
|
| 395 |
"outputs": [
|
| 396 |
{
|
| 397 |
-
"
|
| 398 |
-
"
|
| 399 |
-
"
|
| 400 |
-
|
| 401 |
-
"
|
| 402 |
-
"
|
| 403 |
-
"
|
| 404 |
-
"\n",
|
| 405 |
-
"
|
| 406 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
]
|
| 408 |
}
|
| 409 |
],
|
| 410 |
"source": [
|
| 411 |
-
"
|
| 412 |
-
"
|
|
|
|
| 413 |
"\n"
|
| 414 |
]
|
| 415 |
},
|
|
|
|
| 55 |
},
|
| 56 |
{
|
| 57 |
"cell_type": "code",
|
| 58 |
+
"execution_count": 7,
|
| 59 |
"id": "8494eb94",
|
| 60 |
"metadata": {},
|
| 61 |
"outputs": [],
|
|
|
|
| 120 |
},
|
| 121 |
{
|
| 122 |
"cell_type": "code",
|
| 123 |
+
"execution_count": 2,
|
| 124 |
"id": "a571db87",
|
| 125 |
"metadata": {},
|
| 126 |
"outputs": [],
|
|
|
|
| 131 |
},
|
| 132 |
{
|
| 133 |
"cell_type": "code",
|
| 134 |
+
"execution_count": 3,
|
| 135 |
"id": "267e5fb9",
|
| 136 |
"metadata": {},
|
| 137 |
"outputs": [
|
|
|
|
| 250 |
"4 https://myanimelist.net/reviews.php?id=291149 "
|
| 251 |
]
|
| 252 |
},
|
| 253 |
+
"execution_count": 3,
|
| 254 |
"metadata": {},
|
| 255 |
"output_type": "execute_result"
|
| 256 |
}
|
|
|
|
| 321 |
},
|
| 322 |
{
|
| 323 |
"cell_type": "code",
|
| 324 |
+
"execution_count": 9,
|
| 325 |
"id": "a5ef2896",
|
| 326 |
"metadata": {},
|
| 327 |
"outputs": [],
|
|
|
|
| 332 |
"\n",
|
| 333 |
"#nlp.max_length = 1500000\n",
|
| 334 |
"\n",
|
| 335 |
+
"def clean_text(text):\n",
|
| 336 |
+
" text = re.sub(\"[\\n\\r]\", \" \", text)\n",
|
| 337 |
+
" text = re.sub(\"[^A-Za-z0-9]+\", \" \", text)\n",
|
| 338 |
+
" return text"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
]
|
| 340 |
},
|
| 341 |
{
|
|
|
|
| 385 |
},
|
| 386 |
{
|
| 387 |
"cell_type": "code",
|
| 388 |
+
"execution_count": 10,
|
| 389 |
"id": "ae96b303",
|
| 390 |
"metadata": {},
|
| 391 |
"outputs": [
|
| 392 |
{
|
| 393 |
+
"ename": "IndentationError",
|
| 394 |
+
"evalue": "unexpected indent (<unknown>, line 9)",
|
| 395 |
+
"output_type": "error",
|
| 396 |
+
"traceback": [
|
| 397 |
+
"Traceback \u001b[36m(most recent call last)\u001b[39m:\n",
|
| 398 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/IPython/core/interactiveshell.py:3670\u001b[39m in \u001b[95mrun_code\u001b[39m\n exec(code_obj, self.user_global_ns, self.user_ns)\n",
|
| 399 |
+
" Cell \u001b[92mIn[10]\u001b[39m\u001b[92m, line 2\u001b[39m\n reviews_for_llm['text'] = reviews_for_llm[\"text\"].apply(ast.literal_eval)\n",
|
| 400 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/pandas/core/series.py:4924\u001b[39m in \u001b[95mapply\u001b[39m\n ).apply()\n",
|
| 401 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/pandas/core/apply.py:1427\u001b[39m in \u001b[95mapply\u001b[39m\n return self.apply_standard()\n",
|
| 402 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/pandas/core/apply.py:1507\u001b[39m in \u001b[95mapply_standard\u001b[39m\n mapped = obj._map_values(\n",
|
| 403 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/pandas/core/base.py:921\u001b[39m in \u001b[95m_map_values\u001b[39m\n return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)\n",
|
| 404 |
+
" File \u001b[92m~/Jedha_Bootcamp/Projet_DSFS/DataScience/lib/python3.12/site-packages/pandas/core/algorithms.py:1743\u001b[39m in \u001b[95mmap_array\u001b[39m\n return lib.map_infer(values, mapper, convert=convert)\n",
|
| 405 |
+
" File \u001b[92mlib.pyx:2972\u001b[39m in \u001b[95mpandas._libs.lib.map_infer\u001b[39m\n",
|
| 406 |
+
" File \u001b[92m/usr/lib/python3.12/ast.py:66\u001b[39m in \u001b[95mliteral_eval\u001b[39m\n node_or_string = parse(node_or_string.lstrip(\" \\t\"), mode='eval')\n",
|
| 407 |
+
"\u001b[36m \u001b[39m\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/ast.py:52\u001b[39m\u001b[36m in \u001b[39m\u001b[35mparse\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn compile(source, filename, mode, flags,\u001b[39m\n",
|
| 408 |
+
" \u001b[36mFile \u001b[39m\u001b[32m<unknown>:9\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mmore pics\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unexpected indent\n"
|
| 409 |
]
|
| 410 |
}
|
| 411 |
],
|
| 412 |
"source": [
|
| 413 |
+
"reviews_for_llm = df_reviews.drop(columns=[\"uid\",\"profile\",\"score\",\"scores\",\"link\"])\n",
|
| 414 |
+
"reviews_for_llm['text'] = reviews_for_llm[\"text\"].apply(ast.literal_eval)\n",
|
| 415 |
+
"reviews_for_llm['text'].progress_apply(clean_text)\n",
|
| 416 |
"\n"
|
| 417 |
]
|
| 418 |
},
|
notebook/RECO_04.ipynb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
-
"execution_count":
|
| 6 |
"id": "10c270a4",
|
| 7 |
"metadata": {},
|
| 8 |
"outputs": [],
|
|
@@ -20,7 +20,7 @@
|
|
| 20 |
},
|
| 21 |
{
|
| 22 |
"cell_type": "code",
|
| 23 |
-
"execution_count":
|
| 24 |
"id": "8c49c949",
|
| 25 |
"metadata": {},
|
| 26 |
"outputs": [],
|
|
@@ -30,7 +30,27 @@
|
|
| 30 |
},
|
| 31 |
{
|
| 32 |
"cell_type": "code",
|
| 33 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"id": "17ef934a",
|
| 35 |
"metadata": {},
|
| 36 |
"outputs": [
|
|
@@ -55,88 +75,56 @@
|
|
| 55 |
" <thead>\n",
|
| 56 |
" <tr style=\"text-align: right;\">\n",
|
| 57 |
" <th></th>\n",
|
| 58 |
-
" <th>profile</th>\n",
|
| 59 |
" <th>anime_uid</th>\n",
|
| 60 |
" <th>text</th>\n",
|
| 61 |
-
" <th>score</th>\n",
|
| 62 |
-
" <th>classifications</th>\n",
|
| 63 |
" </tr>\n",
|
| 64 |
" </thead>\n",
|
| 65 |
" <tbody>\n",
|
| 66 |
" <tr>\n",
|
| 67 |
" <th>0</th>\n",
|
| 68 |
-
" <td>DesolatePsyche</td>\n",
|
| 69 |
" <td>34096</td>\n",
|
| 70 |
" <td>[' more pics Overall 8 Story 8 Animation 8 Sou...</td>\n",
|
| 71 |
-
" <td>8</td>\n",
|
| 72 |
-
" <td>[{'label': 'POSITIVE', 'score': 0.931263029575...</td>\n",
|
| 73 |
" </tr>\n",
|
| 74 |
" <tr>\n",
|
| 75 |
" <th>1</th>\n",
|
| 76 |
-
" <td>baekbeans</td>\n",
|
| 77 |
" <td>34599</td>\n",
|
| 78 |
" <td>[' more pics Overall 10 Story 10 Animation 10 ...</td>\n",
|
| 79 |
-
" <td>10</td>\n",
|
| 80 |
-
" <td>[{'label': 'POSITIVE', 'score': 0.882984995841...</td>\n",
|
| 81 |
" </tr>\n",
|
| 82 |
" <tr>\n",
|
| 83 |
" <th>2</th>\n",
|
| 84 |
-
" <td>skrn</td>\n",
|
| 85 |
" <td>28891</td>\n",
|
| 86 |
" <td>[' more pics Overall 7 Story 7 Animation 9 Sou...</td>\n",
|
| 87 |
-
" <td>7</td>\n",
|
| 88 |
-
" <td>[{'label': 'POSITIVE', 'score': 0.972522079944...</td>\n",
|
| 89 |
" </tr>\n",
|
| 90 |
" <tr>\n",
|
| 91 |
" <th>3</th>\n",
|
| 92 |
-
" <td>edgewalker00</td>\n",
|
| 93 |
" <td>2904</td>\n",
|
| 94 |
" <td>[' more pics Overall 9 Story 9 Animation 9 Sou...</td>\n",
|
| 95 |
-
" <td>9</td>\n",
|
| 96 |
-
" <td>[{'label': 'NEGATIVE', 'score': 0.999156832695...</td>\n",
|
| 97 |
" </tr>\n",
|
| 98 |
" <tr>\n",
|
| 99 |
" <th>4</th>\n",
|
| 100 |
-
" <td>aManOfCulture99</td>\n",
|
| 101 |
" <td>4181</td>\n",
|
| 102 |
" <td>[' more pics Overall 10 Story 10 Animation 8 S...</td>\n",
|
| 103 |
-
" <td>10</td>\n",
|
| 104 |
-
" <td>[{'label': 'POSITIVE', 'score': 0.998817861080...</td>\n",
|
| 105 |
" </tr>\n",
|
| 106 |
" </tbody>\n",
|
| 107 |
"</table>\n",
|
| 108 |
"</div>"
|
| 109 |
],
|
| 110 |
"text/plain": [
|
| 111 |
-
"
|
| 112 |
-
"0
|
| 113 |
-
"1
|
| 114 |
-
"2
|
| 115 |
-
"3
|
| 116 |
-
"4
|
| 117 |
-
"\n",
|
| 118 |
-
" text score \\\n",
|
| 119 |
-
"0 [' more pics Overall 8 Story 8 Animation 8 Sou... 8 \n",
|
| 120 |
-
"1 [' more pics Overall 10 Story 10 Animation 10 ... 10 \n",
|
| 121 |
-
"2 [' more pics Overall 7 Story 7 Animation 9 Sou... 7 \n",
|
| 122 |
-
"3 [' more pics Overall 9 Story 9 Animation 9 Sou... 9 \n",
|
| 123 |
-
"4 [' more pics Overall 10 Story 10 Animation 8 S... 10 \n",
|
| 124 |
-
"\n",
|
| 125 |
-
" classifications \n",
|
| 126 |
-
"0 [{'label': 'POSITIVE', 'score': 0.931263029575... \n",
|
| 127 |
-
"1 [{'label': 'POSITIVE', 'score': 0.882984995841... \n",
|
| 128 |
-
"2 [{'label': 'POSITIVE', 'score': 0.972522079944... \n",
|
| 129 |
-
"3 [{'label': 'NEGATIVE', 'score': 0.999156832695... \n",
|
| 130 |
-
"4 [{'label': 'POSITIVE', 'score': 0.998817861080... "
|
| 131 |
]
|
| 132 |
},
|
| 133 |
-
"execution_count":
|
| 134 |
"metadata": {},
|
| 135 |
"output_type": "execute_result"
|
| 136 |
}
|
| 137 |
],
|
| 138 |
"source": [
|
| 139 |
-
"
|
| 140 |
]
|
| 141 |
},
|
| 142 |
{
|
|
@@ -262,7 +250,7 @@
|
|
| 262 |
},
|
| 263 |
{
|
| 264 |
"cell_type": "code",
|
| 265 |
-
"execution_count":
|
| 266 |
"id": "e7aef316",
|
| 267 |
"metadata": {},
|
| 268 |
"outputs": [],
|
|
@@ -307,7 +295,7 @@
|
|
| 307 |
},
|
| 308 |
{
|
| 309 |
"cell_type": "code",
|
| 310 |
-
"execution_count":
|
| 311 |
"id": "28fa6137",
|
| 312 |
"metadata": {},
|
| 313 |
"outputs": [
|
|
@@ -315,17 +303,17 @@
|
|
| 315 |
"name": "stdout",
|
| 316 |
"output_type": "stream",
|
| 317 |
"text": [
|
| 318 |
-
"env: MISTRAL_API_KEY=
|
| 319 |
]
|
| 320 |
}
|
| 321 |
],
|
| 322 |
"source": [
|
| 323 |
-
"%env MISTRAL_API_KEY=
|
| 324 |
]
|
| 325 |
},
|
| 326 |
{
|
| 327 |
"cell_type": "code",
|
| 328 |
-
"execution_count":
|
| 329 |
"id": "01c057cc",
|
| 330 |
"metadata": {},
|
| 331 |
"outputs": [],
|
|
@@ -335,7 +323,7 @@
|
|
| 335 |
},
|
| 336 |
{
|
| 337 |
"cell_type": "code",
|
| 338 |
-
"execution_count":
|
| 339 |
"id": "76d91d6e",
|
| 340 |
"metadata": {},
|
| 341 |
"outputs": [],
|
|
@@ -345,17 +333,17 @@
|
|
| 345 |
},
|
| 346 |
{
|
| 347 |
"cell_type": "code",
|
| 348 |
-
"execution_count":
|
| 349 |
"id": "282c7bf0",
|
| 350 |
"metadata": {},
|
| 351 |
"outputs": [],
|
| 352 |
"source": [
|
| 353 |
-
"test_reviews = reviews[reviews[\"anime_uid\"] ==
|
| 354 |
]
|
| 355 |
},
|
| 356 |
{
|
| 357 |
"cell_type": "code",
|
| 358 |
-
"execution_count":
|
| 359 |
"id": "178adce7",
|
| 360 |
"metadata": {},
|
| 361 |
"outputs": [
|
|
@@ -388,17 +376,52 @@
|
|
| 388 |
" </tr>\n",
|
| 389 |
" </thead>\n",
|
| 390 |
" <tbody>\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
" </tbody>\n",
|
| 392 |
"</table>\n",
|
| 393 |
"</div>"
|
| 394 |
],
|
| 395 |
"text/plain": [
|
| 396 |
-
"
|
| 397 |
-
"
|
| 398 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
]
|
| 400 |
},
|
| 401 |
-
"execution_count":
|
| 402 |
"metadata": {},
|
| 403 |
"output_type": "execute_result"
|
| 404 |
}
|
|
@@ -409,7 +432,28 @@
|
|
| 409 |
},
|
| 410 |
{
|
| 411 |
"cell_type": "code",
|
| 412 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
"id": "b58758ea",
|
| 414 |
"metadata": {},
|
| 415 |
"outputs": [],
|
|
@@ -419,19 +463,23 @@
|
|
| 419 |
},
|
| 420 |
{
|
| 421 |
"cell_type": "code",
|
| 422 |
-
"execution_count":
|
| 423 |
"id": "3c1dafd1",
|
| 424 |
"metadata": {},
|
| 425 |
"outputs": [
|
| 426 |
{
|
| 427 |
-
"
|
| 428 |
-
"
|
| 429 |
-
"
|
| 430 |
-
|
| 431 |
-
"\
|
| 432 |
-
"
|
| 433 |
-
"\
|
| 434 |
-
"\
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
]
|
| 436 |
}
|
| 437 |
],
|
|
@@ -447,8 +495,53 @@
|
|
| 447 |
" else:\n",
|
| 448 |
" print(input_positive_clean)\n",
|
| 449 |
" print(input_negative_clean)\n",
|
| 450 |
-
" print(f'Response generated by Mistral AI, based on {len(test_reviews)} reviews')\n"
|
|
|
|
| 451 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
}
|
| 453 |
],
|
| 454 |
"metadata": {
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
+
"execution_count": 30,
|
| 6 |
"id": "10c270a4",
|
| 7 |
"metadata": {},
|
| 8 |
"outputs": [],
|
|
|
|
| 20 |
},
|
| 21 |
{
|
| 22 |
"cell_type": "code",
|
| 23 |
+
"execution_count": 49,
|
| 24 |
"id": "8c49c949",
|
| 25 |
"metadata": {},
|
| 26 |
"outputs": [],
|
|
|
|
| 30 |
},
|
| 31 |
{
|
| 32 |
"cell_type": "code",
|
| 33 |
+
"execution_count": 50,
|
| 34 |
+
"id": "eb79d2f0",
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"outputs": [],
|
| 37 |
+
"source": [
|
| 38 |
+
"reviews_for_llm = reviews.drop(columns=[\"profile\",\"score\",\"classifications\"])"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"execution_count": 51,
|
| 44 |
+
"id": "43021aae",
|
| 45 |
+
"metadata": {},
|
| 46 |
+
"outputs": [],
|
| 47 |
+
"source": [
|
| 48 |
+
"reviews_for_llm.to_csv(\"reviews_for_llm.csv\",index=False)"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 52,
|
| 54 |
"id": "17ef934a",
|
| 55 |
"metadata": {},
|
| 56 |
"outputs": [
|
|
|
|
| 75 |
" <thead>\n",
|
| 76 |
" <tr style=\"text-align: right;\">\n",
|
| 77 |
" <th></th>\n",
|
|
|
|
| 78 |
" <th>anime_uid</th>\n",
|
| 79 |
" <th>text</th>\n",
|
|
|
|
|
|
|
| 80 |
" </tr>\n",
|
| 81 |
" </thead>\n",
|
| 82 |
" <tbody>\n",
|
| 83 |
" <tr>\n",
|
| 84 |
" <th>0</th>\n",
|
|
|
|
| 85 |
" <td>34096</td>\n",
|
| 86 |
" <td>[' more pics Overall 8 Story 8 Animation 8 Sou...</td>\n",
|
|
|
|
|
|
|
| 87 |
" </tr>\n",
|
| 88 |
" <tr>\n",
|
| 89 |
" <th>1</th>\n",
|
|
|
|
| 90 |
" <td>34599</td>\n",
|
| 91 |
" <td>[' more pics Overall 10 Story 10 Animation 10 ...</td>\n",
|
|
|
|
|
|
|
| 92 |
" </tr>\n",
|
| 93 |
" <tr>\n",
|
| 94 |
" <th>2</th>\n",
|
|
|
|
| 95 |
" <td>28891</td>\n",
|
| 96 |
" <td>[' more pics Overall 7 Story 7 Animation 9 Sou...</td>\n",
|
|
|
|
|
|
|
| 97 |
" </tr>\n",
|
| 98 |
" <tr>\n",
|
| 99 |
" <th>3</th>\n",
|
|
|
|
| 100 |
" <td>2904</td>\n",
|
| 101 |
" <td>[' more pics Overall 9 Story 9 Animation 9 Sou...</td>\n",
|
|
|
|
|
|
|
| 102 |
" </tr>\n",
|
| 103 |
" <tr>\n",
|
| 104 |
" <th>4</th>\n",
|
|
|
|
| 105 |
" <td>4181</td>\n",
|
| 106 |
" <td>[' more pics Overall 10 Story 10 Animation 8 S...</td>\n",
|
|
|
|
|
|
|
| 107 |
" </tr>\n",
|
| 108 |
" </tbody>\n",
|
| 109 |
"</table>\n",
|
| 110 |
"</div>"
|
| 111 |
],
|
| 112 |
"text/plain": [
|
| 113 |
+
" anime_uid text\n",
|
| 114 |
+
"0 34096 [' more pics Overall 8 Story 8 Animation 8 Sou...\n",
|
| 115 |
+
"1 34599 [' more pics Overall 10 Story 10 Animation 10 ...\n",
|
| 116 |
+
"2 28891 [' more pics Overall 7 Story 7 Animation 9 Sou...\n",
|
| 117 |
+
"3 2904 [' more pics Overall 9 Story 9 Animation 9 Sou...\n",
|
| 118 |
+
"4 4181 [' more pics Overall 10 Story 10 Animation 8 S..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
]
|
| 120 |
},
|
| 121 |
+
"execution_count": 52,
|
| 122 |
"metadata": {},
|
| 123 |
"output_type": "execute_result"
|
| 124 |
}
|
| 125 |
],
|
| 126 |
"source": [
|
| 127 |
+
"reviews_for_llm.head()"
|
| 128 |
]
|
| 129 |
},
|
| 130 |
{
|
|
|
|
| 250 |
},
|
| 251 |
{
|
| 252 |
"cell_type": "code",
|
| 253 |
+
"execution_count": 33,
|
| 254 |
"id": "e7aef316",
|
| 255 |
"metadata": {},
|
| 256 |
"outputs": [],
|
|
|
|
| 295 |
},
|
| 296 |
{
|
| 297 |
"cell_type": "code",
|
| 298 |
+
"execution_count": 62,
|
| 299 |
"id": "28fa6137",
|
| 300 |
"metadata": {},
|
| 301 |
"outputs": [
|
|
|
|
| 303 |
"name": "stdout",
|
| 304 |
"output_type": "stream",
|
| 305 |
"text": [
|
| 306 |
+
"env: MISTRAL_API_KEY=\n"
|
| 307 |
]
|
| 308 |
}
|
| 309 |
],
|
| 310 |
"source": [
|
| 311 |
+
"%env MISTRAL_API_KEY="
|
| 312 |
]
|
| 313 |
},
|
| 314 |
{
|
| 315 |
"cell_type": "code",
|
| 316 |
+
"execution_count": 35,
|
| 317 |
"id": "01c057cc",
|
| 318 |
"metadata": {},
|
| 319 |
"outputs": [],
|
|
|
|
| 323 |
},
|
| 324 |
{
|
| 325 |
"cell_type": "code",
|
| 326 |
+
"execution_count": 36,
|
| 327 |
"id": "76d91d6e",
|
| 328 |
"metadata": {},
|
| 329 |
"outputs": [],
|
|
|
|
| 333 |
},
|
| 334 |
{
|
| 335 |
"cell_type": "code",
|
| 336 |
+
"execution_count": 56,
|
| 337 |
"id": "282c7bf0",
|
| 338 |
"metadata": {},
|
| 339 |
"outputs": [],
|
| 340 |
"source": [
|
| 341 |
+
"test_reviews = reviews[reviews[\"anime_uid\"] == 20707]"
|
| 342 |
]
|
| 343 |
},
|
| 344 |
{
|
| 345 |
"cell_type": "code",
|
| 346 |
+
"execution_count": 42,
|
| 347 |
"id": "178adce7",
|
| 348 |
"metadata": {},
|
| 349 |
"outputs": [
|
|
|
|
| 376 |
" </tr>\n",
|
| 377 |
" </thead>\n",
|
| 378 |
" <tbody>\n",
|
| 379 |
+
" <tr>\n",
|
| 380 |
+
" <th>60319</th>\n",
|
| 381 |
+
" <td>OnlyHentaiHD</td>\n",
|
| 382 |
+
" <td>11997</td>\n",
|
| 383 |
+
" <td>[' more pics Overall 8 Story 6 Animation 8 Sou...</td>\n",
|
| 384 |
+
" <td>8</td>\n",
|
| 385 |
+
" <td>[{'label': 'POSITIVE', 'score': 0.988199710845...</td>\n",
|
| 386 |
+
" </tr>\n",
|
| 387 |
+
" <tr>\n",
|
| 388 |
+
" <th>60665</th>\n",
|
| 389 |
+
" <td>mabiniss1</td>\n",
|
| 390 |
+
" <td>11997</td>\n",
|
| 391 |
+
" <td>[' more pics Overall 6 Story 5 Animation 6 Sou...</td>\n",
|
| 392 |
+
" <td>6</td>\n",
|
| 393 |
+
" <td>[{'label': 'NEGATIVE', 'score': 0.998758196830...</td>\n",
|
| 394 |
+
" </tr>\n",
|
| 395 |
+
" <tr>\n",
|
| 396 |
+
" <th>60666</th>\n",
|
| 397 |
+
" <td>I_Love_Kissxsis</td>\n",
|
| 398 |
+
" <td>11997</td>\n",
|
| 399 |
+
" <td>[' more pics Overall 7 Story 7 Animation 9 Sou...</td>\n",
|
| 400 |
+
" <td>7</td>\n",
|
| 401 |
+
" <td>[{'label': 'POSITIVE', 'score': 0.995327234268...</td>\n",
|
| 402 |
+
" </tr>\n",
|
| 403 |
" </tbody>\n",
|
| 404 |
"</table>\n",
|
| 405 |
"</div>"
|
| 406 |
],
|
| 407 |
"text/plain": [
|
| 408 |
+
" profile anime_uid \\\n",
|
| 409 |
+
"60319 OnlyHentaiHD 11997 \n",
|
| 410 |
+
"60665 mabiniss1 11997 \n",
|
| 411 |
+
"60666 I_Love_Kissxsis 11997 \n",
|
| 412 |
+
"\n",
|
| 413 |
+
" text score \\\n",
|
| 414 |
+
"60319 [' more pics Overall 8 Story 6 Animation 8 Sou... 8 \n",
|
| 415 |
+
"60665 [' more pics Overall 6 Story 5 Animation 6 Sou... 6 \n",
|
| 416 |
+
"60666 [' more pics Overall 7 Story 7 Animation 9 Sou... 7 \n",
|
| 417 |
+
"\n",
|
| 418 |
+
" classifications \n",
|
| 419 |
+
"60319 [{'label': 'POSITIVE', 'score': 0.988199710845... \n",
|
| 420 |
+
"60665 [{'label': 'NEGATIVE', 'score': 0.998758196830... \n",
|
| 421 |
+
"60666 [{'label': 'POSITIVE', 'score': 0.995327234268... "
|
| 422 |
]
|
| 423 |
},
|
| 424 |
+
"execution_count": 42,
|
| 425 |
"metadata": {},
|
| 426 |
"output_type": "execute_result"
|
| 427 |
}
|
|
|
|
| 432 |
},
|
| 433 |
{
|
| 434 |
"cell_type": "code",
|
| 435 |
+
"execution_count": 53,
|
| 436 |
+
"id": "0fb08501",
|
| 437 |
+
"metadata": {},
|
| 438 |
+
"outputs": [
|
| 439 |
+
{
|
| 440 |
+
"data": {
|
| 441 |
+
"text/plain": [
|
| 442 |
+
"pandas.core.series.Series"
|
| 443 |
+
]
|
| 444 |
+
},
|
| 445 |
+
"execution_count": 53,
|
| 446 |
+
"metadata": {},
|
| 447 |
+
"output_type": "execute_result"
|
| 448 |
+
}
|
| 449 |
+
],
|
| 450 |
+
"source": [
|
| 451 |
+
"type(test_reviews['text'])"
|
| 452 |
+
]
|
| 453 |
+
},
|
| 454 |
+
{
|
| 455 |
+
"cell_type": "code",
|
| 456 |
+
"execution_count": 57,
|
| 457 |
"id": "b58758ea",
|
| 458 |
"metadata": {},
|
| 459 |
"outputs": [],
|
|
|
|
| 463 |
},
|
| 464 |
{
|
| 465 |
"cell_type": "code",
|
| 466 |
+
"execution_count": 59,
|
| 467 |
"id": "3c1dafd1",
|
| 468 |
"metadata": {},
|
| 469 |
"outputs": [
|
| 470 |
{
|
| 471 |
+
"name": "stdout",
|
| 472 |
+
"output_type": "stream",
|
| 473 |
+
"text": [
|
| 474 |
+
"Users liked: the animation and sound, which were highlighted as strong points.\n",
|
| 475 |
+
"Users disliked: the story, which was criticized for being weak or unengaging.\n",
|
| 476 |
+
"Response generated by Mistral AI, based on 3 reviews\n",
|
| 477 |
+
"```json\n",
|
| 478 |
+
"{\n",
|
| 479 |
+
" \"positive\": \"Users liked: the animation and sound, which were highlighted as strong points.\",\n",
|
| 480 |
+
" \"negative\": \"Users disliked: the story, which was criticized for being weak or unengaging.\"\n",
|
| 481 |
+
"}\n",
|
| 482 |
+
"```\n"
|
| 483 |
]
|
| 484 |
}
|
| 485 |
],
|
|
|
|
| 495 |
" else:\n",
|
| 496 |
" print(input_positive_clean)\n",
|
| 497 |
" print(input_negative_clean)\n",
|
| 498 |
+
" print(f'Response generated by Mistral AI, based on {len(test_reviews)} reviews')\n",
|
| 499 |
+
" print(response.content)"
|
| 500 |
]
|
| 501 |
+
},
|
| 502 |
+
{
|
| 503 |
+
"cell_type": "code",
|
| 504 |
+
"execution_count": 60,
|
| 505 |
+
"id": "4dfa5995",
|
| 506 |
+
"metadata": {},
|
| 507 |
+
"outputs": [],
|
| 508 |
+
"source": [
|
| 509 |
+
" output_positive = parser.parse(response.content).positive\n",
|
| 510 |
+
" output_negative = parser.parse(response.content).negative\n",
|
| 511 |
+
" ai_notice = f'Response generated by Mistral AI, based on {len(input)} reviews'\n",
|
| 512 |
+
" output = [output_positive, output_negative, ai_notice]"
|
| 513 |
+
]
|
| 514 |
+
},
|
| 515 |
+
{
|
| 516 |
+
"cell_type": "code",
|
| 517 |
+
"execution_count": 61,
|
| 518 |
+
"id": "16b51a25",
|
| 519 |
+
"metadata": {},
|
| 520 |
+
"outputs": [
|
| 521 |
+
{
|
| 522 |
+
"data": {
|
| 523 |
+
"text/plain": [
|
| 524 |
+
"['Users liked: the animation and sound, which were highlighted as strong points.',\n",
|
| 525 |
+
" 'Users disliked: the story, which was criticized for being weak or unengaging.',\n",
|
| 526 |
+
" 'Response generated by Mistral AI, based on 3 reviews']"
|
| 527 |
+
]
|
| 528 |
+
},
|
| 529 |
+
"execution_count": 61,
|
| 530 |
+
"metadata": {},
|
| 531 |
+
"output_type": "execute_result"
|
| 532 |
+
}
|
| 533 |
+
],
|
| 534 |
+
"source": [
|
| 535 |
+
"output"
|
| 536 |
+
]
|
| 537 |
+
},
|
| 538 |
+
{
|
| 539 |
+
"cell_type": "code",
|
| 540 |
+
"execution_count": null,
|
| 541 |
+
"id": "ab4621b9",
|
| 542 |
+
"metadata": {},
|
| 543 |
+
"outputs": [],
|
| 544 |
+
"source": []
|
| 545 |
}
|
| 546 |
],
|
| 547 |
"metadata": {
|
pages/2_🎥_Animes.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
st.set_page_config(page_title="Anime Recommendation Engine 🏯", layout="wide")
|
| 4 |
|
| 5 |
-
from utils.common import load_animes, load_anime, load_synopsis_embedding, search_closest_by_uid, write_col, write_col_with_label, display_img, display_synopsis
|
| 6 |
|
| 7 |
st.markdown("## 🎥 Anime Search & Similar Recommendations")
|
| 8 |
|
|
@@ -40,6 +40,9 @@ if selected_anime_uid != None :
|
|
| 40 |
with col2:
|
| 41 |
write_col(selected_anime["synopsis"])
|
| 42 |
write_col_with_label(selected_anime["episodes"], "Episodes : ")
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# RECO_01
|
| 45 |
with st.expander("Anime recommendations based on the description"):
|
|
|
|
| 2 |
|
| 3 |
st.set_page_config(page_title="Anime Recommendation Engine 🏯", layout="wide")
|
| 4 |
|
| 5 |
+
from utils.common import load_animes, load_anime, load_synopsis_embedding, search_closest_by_uid, write_col, write_col_with_label, display_img, display_synopsis, display_review
|
| 6 |
|
| 7 |
st.markdown("## 🎥 Anime Search & Similar Recommendations")
|
| 8 |
|
|
|
|
| 40 |
with col2:
|
| 41 |
write_col(selected_anime["synopsis"])
|
| 42 |
write_col_with_label(selected_anime["episodes"], "Episodes : ")
|
| 43 |
+
if st.button(label="Reviews' summary"):
|
| 44 |
+
display_review(selected_anime["uid"])
|
| 45 |
+
|
| 46 |
|
| 47 |
# RECO_01
|
| 48 |
with st.expander("Anime recommendations based on the description"):
|
utils/common.py
CHANGED
|
@@ -121,35 +121,28 @@ def load_profile_recommendations(df, profile):
|
|
| 121 |
return selected_profile.iloc[0]
|
| 122 |
##
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
## RECO_05 : input in natural language
|
| 126 |
|
| 127 |
# Output format
|
| 128 |
-
class
|
| 129 |
positive: str
|
| 130 |
negative: str
|
| 131 |
title: Optional[str]
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# init Model LLM
|
| 134 |
@st.cache_resource
|
| 135 |
-
def init_model_llm():
|
| 136 |
-
# Prompt string
|
| 137 |
-
sys_prompt="""
|
| 138 |
-
You are a positive and negative element extractor.
|
| 139 |
-
|
| 140 |
-
Analyze the user's sentence and extract:
|
| 141 |
-
- what the user wants (positive),
|
| 142 |
-
- what the user explicitly wants to avoid (negative).
|
| 143 |
-
|
| 144 |
-
If the user mentions a well-known title (such as an anime, movie, game, etc.) in what they want to avoid, extract it separately.
|
| 145 |
-
|
| 146 |
-
Return your response as a JSON object with three fields:
|
| 147 |
-
- positive: a single string summarizing with key-words what the user wants.
|
| 148 |
-
- negative: a single string summarizing with key-words what the user wants to avoid.
|
| 149 |
-
- title: the name of the title the user wants to avoid, if any (e.g., an anime, show, movie); return `null` if none is found.
|
| 150 |
|
| 151 |
-
{{format_instructions}}
|
| 152 |
-
"""
|
| 153 |
|
| 154 |
# Define system prompt
|
| 155 |
start_prompt = ChatPromptTemplate.from_messages([
|
|
@@ -177,8 +170,26 @@ def init_model_MiniLM():
|
|
| 177 |
def search_recommended_animes_from_llm(input_anime_description, filter_hentai_on):
|
| 178 |
input_clean = re.sub("[^A-Za-z]+", " ", str(input_anime_description)).lower()
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Get the response
|
| 184 |
response = model_llm.invoke({"text": input_clean, "format_instructions": parser.get_format_instructions()})
|
|
@@ -282,3 +293,91 @@ def extract_animes_from_uid(df_animes, df_uid):
|
|
| 282 |
selected_animes = df_animes[df_animes['uid'].isin(uids)][['title','uid']].reset_index(drop=True)
|
| 283 |
|
| 284 |
return selected_animes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
return selected_profile.iloc[0]
|
| 122 |
##
|
| 123 |
|
| 124 |
+
#cleaning of a text
|
| 125 |
+
def clean_text(text):
|
| 126 |
+
text = re.sub("[\n\r]", " ", text)
|
| 127 |
+
text = re.sub("[^A-Za-z0-9]+", " ", text)
|
| 128 |
+
return text
|
| 129 |
|
| 130 |
## RECO_05 : input in natural language
|
| 131 |
|
| 132 |
# Output format
|
| 133 |
+
class OutputSchema_describe(BaseModel):
|
| 134 |
positive: str
|
| 135 |
negative: str
|
| 136 |
title: Optional[str]
|
| 137 |
|
| 138 |
+
class OutputSchema_review(BaseModel):
|
| 139 |
+
positive: str
|
| 140 |
+
negative: str
|
| 141 |
+
|
| 142 |
# init Model LLM
|
| 143 |
@st.cache_resource
|
| 144 |
+
def init_model_llm(sys_prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Define system prompt
|
| 148 |
start_prompt = ChatPromptTemplate.from_messages([
|
|
|
|
| 170 |
def search_recommended_animes_from_llm(input_anime_description, filter_hentai_on):
|
| 171 |
input_clean = re.sub("[^A-Za-z]+", " ", str(input_anime_description)).lower()
|
| 172 |
|
| 173 |
+
# Prompt string
|
| 174 |
+
sys_prompt="""
|
| 175 |
+
You are a positive and negative element extractor.
|
| 176 |
+
|
| 177 |
+
Analyze the user's sentence and extract:
|
| 178 |
+
- what the user wants (positive),
|
| 179 |
+
- what the user explicitly wants to avoid (negative).
|
| 180 |
+
|
| 181 |
+
If the user mentions a well-known title (such as an anime, movie, game, etc.) in what they want to avoid, extract it separately.
|
| 182 |
+
|
| 183 |
+
Return your response as a JSON object with three fields:
|
| 184 |
+
- positive: a single string summarizing with key-words what the user wants.
|
| 185 |
+
- negative: a single string summarizing with key-words what the user wants to avoid.
|
| 186 |
+
- title: the name of the title the user wants to avoid, if any (e.g., an anime, show, movie); return `null` if none is found.
|
| 187 |
+
|
| 188 |
+
{{format_instructions}}
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
model_llm = init_model_llm(sys_prompt)
|
| 192 |
+
parser = PydanticOutputParser(pydantic_object=OutputSchema_describe)
|
| 193 |
|
| 194 |
# Get the response
|
| 195 |
response = model_llm.invoke({"text": input_clean, "format_instructions": parser.get_format_instructions()})
|
|
|
|
| 293 |
selected_animes = df_animes[df_animes['uid'].isin(uids)][['title','uid']].reset_index(drop=True)
|
| 294 |
|
| 295 |
return selected_animes
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
# RECO_04
|
| 299 |
+
|
| 300 |
+
# init Model LLM
|
| 301 |
+
@st.cache_resource
|
| 302 |
+
def init_model_llm_reviews():
|
| 303 |
+
# Prompt string
|
| 304 |
+
sys_prompt_reviews="""
|
| 305 |
+
You are a specialized in analysing the positive and negative elements in reviews of animes left by different users. Your task is to provide user with a summary of reviews left by other users on the platform to help them determine whether to watch that anime or not.
|
| 306 |
+
|
| 307 |
+
Ignore the "more pics" citation at the beginning of each review, as it refers to something outside of it.
|
| 308 |
+
|
| 309 |
+
Analyze the users' sentence and extract, if they are encountered:
|
| 310 |
+
- the positive points expressed in the reviews,
|
| 311 |
+
- the negative points on which the criticism is based.
|
| 312 |
+
|
| 313 |
+
Your response shall summarize the key aspects of the anime, taking into account that there are several reviews left by as many users, but without explicitly mentioning the amount of reviews left. Please be relatively measured with the way you present negative criticism.
|
| 314 |
+
You shall also take into account the proportion difference between positive and negative feedbacks.
|
| 315 |
+
|
| 316 |
+
Return your response as a JSON object containing up to two fields:
|
| 317 |
+
If no positive feedback is provided, ignore the generation of the sentence, and proceed to the next point.
|
| 318 |
+
- positive: a single string summarizing the positive aspects raised, starting with "Users liked:"
|
| 319 |
+
If no negative aspect is mentioned, or if they are disproportionately rare compared to the positive feedback, ignore the generation of the sentence. Otherwise, proceed as follows :
|
| 320 |
+
- negative: a single string summarizing the negative aspects raised, starting with "Users disliked:".
|
| 321 |
+
|
| 322 |
+
{{format_instructions}}
|
| 323 |
+
"""
|
| 324 |
+
|
| 325 |
+
# Define system prompt
|
| 326 |
+
start_prompt_reviews = ChatPromptTemplate.from_messages([
|
| 327 |
+
("system", sys_prompt_reviews),
|
| 328 |
+
("user", "{text}")
|
| 329 |
+
])
|
| 330 |
+
|
| 331 |
+
# Don't forget your API Key : export MISTRAL_API_KEY=...
|
| 332 |
+
|
| 333 |
+
# Let's instanciate a model
|
| 334 |
+
llm_reviews = ChatMistralAI(model="mistral-medium-latest")
|
| 335 |
+
|
| 336 |
+
model_llm_reviews = start_prompt_reviews | llm_reviews
|
| 337 |
+
|
| 338 |
+
return model_llm_reviews
|
| 339 |
+
|
| 340 |
+
def generate_review_summary(given_anime_uid):
|
| 341 |
+
sys_prompt_reviews ="""
|
| 342 |
+
You are a specialized in analysing the positive and negative elements in reviews of animes left by different users. Your task is to provide user with a summary of reviews left by other users on the platform to help them determine whether to watch that anime or not.
|
| 343 |
+
|
| 344 |
+
Ignore the "more pics" citation at the beginning of each review, as it refers to something outside of it.
|
| 345 |
+
|
| 346 |
+
Analyze the users' sentence and extract, if they are encountered:
|
| 347 |
+
- the positive points expressed in the reviews,
|
| 348 |
+
- the negative points on which the criticism is based.
|
| 349 |
+
|
| 350 |
+
Your response shall summarize the key aspects of the anime, taking into account that there are several reviews left by as many users, but without explicitly mentioning the amount of reviews left. Please be relatively measured with the way you present negative criticism.
|
| 351 |
+
You shall also take into account the proportion difference between positive and negative feedbacks.
|
| 352 |
+
|
| 353 |
+
Return your response as a JSON object containing up to two fields:
|
| 354 |
+
If no positive feedback is provided, ignore the generation of the sentence, and proceed to the next point.
|
| 355 |
+
- positive: a single string summarizing the positive aspects raised, starting with "Users liked:"
|
| 356 |
+
If no negative aspect is mentioned, or if they are disproportionately rare compared to the positive feedback, ignore the generation of the sentence. Otherwise, proceed as follows :
|
| 357 |
+
- negative: a single string summarizing the negative aspects raised, starting with "Users disliked:".
|
| 358 |
+
|
| 359 |
+
{{format_instructions}}
|
| 360 |
+
"""
|
| 361 |
+
model_llm_reviews = init_model_llm(sys_prompt_reviews)
|
| 362 |
+
parser = PydanticOutputParser(pydantic_object=OutputSchema_review)
|
| 363 |
+
|
| 364 |
+
df_reviews=load_reviews()
|
| 365 |
+
input=df_reviews[df_reviews['anime_uid']==given_anime_uid]['text'].apply(clean_text)
|
| 366 |
+
if len(input) == 0:
|
| 367 |
+
output = ["This anime has no review yet! You can watch it and review it yourself!"]
|
| 368 |
+
else:
|
| 369 |
+
response = model_llm_reviews.invoke({"text": input, "format_instructions": parser.get_format_instructions()})
|
| 370 |
+
output_positive = parser.parse(response.content).positive
|
| 371 |
+
output_negative = parser.parse(response.content).negative
|
| 372 |
+
ai_notice = f'Response generated by Mistral AI, based on {len(input)} reviews'
|
| 373 |
+
output = [output_positive, output_negative, ai_notice]
|
| 374 |
+
return output
|
| 375 |
+
|
| 376 |
+
#Display AI review summary
|
| 377 |
+
@st.dialog("Review summary : powered by Mistral AI")
|
| 378 |
+
def display_review(given_anime_uid):
|
| 379 |
+
if given_anime_uid is not None:
|
| 380 |
+
response = generate_review_summary(given_anime_uid)
|
| 381 |
+
for text in response:
|
| 382 |
+
st.markdown(text)
|
| 383 |
+
|