|
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 4, |
|
"id": "e8f0c0de", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Defaulting to user installation because normal site-packages is not writeable\n", |
|
"Collecting ruamel.yaml\n", |
|
" Downloading ruamel.yaml-0.17.32-py3-none-any.whl (112 kB)\n", |
|
" -------------------------------------- 112.2/112.2 kB 1.6 MB/s eta 0:00:00\n", |
|
"Collecting ruamel.yaml.clib>=0.2.7\n", |
|
" Downloading ruamel.yaml.clib-0.2.7-cp39-cp39-win_amd64.whl (118 kB)\n", |
|
" -------------------------------------- 118.4/118.4 kB 6.8 MB/s eta 0:00:00\n", |
|
"Installing collected packages: ruamel.yaml.clib, ruamel.yaml\n", |
|
"Successfully installed ruamel.yaml-0.17.32 ruamel.yaml.clib-0.2.7\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"!pip install ruamel.yaml" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 5, |
|
"id": "1c445cb6", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"!pip install -Uqq fastai\n", |
|
"from fastai import *" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 10, |
|
"id": "60065e37", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Defaulting to user installation because normal site-packages is not writeable\n", |
|
"Requirement already satisfied: torch in c:\\users\\victo\\appdata\\roaming\\python\\python39\\site-packages (2.0.1)\n", |
|
"Requirement already satisfied: filelock in j:\\anaconda3\\lib\\site-packages (from torch) (3.6.0)\n", |
|
"Requirement already satisfied: typing-extensions in c:\\users\\victo\\appdata\\roaming\\python\\python39\\site-packages (from torch) (4.7.1)\n", |
|
"Requirement already satisfied: jinja2 in j:\\anaconda3\\lib\\site-packages (from torch) (2.11.3)\n", |
|
"Requirement already satisfied: networkx in j:\\anaconda3\\lib\\site-packages (from torch) (2.8.4)\n", |
|
"Requirement already satisfied: sympy in j:\\anaconda3\\lib\\site-packages (from torch) (1.10.1)\n", |
|
"Requirement already satisfied: MarkupSafe>=0.23 in j:\\anaconda3\\lib\\site-packages (from jinja2->torch) (2.0.1)\n", |
|
"Requirement already satisfied: mpmath>=0.19 in j:\\anaconda3\\lib\\site-packages (from sympy->torch) (1.2.1)\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"!pip install torch" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 6, |
|
"id": "8aecaaa1", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"!pip install -Uqq scikit-image\n", |
|
"!pip install -Uqq gradio" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 23, |
|
"id": "96a3aa78", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"!pip install -Uqq pathlib" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 24, |
|
"id": "070bdc04", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"from fastai.vision.all import *\n", |
|
"import pathlib\n", |
|
"temp = pathlib.PosixPath\n", |
|
"pathlib.PosixPath = pathlib.WindowsPath\n", |
|
"learn = load_learner('C:/Users/Victo/Downloads/movie_era_model.pkl')\n", |
|
"pathlib.PosixPath = temp" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 25, |
|
"id": "aef5f09b", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"labels = learn.dls.vocab\n", |
|
"def predict(img):\n", |
|
" img = PILImage.create(img)\n", |
|
" pred,pred_idx,probs = learn.predict(img)\n", |
|
" return {labels[i]: float(probs[i]) for i in range(len(labels))}" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 32, |
|
"id": "eae89582", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import skimage\n", |
|
"title = \"Movie Poster Era Classifier\"\n", |
|
"description = \"This is a study project for models deployment via Gradio and HuggingFace Spaces. The app takes a movie poster as an input, returning probabilities of the poster being drawn either in 1900s-1960s, 1970s-1990s or later on. Pretrained resnet 101 was taken as a learner with being fine-tuned on 1500 posters of each period, retreived from DuckDuckGo search queries. Quality of the resulting model is quite low with 0.35 error rate after 11 epochs, but the aim was more to play around with Gradio, rather than crating working movie poster classifier. Full code for the model and deployment is featured on my GitHub page, linked below\"\n", |
|
"examples = ['C:\\\\Users\\\\Victo\\\\Downloads\\\\example_1.jpeg', 'C:\\\\Users\\\\Victo\\\\Downloads\\\\example_2.jpeg', 'C:\\\\Users\\\\Victo\\\\Downloads\\\\example_3.jpeg']\n", |
|
"article=\"<p style='text-align: center'><a href='https://github.com/VictorPakholkov/movie_era_posters_detection_gradio_app' target='_blank'>Github repo</a></p>\"\n", |
|
"interpretation='default'\n", |
|
"enable_queue=True" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 33, |
|
"id": "602aa885", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stderr", |
|
"output_type": "stream", |
|
"text": [ |
|
"C:\\Users\\Victo\\AppData\\Local\\Temp\\ipykernel_8184\\3255000909.py:3: GradioDeprecationWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", |
|
" inputs=gr.inputs.Image(shape=(512, 512)),\n", |
|
"C:\\Users\\Victo\\AppData\\Local\\Temp\\ipykernel_8184\\3255000909.py:3: GradioDeprecationWarning: `optional` parameter is deprecated, and it has no effect\n", |
|
" inputs=gr.inputs.Image(shape=(512, 512)),\n", |
|
"C:\\Users\\Victo\\AppData\\Local\\Temp\\ipykernel_8184\\3255000909.py:4: GradioDeprecationWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", |
|
" outputs=gr.outputs.Label(num_top_classes=3),\n", |
|
"C:\\Users\\Victo\\AppData\\Local\\Temp\\ipykernel_8184\\3255000909.py:4: GradioUnusedKwargWarning: You have unused kwarg parameters in Label, please remove them: {'type': 'auto'}\n", |
|
" outputs=gr.outputs.Label(num_top_classes=3),\n", |
|
"C:\\Users\\Victo\\AppData\\Local\\Temp\\ipykernel_8184\\3255000909.py:2: GradioDeprecationWarning: `enable_queue` is deprecated in `Interface()`, please use it within `launch()` instead.\n", |
|
" gr.Interface(fn=predict,\n" |
|
] |
|
}, |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Running on local URL: http://127.0.0.1:7860\n", |
|
"Running on public URL: https://6295736d4372057f9f.gradio.live\n", |
|
"\n", |
|
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" |
|
] |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [ |
|
"<div><iframe src=\"https://6295736d4372057f9f.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/plain": [] |
|
}, |
|
"execution_count": 33, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [ |
|
"\n", |
|
"<style>\n", |
|
" /* Turns off some styling */\n", |
|
" progress {\n", |
|
" /* gets rid of default border in Firefox and Opera. */\n", |
|
" border: none;\n", |
|
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", |
|
" background-size: auto;\n", |
|
" }\n", |
|
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", |
|
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", |
|
" }\n", |
|
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", |
|
" background: #F44336;\n", |
|
" }\n", |
|
"</style>\n" |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [ |
|
"\n", |
|
"<style>\n", |
|
" /* Turns off some styling */\n", |
|
" progress {\n", |
|
" /* gets rid of default border in Firefox and Opera. */\n", |
|
" border: none;\n", |
|
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", |
|
" background-size: auto;\n", |
|
" }\n", |
|
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", |
|
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", |
|
" }\n", |
|
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", |
|
" background: #F44336;\n", |
|
" }\n", |
|
"</style>\n" |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [ |
|
"\n", |
|
"<style>\n", |
|
" /* Turns off some styling */\n", |
|
" progress {\n", |
|
" /* gets rid of default border in Firefox and Opera. */\n", |
|
" border: none;\n", |
|
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", |
|
" background-size: auto;\n", |
|
" }\n", |
|
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", |
|
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", |
|
" }\n", |
|
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", |
|
" background: #F44336;\n", |
|
" }\n", |
|
"</style>\n" |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [ |
|
"\n", |
|
"<style>\n", |
|
" /* Turns off some styling */\n", |
|
" progress {\n", |
|
" /* gets rid of default border in Firefox and Opera. */\n", |
|
" border: none;\n", |
|
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n", |
|
" background-size: auto;\n", |
|
" }\n", |
|
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n", |
|
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n", |
|
" }\n", |
|
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n", |
|
" background: #F44336;\n", |
|
" }\n", |
|
"</style>\n" |
|
], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
}, |
|
{ |
|
"data": { |
|
"text/html": [], |
|
"text/plain": [ |
|
"<IPython.core.display.HTML object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
} |
|
], |
|
"source": [ |
|
"import gradio as gr\n", |
|
"gr.Interface(fn=predict,\n", |
|
" inputs=gr.inputs.Image(shape=(512, 512)),\n", |
|
" outputs=gr.outputs.Label(num_top_classes=3),\n", |
|
" title=title,\n", |
|
" description=description,\n", |
|
" article=article,\n", |
|
" examples=examples,\n", |
|
" interpretation=interpretation,\n", |
|
" enable_queue=enable_queue).launch(share=True)" |
|
] |
|
} |
|
], |
|
"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.9.13" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
} |
|
|