{ "cells": [ { "cell_type": "code", "execution_count": 13, "id": "7fc1cfd9-803d-4f61-a21c-59033a36278e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "^C\n" ] } ], "source": [ "!pip install --timeout=1000 fastai gradio" ] }, { "cell_type": "code", "execution_count": 12, "id": "58c07559-6cbd-48d5-9557-aa84e905f924", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'fastai'", "output_type": "error", "traceback": [ "\u001b[1;31m--------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[12], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#|export\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfastai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mvision\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mall\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mgradio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mgr\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mis_cat\u001b[39m(x): \u001b[38;5;28;01mreturn\u001b[39;00m x[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39misupper()\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'fastai'" ] } ], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_cat(x): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": null, "id": "ce124f30-6301-4958-be14-a5592a4c4e49", "metadata": {}, "outputs": [], "source": [ "im = PILImage.create('dog.jpg')\n", "im.thumbnail(192, 192))\n", "im" ] }, { "cell_type": "code", "execution_count": null, "id": "3b9065f9-ac03-4876-b99f-d556c2b6efb6", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learn('model.pkl')" ] }, { "cell_type": "code", "execution_count": null, "id": "28b729e9-ddfd-469e-8998-50290889417d", "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR: Exception:\n", "Traceback (most recent call last):\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 438, in _error_catcher\n", " yield\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 561, in read\n", " data = self._fp_read(amt) if not fp_closed else b\"\"\n", " ^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 527, in _fp_read\n", " return self._fp.read(amt) if amt is not None else self._fp.read()\n", " ^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\cachecontrol\\filewrapper.py\", line 90, in read\n", " data = self.__fp.read(amt)\n", " ^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\http\\client.py\", line 466, in read\n", " s = self.fp.read(amt)\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\socket.py\", line 706, in readinto\n", " return self._sock.recv_into(b)\n", " ^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\ssl.py\", line 1278, in recv_into\n", " return self.read(nbytes, buffer)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\ssl.py\", line 1134, in read\n", " return self._sslobj.read(len, buffer)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", "TimeoutError: The read operation timed out\n", "\n", "During handling of the above exception, another exception occurred:\n", "\n", "Traceback (most recent call last):\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\cli\\base_command.py\", line 180, in exc_logging_wrapper\n", " status = run_func(*args)\n", " ^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\cli\\req_command.py\", line 248, in wrapper\n", " return func(self, options, args)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\commands\\install.py\", line 377, in run\n", " requirement_set = resolver.resolve(\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\resolver.py\", line 92, in resolve\n", " result = self._result = resolver.resolve(\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 546, in resolve\n", " state = resolution.resolve(requirements, max_rounds=max_rounds)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 427, in resolve\n", " failure_causes = self._attempt_to_pin_criterion(name)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 239, in _attempt_to_pin_criterion\n", " criteria = self._get_updated_criteria(candidate)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 230, in _get_updated_criteria\n", " self._add_to_criteria(criteria, requirement, parent=candidate)\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 173, in _add_to_criteria\n", " if not criterion.candidates:\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\structs.py\", line 156, in __bool__\n", " return bool(self._sequence)\n", " ^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py\", line 155, in __bool__\n", " return any(self)\n", " ^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py\", line 143, in \n", " return (c for c in iterator if id(c) not in self._incompatible_ids)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py\", line 47, in _iter_built\n", " candidate = func()\n", " ^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\factory.py\", line 206, in _make_candidate_from_link\n", " self._link_candidate_cache[link] = LinkCandidate(\n", " ^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 293, in __init__\n", " super().__init__(\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 156, in __init__\n", " self.dist = self._prepare()\n", " ^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 225, in _prepare\n", " dist = self._prepare_distribution()\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 304, in _prepare_distribution\n", " return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 538, in prepare_linked_requirement\n", " return self._prepare_linked_requirement(req, parallel_builds)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 609, in _prepare_linked_requirement\n", " local_file = unpack_url(\n", " ^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 166, in unpack_url\n", " file = get_http_url(\n", " ^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 107, in get_http_url\n", " from_path, content_type = download(link, temp_dir.path)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\network\\download.py\", line 147, in __call__\n", " for chunk in chunks:\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\network\\utils.py\", line 63, in response_chunks\n", " for chunk in response.raw.stream(\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 622, in stream\n", " data = self.read(amt=amt, decode_content=decode_content)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 560, in read\n", " with self._error_catcher():\n", " File \"C:\\Python311\\Lib\\contextlib.py\", line 155, in __exit__\n", " self.gen.throw(typ, value, traceback)\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 443, in _error_catcher\n", " raise ReadTimeoutError(self._pool, None, \"Read timed out.\")\n", "pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.\n", "ERROR: Exception:\n", "Traceback (most recent call last):\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 438, in _error_catcher\n", " yield\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 561, in read\n", " data = self._fp_read(amt) if not fp_closed else b\"\"\n", " ^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 527, in _fp_read\n", " return self._fp.read(amt) if amt is not None else self._fp.read()\n", " ^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\cachecontrol\\filewrapper.py\", line 90, in read\n", " data = self.__fp.read(amt)\n", " ^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\http\\client.py\", line 466, in read\n", " s = self.fp.read(amt)\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\socket.py\", line 706, in readinto\n", " return self._sock.recv_into(b)\n", " ^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\ssl.py\", line 1278, in recv_into\n", " return self.read(nbytes, buffer)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\ssl.py\", line 1134, in read\n", " return self._sslobj.read(len, buffer)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", "TimeoutError: The read operation timed out\n", "\n", "During handling of the above exception, another exception occurred:\n", "\n", "Traceback (most recent call last):\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\cli\\base_command.py\", line 180, in exc_logging_wrapper\n", " status = run_func(*args)\n", " ^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\cli\\req_command.py\", line 248, in wrapper\n", " return func(self, options, args)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\commands\\install.py\", line 377, in run\n", " requirement_set = resolver.resolve(\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\resolver.py\", line 92, in resolve\n", " result = self._result = resolver.resolve(\n", " ^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 546, in resolve\n", " state = resolution.resolve(requirements, max_rounds=max_rounds)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 427, in resolve\n", " failure_causes = self._attempt_to_pin_criterion(name)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py\", line 237, in _attempt_to_pin_criterion\n", " for candidate in criterion.candidates:\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py\", line 143, in \n", " return (c for c in iterator if id(c) not in self._incompatible_ids)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py\", line 47, in _iter_built\n", " candidate = func()\n", " ^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\factory.py\", line 206, in _make_candidate_from_link\n", " self._link_candidate_cache[link] = LinkCandidate(\n", " ^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 293, in __init__\n", " super().__init__(\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 156, in __init__\n", " self.dist = self._prepare()\n", " ^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 225, in _prepare\n", " dist = self._prepare_distribution()\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\candidates.py\", line 304, in _prepare_distribution\n", " return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 538, in prepare_linked_requirement\n", " return self._prepare_linked_requirement(req, parallel_builds)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 609, in _prepare_linked_requirement\n", " local_file = unpack_url(\n", " ^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 166, in unpack_url\n", " file = get_http_url(\n", " ^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 107, in get_http_url\n", " from_path, content_type = download(link, temp_dir.path)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\network\\download.py\", line 147, in __call__\n", " for chunk in chunks:\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_internal\\network\\utils.py\", line 63, in response_chunks\n", " for chunk in response.raw.stream(\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 622, in stream\n", " data = self.read(amt=amt, decode_content=decode_content)\n", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 560, in read\n", " with self._error_catcher():\n", " File \"C:\\Python311\\Lib\\contextlib.py\", line 155, in __exit__\n", " self.gen.throw(typ, value, traceback)\n", " File \"C:\\Python311\\Lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 443, in _error_catcher\n", " raise ReadTimeoutError(self._pool, None, \"Read timed out.\")\n", "pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.\n" ] } ], "source": [ "%time learn.predict(im)" ] }, { "cell_type": "code", "execution_count": null, "id": "5c3c9a01-fcb7-46c7-9fef-b03bb95c48d2", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('Dog', 'Cat')\n", "\n", "def classify_image(img):\n", " pred, idx, probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs))" ] }, { "cell_type": "code", "execution_count": null, "id": "55dd515e-3648-432c-9d71-e3322fcff7de", "metadata": {}, "outputs": [], "source": [ "classify_image(im)" ] }, { "cell_type": "code", "execution_count": null, "id": "23aa42c6-24ce-4b3a-a0c1-24a35f67251e", "metadata": {}, "outputs": [], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192, 192))\n", "label = gr.outputs.Label()\n", "examples = ['dog.jpg', 'cat.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": 11, "id": "3959f648-7188-48cc-ab2a-bf8157c3b80b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "The system cannot find the file specified.\n" ] } ], "source": [ "!pip install -U --no-deps nbdev<2 fastcore" ] }, { "cell_type": "code", "execution_count": 7, "id": "5fd6a6e9-60c1-44f5-99de-03dca35aef62", "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "cannot import name 'notebook2script' from 'nbdev.export' (C:\\Users\\user\\AppData\\Roaming\\Python\\Python311\\site-packages\\nbdev\\export.py)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m notebook2script\n", "\u001b[1;31mImportError\u001b[0m: cannot import name 'notebook2script' from 'nbdev.export' (C:\\Users\\user\\AppData\\Roaming\\Python\\Python311\\site-packages\\nbdev\\export.py)" ] } ], "source": [ "from nbdev.export import notebook2script" ] }, { "cell_type": "code", "execution_count": 6, "id": "d24d921a-f65f-45b2-aa28-fe7035107c86", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'notebook2script' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mnotebook2script\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapp.ipynb\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", "\u001b[1;31mNameError\u001b[0m: name 'notebook2script' is not defined" ] } ], "source": [ "notebook2script('app.ipynb')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }