Skip to content

Commit

Permalink
Add Gradio helpers - part 8 (#2337)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yatarkan authored Sep 3, 2024
1 parent 6225f99 commit a85afef
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 365 deletions.
1 change: 1 addition & 0 deletions .ci/ignore_treon_docker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/*

Expand Down
29 changes: 29 additions & 0 deletions notebooks/speculative-sampling/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -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
48 changes: 26 additions & 22 deletions notebooks/speculative-sampling/speculative-sampling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"source": [
"import time\n",
"import numpy as np\n",
"import gradio as gr\n",
"import openvino as ov"
]
},
Expand Down Expand Up @@ -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()"
]
}
],
Expand Down
27 changes: 27 additions & 0 deletions notebooks/stable-audio/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -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
76 changes: 21 additions & 55 deletions notebooks/stable-audio/stable-audio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
"<div><iframe src=\"http://127.0.0.1:7860/\" 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"
},
{
"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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
}
],
Expand Down
50 changes: 50 additions & 0 deletions notebooks/table-question-answering/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a85afef

Please sign in to comment.