From a85afefb08ae847039acb957d2a581f3c5294d6f Mon Sep 17 00:00:00 2001 From: Yaroslav Tarkan Date: Tue, 3 Sep 2024 17:25:41 +0300 Subject: [PATCH] Add Gradio helpers - part 8 (#2337) Ticket: CVS-147626 Notebooks: 1. speculative-sampling/speculative-sampling.ipynb 2. stable-audio/stable-audio.ipynb 3. surya-line-level-text-detection/surya-line-level-text-detection.ipynb - minimal Gradio demo, helper not needed 4. table-question-answering/table-question-answering.ipynb 5. tiny-sd-image-generation/tiny-sd-image-generation.ipynb 6. triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb 7. typo-detector/typo-detector.ipynb - no gradio demo 8. wuerstchen-image-generation/wuerstchen-image-generation.ipynb - minimal Gradio demo, helper not needed 9. yolov10-optimization/yolov10-optimization.ipynb 10. zeroscope-text2video/zeroscope-text2video.ipynb --- .ci/ignore_treon_docker.txt | 1 + Dockerfile | 1 + .../speculative-sampling/gradio_helper.py | 29 +++++ .../speculative-sampling.ipynb | 48 +++++---- notebooks/stable-audio/gradio_helper.py | 27 +++++ notebooks/stable-audio/stable-audio.ipynb | 76 ++++--------- .../surya-line-level-text-detection.ipynb | 16 ++- .../table-question-answering/gradio_helper.py | 50 +++++++++ .../table-question-answering.ipynb | 89 +++++++-------- .../tiny-sd-image-generation/gradio_helper.py | 76 +++++++++++++ .../tiny-sd-image-generation.ipynb | 98 +++++------------ .../gradio_helper.py | 61 +++++++++++ .../triposr-3d-reconstruction.ipynb | 101 ++++++------------ notebooks/typo-detector/typo-detector.ipynb | 2 +- .../wuerstchen-image-generation.ipynb | 23 ++-- .../yolov10-optimization/gradio_helper.py | 52 +++++++++ .../yolov10-optimization.ipynb | 97 ++++++----------- .../zeroscope-text2video/gradio_helper.py | 21 ++++ .../zeroscope-text2video.ipynb | 58 +++++----- 19 files changed, 561 insertions(+), 365 deletions(-) create mode 100644 notebooks/speculative-sampling/gradio_helper.py create mode 100644 notebooks/stable-audio/gradio_helper.py create mode 100644 notebooks/table-question-answering/gradio_helper.py create mode 100644 notebooks/tiny-sd-image-generation/gradio_helper.py create mode 100644 notebooks/triposr-3d-reconstruction/gradio_helper.py create mode 100644 notebooks/yolov10-optimization/gradio_helper.py create mode 100644 notebooks/zeroscope-text2video/gradio_helper.py diff --git a/.ci/ignore_treon_docker.txt b/.ci/ignore_treon_docker.txt index 9a04786f572..7dd84b3c04d 100644 --- a/.ci/ignore_treon_docker.txt +++ b/.ci/ignore_treon_docker.txt @@ -76,3 +76,4 @@ notebooks/explainable-ai-3-map-interpretation/explainable-ai-3-map-interpretatio notebooks/phi-3-vision/phi-3-vision.ipynb notebooks/triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb notebooks/llm-agent-react/llm-agent-rag-llamaindex.ipynb +notebooks/stable-audio/stable-audio.ipynb diff --git a/Dockerfile b/Dockerfile index c43fc4fcbdf..043df6d3588 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,7 @@ RUN . /etc/os-release && \ level-zero intel-level-zero-gpu level-zero-devel && \ rpm -ivh https://dl.fedoraproject.org/pub/epel/9/Everything/x86_64/Packages/c/clinfo-3.0.21.02.21-4.el9.x86_64.rpm \ https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/ocl-icd-2.2.13-4.el9.x86_64.rpm && \ + yum install -y libsndfile && \ dnf clean all -y && \ rm -rf /var/cache/dnf/* diff --git a/notebooks/speculative-sampling/gradio_helper.py b/notebooks/speculative-sampling/gradio_helper.py new file mode 100644 index 00000000000..f107ef63983 --- /dev/null +++ b/notebooks/speculative-sampling/gradio_helper.py @@ -0,0 +1,29 @@ +from typing import Callable +import gradio as gr + + +main_model_id = "meta-llama/Llama-2-7b-chat-hf" +draft_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" + + +def make_demo(fn: Callable): + with gr.Blocks() as demo: + gr.Markdown( + f""" + # Speculative Sampling Demo + ## The output will show a comparison of Autoregressive Sampling vs Speculative Sampling + - Main Model: {main_model_id} + - Draft Model: {draft_model_id} + - K = 5 + """ + ) + with gr.Row(): + input = gr.Textbox( + value="Alan Turing was a", + placeholder="THIS CANNOT BE EMPTY", + label="Input Prompt", + ) + output = gr.Textbox(label="Output") + btn = gr.Button("Run") + btn.click(fn=fn, inputs=input, outputs=output) + return demo diff --git a/notebooks/speculative-sampling/speculative-sampling.ipynb b/notebooks/speculative-sampling/speculative-sampling.ipynb index 1ad52549b76..24c3f65f124 100644 --- a/notebooks/speculative-sampling/speculative-sampling.ipynb +++ b/notebooks/speculative-sampling/speculative-sampling.ipynb @@ -162,7 +162,6 @@ "source": [ "import time\n", "import numpy as np\n", - "import gradio as gr\n", "import openvino as ov" ] }, @@ -516,27 +515,32 @@ "metadata": {}, "outputs": [], "source": [ - "with gr.Blocks() as demo:\n", - " gr.Markdown(\n", - " f\"\"\"\n", - " # Speculative Sampling Demo\n", - " ## The output will show a comparison of Autoregressive Sampling vs Speculative Sampling\n", - " - Main Model: {main_model_id}\n", - " - Draft Model: {draft_model_id}\n", - " - K = 5\n", - " \"\"\"\n", - " )\n", - " with gr.Row():\n", - " inp = gr.Textbox(\n", - " \"Alan Turing was a\",\n", - " placeholder=\"THIS CANNOT BE EMPTY\",\n", - " label=\"Input Prompt\",\n", - " )\n", - " out = gr.Textbox(label=\"Output\")\n", - " btn = gr.Button(\"Run\")\n", - " btn.click(fn=main, inputs=inp, outputs=out)\n", - "\n", - "demo.launch()" + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/speculative-sampling/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", + "\n", + "from gradio_helper import make_demo\n", + "\n", + "demo = make_demo(fn=main)\n", + "\n", + "try:\n", + " demo.launch(debug=True)\n", + "except Exception:\n", + " demo.launch(share=True, debug=True)\n", + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba8aa6cf", + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/stable-audio/gradio_helper.py b/notebooks/stable-audio/gradio_helper.py new file mode 100644 index 00000000000..3850b0b24a2 --- /dev/null +++ b/notebooks/stable-audio/gradio_helper.py @@ -0,0 +1,27 @@ +from typing import Callable +import gradio as gr +import numpy as np + + +MAX_SEED = np.iinfo(np.int32).max + + +def make_demo(fn: Callable): + demo = gr.Interface( + fn=fn, + inputs=[ + gr.Textbox(label="Text Prompt"), + gr.Slider(1, 47, label="Total seconds", step=1, value=10), + gr.Slider(10, 100, label="Number of steps", step=1, value=100), + gr.Slider(0, MAX_SEED, label="Seed", step=1), + ], + outputs=["audio"], + examples=[ + ["128 BPM tech house drum loop"], + ["Blackbird song, summer, dusk in the forest"], + ["Rock beat played in a treated studio, session drumming on an acoustic kit"], + ["Calmful melody and nature sounds for restful sleep"], + ], + allow_flagging="never", + ) + return demo diff --git a/notebooks/stable-audio/stable-audio.ipynb b/notebooks/stable-audio/stable-audio.ipynb index a43a148799f..07e321a403e 100644 --- a/notebooks/stable-audio/stable-audio.ipynb +++ b/notebooks/stable-audio/stable-audio.ipynb @@ -309,10 +309,7 @@ ], "source": [ "from pathlib import Path\n", - "\n", - "import numpy as np\n", "import torch\n", - "\n", "from nncf import compress_weights, CompressWeightsMode\n", "import openvino as ov\n", "\n", @@ -957,69 +954,38 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "eeff99cd-d90d-4439-8c82-351a893d1fd0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Running on local URL: http://127.0.0.1:7860\n", - "\n", - "To create a public link, set `share=True` in `launch()`.\n" - ] - }, - { - "data": { - "text/html": [ - "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Keyboard interruption in main thread... closing server.\n" - ] - } - ], + "outputs": [], "source": [ - "import gradio as gr\n", - "\n", - "\n", - "demo = gr.Interface(\n", - " _generate,\n", - " inputs=[\n", - " gr.Textbox(label=\"Text Prompt\"),\n", - " gr.Slider(1, 47, label=\"Total seconds\", step=1, value=10),\n", - " gr.Slider(10, 100, label=\"Number of steps\", step=1, value=100),\n", - " gr.Slider(0, np.iinfo(np.int32).max, label=\"Seed\", step=1),\n", - " ],\n", - " outputs=[\"audio\"],\n", - " examples=[\n", - " [\"128 BPM tech house drum loop\"],\n", - " [\"Blackbird song, summer, dusk in the forest\"],\n", - " [\"Rock beat played in a treated studio, session drumming on an acoustic kit\"],\n", - " [\"Calmful melody and nature sounds for restful sleep\"],\n", - " ],\n", - " allow_flagging=\"never\",\n", - ")\n", + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/stable-audio/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", + "\n", + "from gradio_helper import make_demo\n", + "\n", + "demo = make_demo(fn=_generate)\n", + "\n", "try:\n", " demo.launch(debug=True)\n", "except Exception:\n", " demo.launch(share=True, debug=True)\n", - "\n", "# If you are launching remotely, specify server_name and server_port\n", "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "492f0163", + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" + ] } ], "metadata": { diff --git a/notebooks/surya-line-level-text-detection/surya-line-level-text-detection.ipynb b/notebooks/surya-line-level-text-detection/surya-line-level-text-detection.ipynb index cd4793c261f..8295e002fe2 100644 --- a/notebooks/surya-line-level-text-detection/surya-line-level-text-detection.ipynb +++ b/notebooks/surya-line-level-text-detection/surya-line-level-text-detection.ipynb @@ -754,9 +754,19 @@ " demo.launch(debug=True, height=1000)\n", "except Exception:\n", " demo.launch(share=True, debug=True, height=1000)\n", - "# if you are launching remotely, specify server_name and server_port\n", - "# demo.launch(server_name='your server name', server_port='server port in int')\n", - "# Read more in the docs: https://gradio.app/docs/" + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/table-question-answering/gradio_helper.py b/notebooks/table-question-answering/gradio_helper.py new file mode 100644 index 00000000000..9de037f5fa5 --- /dev/null +++ b/notebooks/table-question-answering/gradio_helper.py @@ -0,0 +1,50 @@ +from pathlib import Path +from typing import Callable +import gradio as gr +import pandas as pd +import requests + +csv_file_name = "eu_city_population_top10.csv" + +if not Path(csv_file_name).exists(): + r = requests.get("https://github.com/openvinotoolkit/openvino_notebooks/files/13215688/eu_city_population_top10.csv") + with open(csv_file_name, "w") as f: + f.write(r.text) + + +def display_table(csv_file_name): + table = pd.read_csv(csv_file_name.name, delimiter=",") + table = table.astype(str) + return table + + +def make_demo(fn: Callable): + with gr.Blocks(title="TAPAS Table Question Answering") as demo: + with gr.Row(): + with gr.Column(): + search_query = gr.Textbox(label="Search query") + csv_file = gr.File(label="CSV file") + infer_button = gr.Button("Submit", variant="primary") + with gr.Column(): + answer = gr.Textbox(label="Result") + result_csv_file = gr.Dataframe(label="All data") + + examples = [ + [ + "What is the city with the highest population that is not a capital?", + csv_file_name, + ], + ["In which country is Madrid?", csv_file_name], + [ + "In which cities is the population greater than 2,000,000?", + csv_file_name, + ], + ] + gr.Examples(examples, inputs=[search_query, csv_file]) + + # Callbacks + csv_file.upload(display_table, inputs=csv_file, outputs=result_csv_file) + csv_file.select(display_table, inputs=csv_file, outputs=result_csv_file) + csv_file.change(display_table, inputs=csv_file, outputs=result_csv_file) + infer_button.click(fn=fn, inputs=[search_query, csv_file], outputs=[answer, result_csv_file]) + return demo diff --git a/notebooks/table-question-answering/table-question-answering.ipynb b/notebooks/table-question-answering/table-question-answering.ipynb index fbd95d260e5..0d75c37ae0d 100644 --- a/notebooks/table-question-answering/table-question-answering.ipynb +++ b/notebooks/table-question-answering/table-question-answering.ipynb @@ -504,31 +504,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false, - "jupyter": { - "outputs_hidden": false - } - }, + "metadata": {}, "outputs": [], "source": [ - "import requests\n", - "\n", - "import gradio as gr\n", "import pandas as pd\n", "\n", - "r = requests.get(\"https://github.com/openvinotoolkit/openvino_notebooks/files/13215688/eu_city_population_top10.csv\")\n", - "\n", - "with open(\"eu_city_population_top10.csv\", \"w\") as f:\n", - " f.write(r.text)\n", - "\n", - "\n", - "def display_table(csv_file_name):\n", - " table = pd.read_csv(csv_file_name.name, delimiter=\",\")\n", - " table = table.astype(str)\n", - "\n", - " return table\n", - "\n", "\n", "def highlight_answers(x, coordinates):\n", " highlighted_table = pd.DataFrame(\"\", index=x.index, columns=x.columns)\n", @@ -545,42 +525,45 @@ " result = tqa(table=table, query=query)\n", " table = table.style.apply(highlight_answers, axis=None, coordinates=result[\"coordinates\"])\n", "\n", - " return result[\"answer\"], table\n", - "\n", - "\n", - "with gr.Blocks(title=\"TAPAS Table Question Answering\") as demo:\n", - " with gr.Row():\n", - " with gr.Column():\n", - " search_query = gr.Textbox(label=\"Search query\")\n", - " csv_file = gr.File(label=\"CSV file\")\n", - " infer_button = gr.Button(\"Submit\", variant=\"primary\")\n", - " with gr.Column():\n", - " answer = gr.Textbox(label=\"Result\")\n", - " result_csv_file = gr.Dataframe(label=\"All data\")\n", - "\n", - " examples = [\n", - " [\n", - " \"What is the city with the highest population that is not a capital?\",\n", - " \"eu_city_population_top10.csv\",\n", - " ],\n", - " [\"In which country is Madrid?\", \"eu_city_population_top10.csv\"],\n", - " [\n", - " \"In which cities is the population greater than 2,000,000?\",\n", - " \"eu_city_population_top10.csv\",\n", - " ],\n", - " ]\n", - " gr.Examples(examples, inputs=[search_query, csv_file])\n", - "\n", - " # Callbacks\n", - " csv_file.upload(display_table, inputs=csv_file, outputs=result_csv_file)\n", - " csv_file.select(display_table, inputs=csv_file, outputs=result_csv_file)\n", - " csv_file.change(display_table, inputs=csv_file, outputs=result_csv_file)\n", - " infer_button.click(infer, inputs=[search_query, csv_file], outputs=[answer, result_csv_file])\n", + " return result[\"answer\"], table" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/table-question-answering/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", + "\n", + "from gradio_helper import make_demo\n", + "\n", + "demo = make_demo(fn=infer)\n", "\n", "try:\n", " demo.queue().launch(debug=True)\n", "except Exception:\n", - " demo.queue().launch(share=True, debug=True)" + " demo.queue().launch(share=True, debug=True)\n", + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/tiny-sd-image-generation/gradio_helper.py b/notebooks/tiny-sd-image-generation/gradio_helper.py new file mode 100644 index 00000000000..a1989503398 --- /dev/null +++ b/notebooks/tiny-sd-image-generation/gradio_helper.py @@ -0,0 +1,76 @@ +from pathlib import Path +from typing import Callable +import gradio as gr +import requests + +sample_image_name = "tower.jpg" + +if not Path(sample_image_name).exists(): + r = requests.get("https://storage.openvinotoolkit.org/repositories/openvino_notebooks/data/data/image/tower.jpg") + with open(sample_image_name, "wb") as f: + f.write(r.content) + + +def make_demo(text_to_text_fn: Callable, image_to_image_fn: Callable): + with gr.Blocks() as demo: + with gr.Tab("Text-to-Image generation"): + with gr.Row(): + with gr.Column(): + text_input = gr.Textbox(lines=3, label="Positive prompt") + negative_text_input = gr.Textbox(lines=3, label="Negative prompt") + seed_input = gr.Slider(0, 10000000, value=751, label="Seed") + steps_input = gr.Slider(1, 50, value=20, step=1, label="Steps") + output = gr.Image(label="Result", type="pil") + sample_text = "futuristic synthwave city, retro sunset, crystals, spires, volumetric lighting, studio Ghibli style, rendered in unreal engine with clean details" + sample_text2 = "RAW studio photo of tiny cute happy cat in a yellow raincoat in the woods, rain, a character portrait, soft lighting, high resolution, photo realistic, extremely detailed" + negative_sample_text = "" + negative_sample_text2 = "bad anatomy, blurry, noisy, jpeg artifacts, low quality, geometry, mutation, disgusting. ugly" + btn = gr.Button() + btn.click( + fn=text_to_text_fn, + inputs=[text_input, negative_text_input, seed_input, steps_input], + outputs=output, + ) + gr.Examples( + [ + [sample_text, negative_sample_text, 42, 20], + [sample_text2, negative_sample_text2, 1561, 25], + ], + [text_input, negative_text_input, seed_input, steps_input], + ) + with gr.Tab("Image-to-Image generation"): + with gr.Row(): + with gr.Column(): + i2i_input = gr.Image(label="Image", type="pil") + i2i_text_input = gr.Textbox(lines=3, label="Text") + i2i_negative_text_input = gr.Textbox(lines=3, label="Negative prompt") + i2i_seed_input = gr.Slider(0, 10000000, value=42, label="Seed") + i2i_steps_input = gr.Slider(1, 50, value=10, step=1, label="Steps") + strength_input = gr.Slider(0, 1, value=0.5, label="Strength") + i2i_output = gr.Image(label="Result", type="pil") + i2i_btn = gr.Button() + sample_i2i_text = "amazing watercolor painting" + i2i_btn.click( + fn=image_to_image_fn, + inputs=[ + i2i_input, + i2i_text_input, + i2i_negative_text_input, + i2i_seed_input, + i2i_steps_input, + strength_input, + ], + outputs=i2i_output, + ) + gr.Examples( + [[sample_image_name, sample_i2i_text, "", 6400023, 40, 0.3]], + [ + i2i_input, + i2i_text_input, + i2i_negative_text_input, + i2i_seed_input, + i2i_steps_input, + strength_input, + ], + ) + return demo diff --git a/notebooks/tiny-sd-image-generation/tiny-sd-image-generation.ipynb b/notebooks/tiny-sd-image-generation/tiny-sd-image-generation.ipynb index 6e1abd34897..8f1bf4a584d 100644 --- a/notebooks/tiny-sd-image-generation/tiny-sd-image-generation.ipynb +++ b/notebooks/tiny-sd-image-generation/tiny-sd-image-generation.ipynb @@ -1268,10 +1268,6 @@ "source": [ "import gradio as gr\n", "\n", - "sample_img_url = \"https://storage.openvinotoolkit.org/repositories/openvino_notebooks/data/data/image/tower.jpg\"\n", - "\n", - "img = load_image(sample_img_url).save(\"tower.jpg\")\n", - "\n", "\n", "def generate_from_text(text, negative_text, seed, num_steps, _=gr.Progress(track_tqdm=True)):\n", " result = ov_pipe(text, negative_prompt=negative_text, num_inference_steps=num_steps, seed=seed)\n", @@ -1287,80 +1283,40 @@ " seed=seed,\n", " strength=strength,\n", " )\n", - " return result[\"sample\"][0]\n", + " return result[\"sample\"][0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/tiny-sd-image-generation/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", "\n", + "from gradio_helper import make_demo\n", "\n", - "with gr.Blocks() as demo:\n", - " with gr.Tab(\"Text-to-Image generation\"):\n", - " with gr.Row():\n", - " with gr.Column():\n", - " text_input = gr.Textbox(lines=3, label=\"Positive prompt\")\n", - " negative_text_input = gr.Textbox(lines=3, label=\"Negative prompt\")\n", - " seed_input = gr.Slider(0, 10000000, value=751, label=\"Seed\")\n", - " steps_input = gr.Slider(1, 50, value=20, step=1, label=\"Steps\")\n", - " out = gr.Image(label=\"Result\", type=\"pil\")\n", - " sample_text = (\n", - " \"futuristic synthwave city, retro sunset, crystals, spires, volumetric lighting, studio Ghibli style, rendered in unreal engine with clean details\"\n", - " )\n", - " sample_text2 = \"RAW studio photo of tiny cute happy cat in a yellow raincoat in the woods, rain, a character portrait, soft lighting, high resolution, photo realistic, extremely detailed\"\n", - " negative_sample_text = \"\"\n", - " negative_sample_text2 = \"bad anatomy, blurry, noisy, jpeg artifacts, low quality, geometry, mutation, disgusting. ugly\"\n", - " btn = gr.Button()\n", - " btn.click(\n", - " generate_from_text,\n", - " [text_input, negative_text_input, seed_input, steps_input],\n", - " out,\n", - " )\n", - " gr.Examples(\n", - " [\n", - " [sample_text, negative_sample_text, 42, 20],\n", - " [sample_text2, negative_sample_text2, 1561, 25],\n", - " ],\n", - " [text_input, negative_text_input, seed_input, steps_input],\n", - " )\n", - " with gr.Tab(\"Image-to-Image generation\"):\n", - " with gr.Row():\n", - " with gr.Column():\n", - " i2i_input = gr.Image(label=\"Image\", type=\"pil\")\n", - " i2i_text_input = gr.Textbox(lines=3, label=\"Text\")\n", - " i2i_negative_text_input = gr.Textbox(lines=3, label=\"Negative prompt\")\n", - " i2i_seed_input = gr.Slider(0, 10000000, value=42, label=\"Seed\")\n", - " i2i_steps_input = gr.Slider(1, 50, value=10, step=1, label=\"Steps\")\n", - " strength_input = gr.Slider(0, 1, value=0.5, label=\"Strength\")\n", - " i2i_out = gr.Image(label=\"Result\", type=\"pil\")\n", - " i2i_btn = gr.Button()\n", - " sample_i2i_text = \"amazing watercolor painting\"\n", - " i2i_btn.click(\n", - " generate_from_image,\n", - " [\n", - " i2i_input,\n", - " i2i_text_input,\n", - " i2i_negative_text_input,\n", - " i2i_seed_input,\n", - " i2i_steps_input,\n", - " strength_input,\n", - " ],\n", - " i2i_out,\n", - " )\n", - " gr.Examples(\n", - " [[\"tower.jpg\", sample_i2i_text, \"\", 6400023, 40, 0.3]],\n", - " [\n", - " i2i_input,\n", - " i2i_text_input,\n", - " i2i_negative_text_input,\n", - " i2i_seed_input,\n", - " i2i_steps_input,\n", - " strength_input,\n", - " ],\n", - " )\n", + "demo = make_demo(text_to_text_fn=generate_from_text, image_to_image_fn=generate_from_image)\n", "\n", "try:\n", " demo.queue().launch(debug=True)\n", "except Exception:\n", " demo.queue().launch(share=True, debug=True)\n", - "# if you are launching remotely, specify server_name and server_port\n", - "# demo.launch(server_name='your server name', server_port='server port in int')\n", - "# Read more in the docs: https://gradio.app/docs/" + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/triposr-3d-reconstruction/gradio_helper.py b/notebooks/triposr-3d-reconstruction/gradio_helper.py new file mode 100644 index 00000000000..bd4aa468add --- /dev/null +++ b/notebooks/triposr-3d-reconstruction/gradio_helper.py @@ -0,0 +1,61 @@ +import os +from typing import Callable +import gradio as gr + +examples = [os.path.join("TripoSR/examples", img_name) for img_name in sorted(os.listdir("TripoSR/examples"))] + + +def check_input_image(input_image): + if input_image is None: + raise gr.Error("No image uploaded!") + + +def make_demo(preprocess_fn: Callable, generate_fn: Callable): + with gr.Blocks() as demo: + with gr.Row(variant="panel"): + with gr.Column(): + with gr.Row(): + input_image = gr.Image( + label="Input Image", + image_mode="RGBA", + sources="upload", + type="pil", + elem_id="content_image", + ) + processed_image = gr.Image(label="Processed Image", interactive=False) + with gr.Row(): + with gr.Group(): + do_remove_background = gr.Checkbox(label="Remove Background", value=True) + foreground_ratio = gr.Slider( + label="Foreground Ratio", + minimum=0.5, + maximum=1.0, + value=0.85, + step=0.05, + ) + with gr.Row(): + submit = gr.Button("Generate", elem_id="generate", variant="primary") + with gr.Column(): + with gr.Tab("Model"): + output_model = gr.Model3D( + label="Output Model", + interactive=False, + ) + with gr.Row(variant="panel"): + gr.Examples( + examples=examples, + inputs=[input_image], + outputs=[processed_image, output_model], + label="Examples", + examples_per_page=20, + ) + submit.click(fn=check_input_image, inputs=[input_image]).success( + fn=preprocess_fn, + inputs=[input_image, do_remove_background, foreground_ratio], + outputs=[processed_image], + ).success( + fn=generate_fn, + inputs=[processed_image], + outputs=[output_model], + ) + return demo diff --git a/notebooks/triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb b/notebooks/triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb index 3f2218fa64d..cf1d6a8e490 100644 --- a/notebooks/triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb +++ b/notebooks/triposr-3d-reconstruction/triposr-3d-reconstruction.ipynb @@ -75,7 +75,7 @@ "pip_install(\"torch>=2.2.2\", \"torchvision\", \"transformers>=4.35.0\", \"--extra-index-url\", \"https://download.pytorch.org/whl/cpu\")\n", "pip_install(\"rembg\", \"trimesh\", \"einops\", \"omegaconf>=2.3.0\", \"--extra-index-url\", \"https://download.pytorch.org/whl/cpu\")\n", "pip_install(\n", - " \"git+https://github.com/tatsy/torchmcubes.git@cbb3c3795b1e168bf81e8dee28623eaf5c33cd1c\", \"--extra-index-url\", \"https://download.pytorch.org/whl/cpu\"\n", + " \"git+https://github.com/tatsy/torchmcubes.git@cb81cddece46a8a126b08f7fbb9742f8605eefab\", \"--extra-index-url\", \"https://download.pytorch.org/whl/cpu\"\n", ")" ] }, @@ -113,8 +113,6 @@ }, "outputs": [], "source": [ - "import os\n", - "\n", "from tsr.system import TSR\n", "\n", "\n", @@ -124,7 +122,7 @@ " weight_name=\"model.ckpt\",\n", ")\n", "model.renderer.set_chunk_size(131072)\n", - "model.to(\"cpu\");" + "model.to(\"cpu\")" ] }, { @@ -525,13 +523,11 @@ { "cell_type": "code", "execution_count": null, - "id": "044bc9cf-3141-433d-a267-f5da84c9aa11", + "id": "28d8a250", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", - "\n", - "import gradio as gr\n", "import numpy as np\n", "import rembg\n", "from PIL import Image\n", @@ -542,11 +538,6 @@ "rembg_session = rembg.new_session()\n", "\n", "\n", - "def check_input_image(input_image):\n", - " if input_image is None:\n", - " raise gr.Error(\"No image uploaded!\")\n", - "\n", - "\n", "def preprocess(input_image, do_remove_background, foreground_ratio):\n", " def fill_background(image):\n", " image = np.array(image).astype(np.float32) / 255.0\n", @@ -572,64 +563,42 @@ " mesh = to_gradio_3d_orientation(mesh)\n", " mesh_path = tempfile.NamedTemporaryFile(suffix=\".obj\", delete=False)\n", " mesh.export(mesh_path.name)\n", - " return mesh_path.name\n", - "\n", - "\n", - "with gr.Blocks() as demo:\n", - " with gr.Row(variant=\"panel\"):\n", - " with gr.Column():\n", - " with gr.Row():\n", - " input_image = gr.Image(\n", - " label=\"Input Image\",\n", - " image_mode=\"RGBA\",\n", - " sources=\"upload\",\n", - " type=\"pil\",\n", - " elem_id=\"content_image\",\n", - " )\n", - " processed_image = gr.Image(label=\"Processed Image\", interactive=False)\n", - " with gr.Row():\n", - " with gr.Group():\n", - " do_remove_background = gr.Checkbox(label=\"Remove Background\", value=True)\n", - " foreground_ratio = gr.Slider(\n", - " label=\"Foreground Ratio\",\n", - " minimum=0.5,\n", - " maximum=1.0,\n", - " value=0.85,\n", - " step=0.05,\n", - " )\n", - " with gr.Row():\n", - " submit = gr.Button(\"Generate\", elem_id=\"generate\", variant=\"primary\")\n", - " with gr.Column():\n", - " with gr.Tab(\"Model\"):\n", - " output_model = gr.Model3D(\n", - " label=\"Output Model\",\n", - " interactive=False,\n", - " )\n", - " with gr.Row(variant=\"panel\"):\n", - " gr.Examples(\n", - " examples=[os.path.join(\"TripoSR/examples\", img_name) for img_name in sorted(os.listdir(\"TripoSR/examples\"))],\n", - " inputs=[input_image],\n", - " outputs=[processed_image, output_model],\n", - " label=\"Examples\",\n", - " examples_per_page=20,\n", - " )\n", - " submit.click(fn=check_input_image, inputs=[input_image]).success(\n", - " fn=preprocess,\n", - " inputs=[input_image, do_remove_background, foreground_ratio],\n", - " outputs=[processed_image],\n", - " ).success(\n", - " fn=generate,\n", - " inputs=[processed_image],\n", - " outputs=[output_model],\n", - " )\n", + " return mesh_path.name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "044bc9cf-3141-433d-a267-f5da84c9aa11", + "metadata": {}, + "outputs": [], + "source": [ + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/triposr-3d-reconstruction/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", + "\n", + "from gradio_helper import make_demo\n", + "\n", + "demo = make_demo(preprocess_fn=preprocess, generate_fn=generate)\n", "\n", "try:\n", " demo.launch(debug=True, height=680)\n", "except Exception:\n", - " demo.queue().launch(share=True, debug=True, height=680)\n", - "# if you are launching remotely, specify server_name and server_port\n", - "# demo.launch(server_name='your server name', server_port='server port in int')\n", - "# Read more in the docs: https://gradio.app/docs/" + " demo.launch(share=True, debug=True, height=680)\n", + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f1dbdc5", + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/typo-detector/typo-detector.ipynb b/notebooks/typo-detector/typo-detector.ipynb index c976ede0814..7ebf1f596ac 100644 --- a/notebooks/typo-detector/typo-detector.ipynb +++ b/notebooks/typo-detector/typo-detector.ipynb @@ -63,7 +63,7 @@ "metadata": {}, "outputs": [], "source": [ - "%pip install -q \"diffusers>=0.17.1\" \"openvino>=2023.1.0\" \"nncf>=2.5.0\" \"gradio>=4.19\" \"onnx>=1.11.0,<1.16.2\" \"transformers>=4.39.0\" \"torch>=2.1,<2.4\" \"torchvision<0.19.0\" --extra-index-url https://download.pytorch.org/whl/cpu\n", + "%pip install -q \"diffusers>=0.17.1\" \"openvino>=2023.1.0\" \"nncf>=2.5.0\" \"onnx>=1.11.0,<1.16.2\" \"transformers>=4.39.0\" \"torch>=2.1,<2.4\" \"torchvision<0.19.0\" --extra-index-url https://download.pytorch.org/whl/cpu\n", "%pip install -q \"git+https://github.com/huggingface/optimum-intel.git\"" ] }, diff --git a/notebooks/wuerstchen-image-generation/wuerstchen-image-generation.ipynb b/notebooks/wuerstchen-image-generation/wuerstchen-image-generation.ipynb index eff2332c313..81abdab5e05 100644 --- a/notebooks/wuerstchen-image-generation/wuerstchen-image-generation.ipynb +++ b/notebooks/wuerstchen-image-generation/wuerstchen-image-generation.ipynb @@ -1505,14 +1505,14 @@ "outputs": [], "source": [ "demo = gr.Interface(\n", - " generate,\n", - " [\n", + " fn=generate,\n", + " inputs=[\n", " gr.Textbox(label=\"Caption\"),\n", " gr.Textbox(label=\"Negative prompt\"),\n", " gr.Slider(2, 20, step=1, label=\"Prior guidance scale\"),\n", " gr.Slider(0, np.iinfo(np.int32).max, label=\"Seed\"),\n", " ],\n", - " \"image\",\n", + " outputs=\"image\",\n", " examples=[[\"Anthropomorphic cat dressed as a firefighter\", \"\", 4, 0]],\n", " allow_flagging=\"never\",\n", ")\n", @@ -1520,9 +1520,20 @@ " demo.queue().launch(debug=True)\n", "except Exception:\n", " demo.queue().launch(debug=True, share=True)\n", - "# if you are launching remotely, specify server_name and server_port\n", - "# demo.launch(server_name='your server name', server_port='server port in int')\n", - "# Read more in the docs: https://gradio.app/docs/" + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69eea01b", + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/yolov10-optimization/gradio_helper.py b/notebooks/yolov10-optimization/gradio_helper.py new file mode 100644 index 00000000000..e35d3086c48 --- /dev/null +++ b/notebooks/yolov10-optimization/gradio_helper.py @@ -0,0 +1,52 @@ +from typing import Callable +import gradio as gr + + +def make_demo(fn: Callable, quantized: bool): + with gr.Blocks() as demo: + gr.HTML( + """ +

+ YOLOv10: Real-Time End-to-End Object Detection using OpenVINO +

+ """ + ) + with gr.Row(): + with gr.Column(): + image = gr.Image(type="numpy", label="Image") + conf_threshold = gr.Slider( + label="Confidence Threshold", + minimum=0.1, + maximum=1.0, + step=0.1, + value=0.2, + ) + iou_threshold = gr.Slider( + label="IoU Threshold", + minimum=0.1, + maximum=1.0, + step=0.1, + value=0.45, + ) + use_int8 = gr.Checkbox( + value=quantized, + visible=quantized, + label="Use INT8 model", + ) + yolov10_infer = gr.Button(value="Detect Objects") + + with gr.Column(): + output_image = gr.Image(type="pil", label="Annotated Image") + + yolov10_infer.click( + fn=fn, + inputs=[ + image, + use_int8, + conf_threshold, + iou_threshold, + ], + outputs=[output_image], + ) + gr.Examples(["data/coco_bike.jpg"], inputs=[image]) + return demo diff --git a/notebooks/yolov10-optimization/yolov10-optimization.ipynb b/notebooks/yolov10-optimization/yolov10-optimization.ipynb index 8e8bd6d9d9d..70496c5d0c6 100644 --- a/notebooks/yolov10-optimization/yolov10-optimization.ipynb +++ b/notebooks/yolov10-optimization/yolov10-optimization.ipynb @@ -1443,82 +1443,53 @@ { "cell_type": "code", "execution_count": null, - "id": "ffdbe248-7086-4804-b4b9-c3dd454bf80c", - "metadata": { - "tags": [] - }, + "id": "2e5a6249", + "metadata": {}, "outputs": [], "source": [ - "import gradio as gr\n", - "\n", - "\n", "def yolov10_inference(image, int8, conf_threshold, iou_threshold):\n", " model = ov_yolo_model if not int8 else ov_yolo_int8_model\n", " results = model(source=image, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]\n", " annotated_image = Image.fromarray(results.plot())\n", "\n", - " return annotated_image\n", - "\n", + " return annotated_image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ffdbe248-7086-4804-b4b9-c3dd454bf80c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/yolov10-optimization/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", "\n", - "with gr.Blocks() as demo:\n", - " gr.HTML(\n", - " \"\"\"\n", - "

\n", - " YOLOv10: Real-Time End-to-End Object Detection using OpenVINO\n", - "

\n", - " \"\"\"\n", - " )\n", - " with gr.Row():\n", - " with gr.Column():\n", - " image = gr.Image(type=\"numpy\", label=\"Image\")\n", - " conf_threshold = gr.Slider(\n", - " label=\"Confidence Threshold\",\n", - " minimum=0.1,\n", - " maximum=1.0,\n", - " step=0.1,\n", - " value=0.2,\n", - " )\n", - " iou_threshold = gr.Slider(\n", - " label=\"IoU Threshold\",\n", - " minimum=0.1,\n", - " maximum=1.0,\n", - " step=0.1,\n", - " value=0.45,\n", - " )\n", - " use_int8 = gr.Checkbox(\n", - " value=ov_yolo_int8_model is not None,\n", - " visible=ov_yolo_int8_model is not None,\n", - " label=\"Use INT8 model\",\n", - " )\n", - " yolov10_infer = gr.Button(value=\"Detect Objects\")\n", - "\n", - " with gr.Column():\n", - " output_image = gr.Image(type=\"pil\", label=\"Annotated Image\")\n", - "\n", - " yolov10_infer.click(\n", - " fn=yolov10_inference,\n", - " inputs=[\n", - " image,\n", - " use_int8,\n", - " conf_threshold,\n", - " iou_threshold,\n", - " ],\n", - " outputs=[output_image],\n", - " )\n", - " examples = gr.Examples(\n", - " [\n", - " \"data/coco_bike.jpg\",\n", - " ],\n", - " inputs=[\n", - " image,\n", - " ],\n", - " )\n", + "from gradio_helper import make_demo\n", "\n", + "demo = make_demo(fn=yolov10_inference, quantized=ov_yolo_int8_model is not None)\n", "\n", "try:\n", " demo.launch(debug=True)\n", "except Exception:\n", - " demo.launch(debug=True, share=True)" + " demo.launch(debug=True, share=True)\n", + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b123c5e", + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ], diff --git a/notebooks/zeroscope-text2video/gradio_helper.py b/notebooks/zeroscope-text2video/gradio_helper.py new file mode 100644 index 00000000000..55dc27db93c --- /dev/null +++ b/notebooks/zeroscope-text2video/gradio_helper.py @@ -0,0 +1,21 @@ +from typing import Callable +import gradio as gr + + +def make_demo(fn: Callable): + demo = gr.Interface( + fn=fn, + inputs=[ + gr.Textbox(label="Prompt"), + gr.Slider(0, 1000000, value=42, label="Seed", step=1), + gr.Slider(10, 50, value=25, label="Number of inference steps", step=1), + ], + outputs=gr.Image(label="Result"), + examples=[ + ["An astronaut riding a horse.", 0, 25], + ["A panda eating bamboo on a rock.", 0, 25], + ["Spiderman is surfing.", 0, 25], + ], + allow_flagging="never", + ) + return demo diff --git a/notebooks/zeroscope-text2video/zeroscope-text2video.ipynb b/notebooks/zeroscope-text2video/zeroscope-text2video.ipynb index a3ac383ae13..b146527e136 100644 --- a/notebooks/zeroscope-text2video/zeroscope-text2video.ipynb +++ b/notebooks/zeroscope-text2video/zeroscope-text2video.ipynb @@ -1073,12 +1073,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "test_replace": { - " demo.queue().launch(debug=True)": " demo.queue.launch()", - " demo.queue().launch(share=True, debug=True)": " demo.queue().launch(share=True)" - } - }, + "metadata": {}, "outputs": [], "source": [ "def generate(prompt, seed, num_inference_steps, _=gr.Progress(track_tqdm=True)):\n", @@ -1091,32 +1086,45 @@ " out_file = tempfile.NamedTemporaryFile(suffix=\".gif\", delete=False)\n", " images = [PIL.Image.fromarray(frame) for frame in frames]\n", " images[0].save(out_file, save_all=True, append_images=images[1:], duration=125, loop=0)\n", - " return out_file.name\n", + " return out_file.name" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "test_replace": { + " demo.queue().launch(debug=True)": " demo.queue.launch()", + " demo.queue().launch(share=True, debug=True)": " demo.queue().launch(share=True)" + } + }, + "outputs": [], + "source": [ + "if not Path(\"gradio_helper.py\").exists():\n", + " r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/zeroscope-text2video/gradio_helper.py\")\n", + " open(\"gradio_helper.py\", \"w\").write(r.text)\n", "\n", + "from gradio_helper import make_demo\n", "\n", - "demo = gr.Interface(\n", - " generate,\n", - " [\n", - " gr.Textbox(label=\"Prompt\"),\n", - " gr.Slider(0, 1000000, value=42, label=\"Seed\", step=1),\n", - " gr.Slider(10, 50, value=25, label=\"Number of inference steps\", step=1),\n", - " ],\n", - " gr.Image(label=\"Result\"),\n", - " examples=[\n", - " [\"An astronaut riding a horse.\", 0, 25],\n", - " [\"A panda eating bamboo on a rock.\", 0, 25],\n", - " [\"Spiderman is surfing.\", 0, 25],\n", - " ],\n", - " allow_flagging=\"never\",\n", - ")\n", + "demo = make_demo(fn=generate)\n", "\n", "try:\n", " demo.queue().launch(debug=True)\n", "except Exception:\n", " demo.queue().launch(share=True, debug=True)\n", - "# if you are launching remotely, specify server_name and server_port\n", - "# demo.launch(server_name='your server name', server_port='server port in int')\n", - "# Read more in the docs: https://gradio.app/docs/" + "# If you are launching remotely, specify server_name and server_port\n", + "# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n", + "# To learn more please refer to the Gradio docs: https://gradio.app/docs/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# please uncomment and run this cell for stopping gradio interface\n", + "# demo.close()" ] } ],