Spaces:
Runtime error
Runtime error
Delete notebooks
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "8e40675b", "metadata": {}, "outputs": [], "source": [ "from sentence_transformers import SentenceTransformer\n", "import chromadb" ] }, { "cell_type": "code", "execution_count": 3, "id": "bac36205", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/pushkar/AllDocuments/EtE_projects/anime_rag_system\n" ] } ], "source": [ "from pathlib import Path\n", "\n", "# BASE_DIR = Path(__file__).resolve().parent # if python file\n", "BASE_DIR = Path.cwd().parent\n", "print(BASE_DIR)" ] }, { "cell_type": "code", "execution_count": 4, "id": "2878800c", "metadata": {}, "outputs": [], "source": [ "client = chromadb.PersistentClient(path=BASE_DIR / \"data/embeddings/chroma_db\")\n", "collection = client.get_collection(name=\"anime_collection\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "70b38ecc", "metadata": {}, "outputs": [], "source": [ "queries = [\n", " \"psychological thriller anime\",\n", " \"comedy slice of life school anime\",\n", " \"anime like Attack on Titan\",\n", " \"sad anime that will make me cry\"\n", "]" ] }, { "cell_type": "code", "execution_count": 6, "id": "672b383c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Query: psychological thriller anime: \n", "1. Kimetsu no Yaiba Movie 1: Mugenjou-hen - Akaza Sairai (distance: 0.902)\n", "2. Hoozuki no Reitetsu 2nd Season (distance: 0.912)\n", "3. Mob Psycho 100 II (distance: 0.961)\n", "4. Mob Psycho 100 III (distance: 1.003)\n", "5. Akira (distance: 1.037)\n", "6. Mob Psycho 100 (distance: 1.041)\n", "7. Fujimoto Tatsuki 17-26 (distance: 1.056)\n", "8. Guimi Zhi Zhu: Xiaochou Pian (distance: 1.057)\n", "Query: comedy slice of life school anime: \n", "1. 3-nen Z-gumi Ginpachi-sensei (distance: 0.759)\n", "2. Kimi to Boku. 2 (distance: 0.793)\n", "3. School Rumble (distance: 0.914)\n", "4. Non Non Biyori Repeat (distance: 0.926)\n", "5. Hidamari Sketch: Sae Hiro Sotsugyou-hen (distance: 0.930)\n", "6. 5-toubun no Hanayome∽ (distance: 0.933)\n", "7. Danshi Koukousei no Nichijou (distance: 0.935)\n", "8. Kono Subarashii Sekai ni Shukufuku wo! 3: Bonus Stage (distance: 0.940)\n", "Query: anime like Attack on Titan: \n", "1. Shingeki no Kyojin Movie: Kanketsu-hen - The Last Attack (distance: 0.719)\n", "2. Kidou Senshi Zeta Gundam (distance: 0.886)\n", "3. Shingeki no Kyojin: The Final Season (distance: 0.937)\n", "4. Shingeki no Kyojin: The Final Season Part 2 (distance: 0.941)\n", "5. Shingeki no Kyojin (distance: 0.971)\n", "6. Shingeki no Kyojin Season 2 (distance: 0.994)\n", "7. Shingeki no Kyojin Season 3 Part 2 (distance: 1.011)\n", "8. Shingeki no Kyojin OVA (distance: 1.034)\n", "Query: sad anime that will make me cry: \n", "1. Fujimoto Tatsuki 17-26 (distance: 0.990)\n", "2. Osomatsu-san Movie (distance: 1.051)\n", "3. Gintama: Dai Hanseikai (distance: 1.052)\n", "4. Yuuki Yuuna wa Yuusha de Aru: Washio Sumi no Shou 3 - Yakusoku (distance: 1.094)\n", "5. Tengoku Daimakyou (distance: 1.096)\n", "6. Kimetsu no Yaiba Movie 1: Mugenjou-hen - Akaza Sairai (distance: 1.107)\n", "7. Girls Band Cry (distance: 1.111)\n", "8. Mahou Shoujo Madoka★Magica Movie 3: Hangyaku no Monogatari (distance: 1.121)\n" ] } ], "source": [ "for query in queries:\n", " print(f\"Query: {query}: \")\n", " results = collection.query(\n", " query_texts=[query],\n", " n_results=8\n", " )\n", "\n", " for i, (title, distance) in enumerate(zip(\n", " [m[\"title\"] for m in results[\"metadatas\"][0]],\n", " results[\"distances\"][0]\n", " )):\n", " print(f\"{i+1}. {title} (distance: {distance:.3f})\")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "a9843de5", "metadata": {}, "outputs": [], "source": [ "def show_results(results):\n", " for i, (title, distance) in enumerate(zip(\n", " [m[\"title\"] for m in results[\"metadatas\"][0]],\n", " results[\"distances\"][0]\n", " )):\n", " print(f\"{i+1}. {title} (distance: {distance:.3f})\")" ] }, { "cell_type": "code", "execution_count": null, "id": "453ed4e7", "metadata": {}, "outputs": [], "source": [ "que = [\"Anime about a mage who goes on adventures\"]\n", "\n", "res1 = collection.query(query_texts=que,\n", " n_results=20)\n", "\n", "res2 = collection.query(query_texts=que,\n", " n_results=100,\n", " where={\"score\":{\n", " \"$gt\": 0\n", " }})" ] }, { "cell_type": "code", "execution_count": 13, "id": "985018d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Results without metadata filtering: \n", "1. Meitantei Conan: Episode One - Chiisaku Natta Meitantei (distance: 1.009)\n", "2. Magi: The Kingdom of Magic (distance: 1.035)\n", "3. Guimi Zhi Zhu: Xiaochou Pian (distance: 1.072)\n", "4. 3-nen Z-gumi Ginpachi-sensei (distance: 1.075)\n", "5. Mahoutsukai no Yome: Hoshi Matsu Hito (distance: 1.088)\n", "6. Magi: The Labyrinth of Magic (distance: 1.090)\n", "7. Akagami no Shirayuki-hime 2nd Season (distance: 1.091)\n", "8. Kimetsu no Yaiba Movie 1: Mugenjou-hen - Akaza Sairai (distance: 1.103)\n", "9. Silent Witch: Chinmoku no Majo no Kakushigoto (distance: 1.117)\n", "10. Meitantei Conan Movie 13: Shikkoku no Chaser (distance: 1.121)\n", "11. Yuru Yuri San☆Hai! (distance: 1.126)\n", "12. Gintama: Dai Hanseikai (distance: 1.139)\n", "13. Zhu Xian 2nd Season (distance: 1.142)\n", "14. Yuri!!! on Ice: Yuri Plisetsky GPF in Barcelona EX - Welcome to The Madness (distance: 1.144)\n", "15. Mushoku Tensei: Isekai Ittara Honki Dasu (distance: 1.148)\n", "16. Kono Subarashii Sekai ni Shukufuku wo! 3: Bonus Stage (distance: 1.149)\n", "17. Lupin III vs. Meitantei Conan: The Movie (distance: 1.154)\n", "18. xxxHOLiC Movie: Manatsu no Yoru no Yume (distance: 1.155)\n", "19. Hoozuki no Reitetsu 2nd Season (distance: 1.156)\n", "20. Tsubasa: Shunraiki (distance: 1.157)\n", "Results with metadata filtering: \n", "1 Meitantei Conan: Episode One - Chiisaku Natta Meitantei, \n", "Genre: Adventure, Comedy, Mystery \n", "Distance: 1.0094857215881348\n", "1 Magi: The Kingdom of Magic, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.0348175764083862\n", "1 Magi: The Labyrinth of Magic, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.0896832942962646\n", "1 Zhu Xian 2nd Season, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.1424976587295532\n", "1 Mushoku Tensei: Isekai Ittara Honki Dasu, \n", "Genre: Adventure, Drama, Fantasy, Ecchi \n", "Distance: 1.1479932069778442\n", "1 Kono Subarashii Sekai ni Shukufuku wo! 3: Bonus Stage, \n", "Genre: Adventure, Comedy, Fantasy \n", "Distance: 1.1492812633514404\n", "1 Tsubasa: Shunraiki, \n", "Genre: Action, Adventure, Drama, Fantasy, Romance \n", "Distance: 1.1567258834838867\n", "1 Wanmei Shijie, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.1588501930236816\n", "1 Meitantei Conan OVA 09: 10-nengo no Stranger, \n", "Genre: Adventure, Comedy, Mystery \n", "Distance: 1.1683908700942993\n", "1 Meitantei Conan Movie 03: Seikimatsu no Majutsushi, \n", "Genre: Adventure, Comedy, Mystery \n", "Distance: 1.183741807937622\n", "1 Golden Boy, \n", "Genre: Adventure, Comedy, Ecchi \n", "Distance: 1.1899913549423218\n", "1 Mirai Shounen Conan, \n", "Genre: Adventure, Drama, Sci-Fi \n", "Distance: 1.192782998085022\n", "1 Mushoku Tensei: Isekai Ittara Honki Dasu Part 2, \n", "Genre: Adventure, Drama, Fantasy, Ecchi \n", "Distance: 1.2001222372055054\n", "1 Majo no Takkyuubin, \n", "Genre: Adventure, Award Winning, Comedy, Drama, Fantasy \n", "Distance: 1.2021398544311523\n", "1 Naruto, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.2085628509521484\n", "1 Sword Art Online: Progressive Movie - Hoshi Naki Yoru no Aria, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.2172694206237793\n", "1 Ginga Sengoku Gunyuuden Rai, \n", "Genre: Adventure, Romance, Sci-Fi \n", "Distance: 1.2243961095809937\n", "1 Xian Ni, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.2341101169586182\n", "1 Overlord, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.2380468845367432\n", "1 Akatsuki no Yona OVA, \n", "Genre: Adventure, Fantasy \n", "Distance: 1.2383925914764404\n", "1 Mushishi Zoku Shou: Suzu no Shizuku, \n", "Genre: Adventure, Mystery, Slice of Life, Supernatural \n", "Distance: 1.2392939329147339\n", "1 Shen Yin Wangzuo Movie: Yi Lai Ke Si Chuanqi, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.239490270614624\n", "1 Dragon Ball, \n", "Genre: Action, Adventure, Comedy, Fantasy \n", "Distance: 1.2412399053573608\n", "1 Wu Liuqi: Xuanwu Guo Pian, \n", "Genre: Action, Adventure, Comedy, Drama, Mystery \n", "Distance: 1.242012858390808\n", "1 Meitantei Conan, \n", "Genre: Adventure, Comedy, Mystery \n", "Distance: 1.2530790567398071\n", "1 Kono Subarashii Sekai ni Shukufuku wo! 2, \n", "Genre: Adventure, Comedy, Fantasy \n", "Distance: 1.2592735290527344\n", "1 Fanren Xiu Xian Chuan: Xinghai Feichi Prologue, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.264496922492981\n", "1 Saint Seiya: The Lost Canvas - Meiou Shinwa 2, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.267714023590088\n", "1 Meitantei Conan Movie 08: Ginyoku no Magician, \n", "Genre: Adventure, Comedy, Mystery \n", "Distance: 1.2713207006454468\n", "1 Fullmetal Alchemist: Brotherhood Specials, \n", "Genre: Action, Adventure, Drama, Fantasy \n", "Distance: 1.273901343345642\n", "1 Naruto: Shippuuden, \n", "Genre: Action, Adventure, Fantasy \n", "Distance: 1.2760953903198242\n", "1 Tenkuu no Shiro Laputa, \n", "Genre: Adventure, Award Winning, Fantasy \n", "Distance: 1.278309941291809\n", "1 Ie Naki Ko Remy, \n", "Genre: Adventure, Drama \n", "Distance: 1.2802643775939941\n", "1 Yi Nian Yong Heng 3rd Season, \n", "Genre: Action, Adventure, Comedy, Fantasy \n", "Distance: 1.284270167350769\n" ] } ], "source": [ "print(\"Results without metadata filtering: \")\n", "show_results(res1)\n", "\n", "print(\"Results with metadata filtering: \")\n", "i = 0;\n", "for _, (title, genres, distance) in enumerate(zip([m[\"title\"] for m in res2[\"metadatas\"][0]], [m.get(\"genres\", \"Unknown\") for m in res2[\"metadatas\"][0]], res2[\"distances\"][0])):\n", " if \"adventure\" in genres.lower():\n", " print(f\"{i + 1} {title}, \\nGenre: {genres} \\nDistance: {distance}\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b6e41bbf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "42413036", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "anime-rag-system", "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.14.0" } }, "nbformat": 4, "nbformat_minor": 5}notebooks/01_embedding_experiments.ipynbDeleted-347