Upload scandl2_demo.ipynb
#2
by minhgou - opened
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "a8049e2d", "metadata": {}, "outputs": [], "source": [ "%pip uninstall -y gradio gradio-client\n", "%pip install \"huggingface-hub>=0.16.4,<0.18\"\n", "%pip check" ] }, { "cell_type": "markdown", "id": "1d32e5f6", "metadata": {}, "source": [ "# ScanDL2 Demo" ] }, { "cell_type": "code", "execution_count": 107, "id": "73158dd6", "metadata": {}, "outputs": [], "source": [ "from ScanDL2 import ScanDL2\n", "import torch\n", "import sys\n", "import torch\n", "from unittest.mock import patch" ] }, { "cell_type": "code", "execution_count": 110, "id": "6cb55d37", "metadata": {}, "outputs": [], "source": [ "sample_sentences = [\n", " \"The quick brown fox jumps over the lazy dog.\",\n", "]\n", "sample_paragraphs = [\n", " \"The sun was shining brightly in the clear blue sky. Birds chirped melodiously from the trees, filling the air with a cheerful tune. It was the perfect day for a walk in the park.\",\n", " \"She had always loved the scent of fresh coffee in the morning. As she sipped her espresso, she glanced at the newspaper headlines. Another day, another adventure awaited her.\",\n", " \"The conference room was filled with eager attendees. The keynote speaker took the stage, adjusting the microphone. With a confident smile, he began his presentation on the future of artificial intelligence.\",\n", " \"They packed their bags and set off on their long-awaited road trip. The open road stretched ahead, inviting them to explore new places. With music playing softly in the background, they felt a sense of freedom like never before.\",\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "762cf32a", "metadata": {}, "outputs": [], "source": [ "def display_scandl_scanpath(text, output, sample_index=0):\n", " words = output[\"predicted_sp_words\"][sample_index]\n", " positions = output[\"predicted_sp_ids\"][sample_index]\n", " durations = output[\"predicted_fix_durs\"][sample_index]\n", "\n", " if not (len(words) == len(positions) == len(durations)):\n", " raise ValueError(\"Word, position, and duration lengths do not match.\")\n", "\n", " rows = []\n", "\n", " for step in range(len(positions)):\n", " word = str(words[step]).replace(\"|\", r\"\\|\")\n", " rows.append(\n", " f\"| {step + 1} | {word} | {positions[step]} \"\n", " f\"| {float(durations[step]):.1f} |\"\n", " )\n", "\n", " display(Markdown(\n", " f\"**Input:** {text}\\n\\n\"\n", " f\"**Scanpath:** {positions}\\n\\n\"\n", " f\"**Fixated sequence:** {' → '.join(words)}\\n\\n\"\n", " \"| Fixation | Word | Position | Duration (ms) |\\n\"\n", " \"|---|---|---|---|\\n\"\n", " + \"\\n\".join(rows)\n", " ))\n", "\n", "def run_scandl_raw_text_input(model, text):\n", " torch.manual_seed(0)\n", "\n", " with torch.no_grad():\n", " return model(texts=[text])" ] }, { "cell_type": "markdown", "id": "34e2e22e", "metadata": {}, "source": [ "## ScanDL2 — Sentences" ] }, { "cell_type": "code", "execution_count": 111, "id": "9be2fa4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Logging to /var/folders/vg/2mjpcbv55m966znhbf9c5k580000gn/T/openai-2026-10-27-16-28-41-658594\n", "### Loading ScanDL Diffusion Module ...\n", "### Total number of parameters: 130855296\n", "\t\t### ScanDL Module generates fixation locations ...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "1it [01:08, 68.80s/it]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\t\t### FixDur Module generates fixation durations ...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "1it [00:00, 12.33it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "fixdur original sn: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/markdown": [ "### Sentence 1" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** The quick brown fox jumps over the lazy dog.\n", "\n", "**Scanpath:** [1, 2, 3, 4, 5, 5, 6, 8, 8]\n", "\n", "**Fixated sequence:** The → quick → brown → fox → jumps → jumps → over → lazy → lazy\n", "\n", "| Fixation | Word | Position | Duration (ms) |\n", "|---|---|---|---|\n", "| 1 | The | 1 | 191.8 |\n", "| 2 | quick | 2 | 188.5 |\n", "| 3 | brown | 3 | 201.7 |\n", "| 4 | fox | 4 | 207.9 |\n", "| 5 | jumps | 5 | 219.3 |\n", "| 6 | jumps | 5 | 229.0 |\n", "| 7 | over | 6 | 235.7 |\n", "| 8 | lazy | 8 | 221.9 |\n", "| 9 | lazy | 8 | 173.3 |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with patch.object(sys, \"argv\", [sys.argv[0]]):\n", " scandl2_sent = ScanDL2(\n", " text_type=\"sentence\",\n", " bsz=2,\n", " save=None,\n", " filename=None,\n", ")\n", "scandl2_sent.eval()\n", "\n", "with torch.no_grad():\n", " sentence_output = scandl2_sent(sample_sentences)\n", "\n", "for i, text in enumerate(sample_sentences):\n", " display(Markdown(f\"### Sentence {i + 1}\"))\n", " display_scandl_scanpath(text, sentence_output, sample_index=i)\n" ] }, { "cell_type": "markdown", "id": "2887484f", "metadata": {}, "source": [ "## ScanDL2 — Paragraphs" ] }, { "cell_type": "code", "execution_count": 109, "id": "6e695749", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Logging to /var/folders/vg/2mjpcbv55m966znhbf9c5k580000gn/T/openai-2026-10-27-16-08-03-314381\n", "### Loading ScanDL Diffusion Module ...\n", "### Total number of parameters: 131630560\n", "\t\t### ScanDL Module generates fixation locations ...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2it [11:09, 334.98s/it]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\t\t### FixDur Module generates fixation durations ...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "1it [00:00, 2.03it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "fixdur original sn: ['The', 'sun', 'was', 'shining', 'brightly', 'in', 'the', 'clear', 'blue', 'sky.', 'Birds', 'chirped', 'melodiously', 'from', 'the', 'trees,', 'filling', 'the', 'air', 'with', 'a', 'cheerful', 'tune.', 'It', 'was', 'the', 'perfect', 'day', 'for', 'a', 'walk', 'in', 'the', 'park.']\n", "fixdur original sn: ['She', 'had', 'always', 'loved', 'the', 'scent', 'of', 'fresh', 'coffee', 'in', 'the', 'morning.', 'As', 'she', 'sipped', 'her', 'espresso,', 'she', 'glanced', 'at', 'the', 'newspaper', 'headlines.', 'Another', 'day,', 'another', 'adventure', 'awaited', 'her.']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2it [00:00, 2.36it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "fixdur original sn: ['The', 'conference', 'room', 'was', 'filled', 'with', 'eager', 'attendees.', 'The', 'keynote', 'speaker', 'took', 'the', 'stage,', 'adjusting', 'the', 'microphone.', 'With', 'a', 'confident', 'smile,', 'he', 'began', 'his', 'presentation', 'on', 'the', 'future', 'of', 'artificial', 'intelligence.']\n", "fixdur original sn: ['They', 'packed', 'their', 'bags', 'and', 'set', 'off', 'on', 'their', 'long-awaited', 'road', 'trip.', 'The', 'open', 'road', 'stretched', 'ahead,', 'inviting', 'them', 'to', 'explore', 'new', 'places.', 'With', 'music', 'playing', 'softly', 'in', 'the', 'background,', 'they', 'felt', 'a', 'sense', 'of', 'freedom', 'like', 'never', 'before.']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/markdown": [ "### Paragraph 1" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** The sun was shining brightly in the clear blue sky. Birds chirped melodiously from the trees, filling the air with a cheerful tune. It was the perfect day for a walk in the park.\n", "\n", "**Scanpath:** [2, 3, 4, 4, 5, 8, 7, 9, 11, 11, 13, 12, 12, 13, 14, 17, 17, 16, 18, 20, 22, 22, 23, 25, 27, 29, 28, 31, 31, 32, 34, 34]\n", "\n", "**Fixated sequence:** sun → was → shining → shining → brightly → clear → the → blue → Birds → Birds → melodiously → chirped → chirped → melodiously → from → filling → filling → trees, → the → with → cheerful → cheerful → tune. → was → perfect → for → day → walk → walk → in → park. → park.\n", "\n", "| Fixation | Word | Position | Duration (ms) |\n", "|---|---|---|---|\n", "| 1 | sun | 2 | 221.6 |\n", "| 2 | was | 3 | 197.2 |\n", "| 3 | shining | 4 | 212.7 |\n", "| 4 | shining | 4 | 220.1 |\n", "| 5 | brightly | 5 | 228.2 |\n", "| 6 | clear | 8 | 226.2 |\n", "| 7 | the | 7 | 224.6 |\n", "| 8 | blue | 9 | 220.1 |\n", "| 9 | Birds | 11 | 219.7 |\n", "| 10 | Birds | 11 | 221.9 |\n", "| 11 | melodiously | 13 | 227.9 |\n", "| 12 | chirped | 12 | 231.2 |\n", "| 13 | chirped | 12 | 221.8 |\n", "| 14 | melodiously | 13 | 218.9 |\n", "| 15 | from | 14 | 221.9 |\n", "| 16 | filling | 17 | 230.9 |\n", "| 17 | filling | 17 | 210.3 |\n", "| 18 | trees, | 16 | 223.7 |\n", "| 19 | the | 18 | 222.9 |\n", "| 20 | with | 20 | 228.0 |\n", "| 21 | cheerful | 22 | 223.6 |\n", "| 22 | cheerful | 22 | 219.1 |\n", "| 23 | tune. | 23 | 212.6 |\n", "| 24 | was | 25 | 209.8 |\n", "| 25 | perfect | 27 | 206.0 |\n", "| 26 | for | 29 | 224.1 |\n", "| 27 | day | 28 | 210.8 |\n", "| 28 | walk | 31 | 196.1 |\n", "| 29 | walk | 31 | 193.6 |\n", "| 30 | in | 32 | 183.9 |\n", "| 31 | park. | 34 | 189.7 |\n", "| 32 | park. | 34 | 187.6 |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "### Paragraph 2" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** She had always loved the scent of fresh coffee in the morning. As she sipped her espresso, she glanced at the newspaper headlines. Another day, another adventure awaited her.\n", "\n", "**Scanpath:** [1, 2, 3, 4, 6, 8, 9, 11, 12, 15, 15, 15, 17, 19, 23, 24, 25, 27, 28, 28, 29]\n", "\n", "**Fixated sequence:** She → had → always → loved → scent → fresh → coffee → the → morning. → sipped → sipped → sipped → espresso, → glanced → headlines. → Another → day, → adventure → awaited → awaited → her.\n", "\n", "| Fixation | Word | Position | Duration (ms) |\n", "|---|---|---|---|\n", "| 1 | She | 1 | 220.6 |\n", "| 2 | had | 2 | 195.1 |\n", "| 3 | always | 3 | 211.5 |\n", "| 4 | loved | 4 | 218.4 |\n", "| 5 | scent | 6 | 226.8 |\n", "| 6 | fresh | 8 | 226.2 |\n", "| 7 | coffee | 9 | 223.3 |\n", "| 8 | the | 11 | 219.0 |\n", "| 9 | morning. | 12 | 217.6 |\n", "| 10 | sipped | 15 | 219.6 |\n", "| 11 | sipped | 15 | 225.9 |\n", "| 12 | sipped | 15 | 227.9 |\n", "| 13 | espresso, | 17 | 220.1 |\n", "| 14 | glanced | 19 | 216.8 |\n", "| 15 | headlines. | 23 | 219.4 |\n", "| 16 | Another | 24 | 228.6 |\n", "| 17 | day, | 25 | 207.4 |\n", "| 18 | adventure | 27 | 222.9 |\n", "| 19 | awaited | 28 | 220.6 |\n", "| 20 | awaited | 28 | 227.6 |\n", "| 21 | her. | 29 | 222.0 |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "### Paragraph 3" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** The conference room was filled with eager attendees. The keynote speaker took the stage, adjusting the microphone. With a confident smile, he began his presentation on the future of artificial intelligence.\n", "\n", "**Scanpath:** [1, 2, 3, 3, 5, 5, 7, 7, 8, 9, 10, 10, 12, 11, 14, 15, 17, 19, 21, 25, 23, 27, 28, 30, 31]\n", "\n", "**Fixated sequence:** The → conference → room → room → filled → filled → eager → eager → attendees. → The → keynote → keynote → took → speaker → stage, → adjusting → microphone. → a → smile, → presentation → began → the → future → artificial → intelligence.\n", "\n", "| Fixation | Word | Position | Duration (ms) |\n", "|---|---|---|---|\n", "| 1 | The | 1 | 220.4 |\n", "| 2 | conference | 2 | 195.4 |\n", "| 3 | room | 3 | 211.5 |\n", "| 4 | room | 3 | 218.5 |\n", "| 5 | filled | 5 | 226.9 |\n", "| 6 | filled | 5 | 225.9 |\n", "| 7 | eager | 7 | 223.4 |\n", "| 8 | eager | 7 | 219.0 |\n", "| 9 | attendees. | 8 | 217.8 |\n", "| 10 | The | 9 | 219.9 |\n", "| 11 | keynote | 10 | 226.0 |\n", "| 12 | keynote | 10 | 228.4 |\n", "| 13 | took | 12 | 220.2 |\n", "| 14 | speaker | 11 | 217.0 |\n", "| 15 | stage, | 14 | 219.7 |\n", "| 16 | adjusting | 15 | 228.9 |\n", "| 17 | microphone. | 17 | 207.9 |\n", "| 18 | a | 19 | 222.7 |\n", "| 19 | smile, | 21 | 220.9 |\n", "| 20 | presentation | 25 | 227.4 |\n", "| 21 | began | 23 | 222.1 |\n", "| 22 | the | 27 | 218.0 |\n", "| 23 | future | 28 | 210.6 |\n", "| 24 | artificial | 30 | 208.1 |\n", "| 25 | intelligence. | 31 | 203.7 |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "### Paragraph 4" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** They packed their bags and set off on their long-awaited road trip. The open road stretched ahead, inviting them to explore new places. With music playing softly in the background, they felt a sense of freedom like never before.\n", "\n", "**Scanpath:** [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 18, 17, 19, 19, 21, 21, 22, 22, 23, 25, 25, 27, 29, 30, 31, 32, 33, 36, 37, 36, 39]\n", "\n", "**Fixated sequence:** They → packed → their → bags → set → off → on → their → long-awaited → road → trip. → The → open → stretched → stretched → inviting → ahead, → them → them → explore → explore → new → new → places. → music → music → softly → the → background, → they → felt → a → freedom → like → freedom → before.\n", "\n", "| Fixation | Word | Position | Duration (ms) |\n", "|---|---|---|---|\n", "| 1 | They | 1 | 222.5 |\n", "| 2 | packed | 2 | 198.3 |\n", "| 3 | their | 3 | 213.7 |\n", "| 4 | bags | 4 | 221.3 |\n", "| 5 | set | 6 | 229.2 |\n", "| 6 | off | 7 | 226.6 |\n", "| 7 | on | 8 | 225.4 |\n", "| 8 | their | 9 | 220.7 |\n", "| 9 | long-awaited | 10 | 221.2 |\n", "| 10 | road | 11 | 223.3 |\n", "| 11 | trip. | 12 | 229.2 |\n", "| 12 | The | 13 | 232.8 |\n", "| 13 | open | 14 | 223.0 |\n", "| 14 | stretched | 16 | 220.0 |\n", "| 15 | stretched | 16 | 223.2 |\n", "| 16 | inviting | 18 | 232.0 |\n", "| 17 | ahead, | 17 | 211.8 |\n", "| 18 | them | 19 | 224.5 |\n", "| 19 | them | 19 | 224.2 |\n", "| 20 | explore | 21 | 228.6 |\n", "| 21 | explore | 21 | 224.7 |\n", "| 22 | new | 22 | 220.0 |\n", "| 23 | new | 22 | 214.0 |\n", "| 24 | places. | 23 | 210.9 |\n", "| 25 | music | 25 | 207.4 |\n", "| 26 | music | 25 | 224.4 |\n", "| 27 | softly | 27 | 212.3 |\n", "| 28 | the | 29 | 198.9 |\n", "| 29 | background, | 30 | 196.8 |\n", "| 30 | they | 31 | 187.3 |\n", "| 31 | felt | 32 | 192.9 |\n", "| 32 | a | 33 | 191.2 |\n", "| 33 | freedom | 36 | 174.0 |\n", "| 34 | like | 37 | 186.6 |\n", "| 35 | freedom | 36 | 182.3 |\n", "| 36 | before. | 39 | 168.4 |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with patch.object(sys, \"argv\", [sys.argv[0]]):\n", " scandl2_par = ScanDL2(\n", " text_type=\"paragraph\",\n", " bsz=2,\n", " save=None,\n", " filename=None,\n", ")\n", "scandl2_par.eval()\n", "\n", "with torch.no_grad():\n", " paragraph_output = scandl2_par(sample_paragraphs)\n", "\n", "for i, text in enumerate(sample_paragraphs):\n", " display(Markdown(f\"### Paragraph {i + 1}\"))\n", " display_scandl_scanpath(text, paragraph_output, sample_index=i)\n" ] }, { "cell_type": "markdown", "id": "1c44a7f5", "metadata": {}, "source": [ "# Eyettention Demo" ] }, { "cell_type": "markdown", "id": "c33d6269", "metadata": {}, "source": [ "### Chinese Language / BSC Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "14a3e7ef", "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, Markdown\n", "import warnings\n", "\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "def display_scanpath(text, scanpath, dataset=\"BSC\", previous_scanpath=None):\n", " if dataset == \"BSC\":\n", " units = [char for char in text if not char.isspace()]\n", " elif dataset == \"celer\":\n", " units = text.split()\n", " else:\n", " raise ValueError(\"dataset must be 'BSC' or 'celer'\")\n", "\n", " positions = scanpath[0].tolist()\n", "\n", " # Match the model's handling of an omitted start token.\n", " if previous_scanpath is not None:\n", " previous_scanpath = list(previous_scanpath)\n", " if not previous_scanpath or previous_scanpath[0] != 0:\n", " previous_scanpath = [0] + previous_scanpath\n", "\n", " rows = []\n", " sequence = []\n", "\n", " for step, position in enumerate(positions):\n", " if position == len(units) + 1: # End token\n", " break\n", " if position == 0: # Start token\n", " continue\n", "\n", " unit = (\n", " units[position - 1]\n", " if 1 <= position <= len(units)\n", " else \"?\"\n", " )\n", "\n", " source = (\n", " \"Observed\"\n", " if previous_scanpath is not None and step < len(previous_scanpath)\n", " else \"Generated\"\n", " )\n", "\n", " sequence.append(unit)\n", " rows.append(\n", " f\"| {len(rows) + 1} | {unit} | {position} | {source} |\"\n", " )\n", "\n", " display(Markdown(\n", " f\"**Input:** {text}\\n\\n\"\n", " f\"**Scanpath:** {scanpath.tolist()[0]}\\n\\n\"\n", " f\"**Fixated sequence:** {' → '.join(sequence)}\\n\\n\"\n", " \"| Fixation | Word / Character | Position | Source |\\n\"\n", " \"|---|---|---|---|\\n\"\n", " + \"\\n\".join(rows)\n", " ))" ] }, { "cell_type": "code", "execution_count": null, "id": "23163c57", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "keeping Bert with pre-trained weights\n" ] }, { "data": { "text/markdown": [ "## Chinese Raw-text generation" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** 中国选手在男子滑雪比赛中有望蝉联冠军\n", "\n", "**Scanpath:** [0, 1, 1, 4, 3]\n", "\n", "**Fixated sequence:** 中 → 中 → 手 → 选\n", "\n", "| Fixation | Word / Character | Position | Source |\n", "|---|---|---|---|\n", "| 1 | 中 | 1 | Generated |\n", "| 2 | 中 | 1 | Generated |\n", "| 3 | 手 | 4 | Generated |\n", "| 4 | 选 | 3 | Generated |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "---\n", "\n", "## Chinese Continuation from a previous scanpath" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** 中国选手在男子滑雪比赛中有望蝉联冠军\n", "\n", "**Scanpath:** [0, 1, 2, 3, 4, 4, 7, 9, 11]\n", "\n", "**Fixated sequence:** 中 → 国 → 选 → 手 → 手 → 子 → 雪 → 赛\n", "\n", "| Fixation | Word / Character | Position | Source |\n", "|---|---|---|---|\n", "| 1 | 中 | 1 | Observed |\n", "| 2 | 国 | 2 | Observed |\n", "| 3 | 选 | 3 | Observed |\n", "| 4 | 手 | 4 | Generated |\n", "| 5 | 手 | 4 | Generated |\n", "| 6 | 子 | 7 | Generated |\n", "| 7 | 雪 | 9 | Generated |\n", "| 8 | 赛 | 11 | Generated |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from Eyettention import EyettentionRawTextInference\n", "\n", "chinese_text = \"中国选手在男子滑雪比赛中有望蝉联冠军\"\n", "chinese_previous_scanpath = [1,2,3]\n", "\n", "chinese_model = EyettentionRawTextInference(\n", " checkpoint_path=\"Eyettention/results/BSC/Eyettention_chinese.pth\",\n", " dataset=\"BSC\",\n", " device=\"cpu\",\n", ")\n", "\n", "def run_chinese_raw_text_input(model, text, max_pred_len=5):\n", " torch.manual_seed(42)\n", "\n", " scanpath, _ = model.generate_from_chinese_text(\n", " text=text,\n", " max_pred_len=max_pred_len,\n", " previous_scanpath=None\n", " )\n", " \n", " return scanpath\n", "\n", "def run_chinese_previous_scanpath(model, text, max_pred_len=5, previous_scanpath=None):\n", " torch.manual_seed(42)\n", "\n", " scanpath, _ = model.generate_from_chinese_text(\n", " text=text,\n", " max_pred_len=max_pred_len,\n", " previous_scanpath= previous_scanpath\n", " )\n", "\n", " return scanpath\n", "\n", "\n", "display(Markdown(\"## Chinese Raw-text generation\"))\n", "\n", "raw_text = run_chinese_raw_text_input(chinese_model, chinese_text)\n", "display_scanpath(chinese_text, scanpath=raw_text, dataset=\"BSC\")\n", "\n", "\n", "display(Markdown(\"---\\n\\n## Chinese Continuation from a previous scanpath\"))\n", "\n", "\n", "continuation = run_chinese_previous_scanpath(\n", " chinese_model,\n", " chinese_text,\n", " previous_scanpath=chinese_previous_scanpath,\n", ")\n", "\n", "display_scanpath(\n", " chinese_text,\n", " scanpath=continuation,\n", " dataset=\"BSC\",\n", " previous_scanpath=chinese_previous_scanpath,\n", ")" ] }, { "cell_type": "markdown", "id": "399b583e", "metadata": {}, "source": [ "### English Language / CELER Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "f6ae615b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "keeping Bert with pre-trained weights\n" ] }, { "data": { "text/markdown": [ "## English Raw-text generation" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** The quick brown fox jumps over the lazy dog\n", "\n", "**Scanpath:** [0, 1, 4, 5, 8, 9, 3]\n", "\n", "**Fixated sequence:** The → fox → jumps → lazy → dog → brown\n", "\n", "| Fixation | Word / Character | Position | Source |\n", "|---|---|---|---|\n", "| 1 | The | 1 | Generated |\n", "| 2 | fox | 4 | Generated |\n", "| 3 | jumps | 5 | Generated |\n", "| 4 | lazy | 8 | Generated |\n", "| 5 | dog | 9 | Generated |\n", "| 6 | brown | 3 | Generated |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "---\n", "\n", "## English Continuation from a previous scanpath" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "**Input:** The quick brown fox jumps over the lazy dog\n", "\n", "**Scanpath:** [0, 1, 2, 3, 6, 9, 10, 4, 5]\n", "\n", "**Fixated sequence:** The → quick → brown → over → dog\n", "\n", "| Fixation | Word / Character | Position | Source |\n", "|---|---|---|---|\n", "| 1 | The | 1 | Observed |\n", "| 2 | quick | 2 | Observed |\n", "| 3 | brown | 3 | Observed |\n", "| 4 | over | 6 | Generated |\n", "| 5 | dog | 9 | Generated |" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "english_text = \"The quick brown fox jumps over the lazy dog\"\n", "english_previous_scanpath = [1,2,3]\n", "\n", "english_model = EyettentionRawTextInference(\n", " checkpoint_path=\"Eyettention/results/CELER/Eyettention_english.pth\",\n", " dataset=\"celer\",\n", " device=\"cpu\",\n", ")\n", "\n", "def run_english_raw_text_input(model, text, max_pred_len=7):\n", " torch.manual_seed(42)\n", "\n", " scanpath, _ = model.generate_from_english_text(\n", " text=text,\n", " max_pred_len=max_pred_len,\n", " previous_scanpath=None\n", " )\n", " \n", " return scanpath\n", "\n", "def run_english_previous_scanpath(model, text, max_pred_len=5, previous_scanpath=None):\n", " torch.manual_seed(42)\n", "\n", " scanpath, _ = model.generate_from_english_text(\n", " text=text,\n", " max_pred_len=max_pred_len,\n", " previous_scanpath= previous_scanpath\n", " )\n", "\n", " return scanpath\n", "\n", "\n", "display(Markdown(\"## English Raw-text generation\"))\n", "\n", "raw_text = run_english_raw_text_input(english_model, english_text)\n", "display_scanpath(english_text, scanpath=raw_text, dataset=\"celer\")\n", "\n", "\n", "display(Markdown(\"---\\n\\n## English Continuation from a previous scanpath\"))\n", "\n", "\n", "continuation = run_english_previous_scanpath(\n", " english_model,\n", " english_text,\n", " previous_scanpath=english_previous_scanpath,\n", ")\n", "\n", "display_scanpath(english_text, scanpath=continuation, dataset=\"celer\", previous_scanpath=english_previous_scanpath)" ] } ], "metadata": { "kernelspec": { "display_name": "scanpaths (3.10.7)", "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.7" } }, "nbformat": 4, "nbformat_minor": 5}scandl2_demo.ipynbAdded+943