Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gradio helpers - part 6 #2325

Merged
merged 11 commits into from
Aug 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@
"\n",
"\n",
"demo = gr.Interface(\n",
" classify,\n",
" [\n",
" fn=classify,\n",
" inputs=[\n",
" gr.File(label=\"Input file\", type=\"binary\"),\n",
" ],\n",
" gr.Label(label=\"Result\"),\n",
" outputs=gr.Label(label=\"Result\"),\n",
" examples=[[\"./README.md\"]],\n",
" allow_flagging=\"never\",\n",
")\n",
Expand All @@ -425,6 +425,17 @@
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
"# Read more in the docs: https://gradio.app/docs/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "914a8a84",
"metadata": {},
"outputs": [],
"source": [
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
"metadata": {
Expand Down
62 changes: 62 additions & 0 deletions notebooks/mms-massively-multilingual-speech/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import Callable
import gradio as gr


title = "MMS with Gradio"
description = (
'Gradio Demo for MMS and OpenVINO™. Upload a source audio, then click the "Submit" button to detect a language ID and a transcription. '
"Make sure that the audio data is sampled to 16000 kHz. If this language has not been used before, it may take some time to prepare the ASR model."
"\n"
"> Note: In order to run quantized model to transcribe some language, first the quantized model for that specific language must be prepared."
)


def make_demo(fn: Callable, quantized: bool):
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown(f"# {title}")
with gr.Row():
gr.Markdown(description)

with gr.Row():
with gr.Column():
audio = gr.Audio(label="Source Audio", type="filepath")
run_button = gr.Button(value="Run FP32")
# if quantized:
# run_quantized_button = gr.Button(value="Run INT8")
with gr.Column():
detected_language = gr.Textbox(label="Detected language ID")
transcription = gr.Textbox(label="Transcription")
identification_time = gr.Textbox(label="Identification time")
transcription_time = gr.Textbox(label="Transcription time")
with gr.Row(visible=quantized):
with gr.Column():
run_quantized_button = gr.Button(value="Run INT8")
with gr.Column():
detected_language_quantized = gr.Textbox(label="Detected language ID (Quantized)")
transcription_quantized = gr.Textbox(label="Transcription (Quantized)")
identification_time_quantized = gr.Textbox(label="Identification time (Quantized)")
transcription_time_quantized = gr.Textbox(label="Transcription time (Quantized)")

run_button.click(
fn=fn,
inputs=[audio, gr.Number(0, visible=False)],
outputs=[
detected_language,
transcription,
identification_time,
transcription_time,
],
)
if quantized:
run_quantized_button.click(
fn=fn,
inputs=[audio, gr.Number(1, visible=False)],
outputs=[
detected_language_quantized,
transcription_quantized,
identification_time_quantized,
transcription_time_quantized,
],
)
return demo
Original file line number Diff line number Diff line change
Expand Up @@ -1560,30 +1560,15 @@
{
"cell_type": "code",
"execution_count": null,
"id": "49f4b7871f18ab46",
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-12T16:17:33.520443100Z",
"start_time": "2023-10-12T16:13:07.494290500Z"
},
"collapsed": false
},
"id": "26913187",
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import librosa\n",
"import time\n",
"\n",
"\n",
"title = \"MMS with Gradio\"\n",
"description = (\n",
" 'Gradio Demo for MMS and OpenVINO™. Upload a source audio, then click the \"Submit\" button to detect a language ID and a transcription. '\n",
" \"Make sure that the audio data is sampled to 16000 kHz. If this language has not been used before, it may take some time to prepare the ASR model.\"\n",
" \"\\n\"\n",
" \"> Note: In order to run quantized model to transcribe some language, first the quantized model for that specific language must be prepared.\"\n",
")\n",
"\n",
"\n",
"current_state = {\n",
" \"fp32\": {\"model\": None, \"language\": None},\n",
" \"int8\": {\"model\": None, \"language\": None},\n",
Expand Down Expand Up @@ -1620,57 +1605,31 @@
" transcription,\n",
" identification_delta_time,\n",
" transcription_delta_time,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49f4b7871f18ab46",
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-12T16:17:33.520443100Z",
"start_time": "2023-10-12T16:13:07.494290500Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"if not Path(\"gradio_helper.py\").exists():\n",
" r = requests.get(\n",
" url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/mms-massively-multilingual-speech/gradio_helper.py\"\n",
" )\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.Row():\n",
" gr.Markdown(f\"# {title}\")\n",
" with gr.Row():\n",
" gr.Markdown(description)\n",
"\n",
" run_button = {True: None, False: None}\n",
" detected_language = {True: None, False: None}\n",
" transcription = {True: None, False: None}\n",
" identification_time = {True: None, False: None}\n",
" transcription_time = {True: None, False: None}\n",
" for quantized in [False, True]:\n",
" if quantized and not to_quantize.value:\n",
" break\n",
" with gr.Row():\n",
" with gr.Column():\n",
" if not quantized:\n",
" audio = gr.Audio(label=\"Source Audio\", type=\"filepath\")\n",
" run_button_name = \"Run INT8\" if quantized else \"Run FP32\" if to_quantize.value else \"Run\"\n",
" run_button[quantized] = gr.Button(value=run_button_name)\n",
" with gr.Column():\n",
" detected_language[quantized] = gr.Textbox(label=f\"Detected language ID{' (Quantized)' if quantized else ''}\")\n",
" transcription[quantized] = gr.Textbox(label=f\"Transcription{' (Quantized)' if quantized else ''}\")\n",
" identification_time[quantized] = gr.Textbox(label=f\"Identification time{' (Quantized)' if quantized else ''}\")\n",
" transcription_time[quantized] = gr.Textbox(label=f\"Transcription time{' (Quantized)' if quantized else ''}\")\n",
"\n",
" run_button[False].click(\n",
" infer,\n",
" inputs=[audio, gr.Number(0, visible=False)],\n",
" outputs=[\n",
" detected_language[False],\n",
" transcription[False],\n",
" identification_time[False],\n",
" transcription_time[False],\n",
" ],\n",
" )\n",
" if to_quantize.value:\n",
" run_button[True].click(\n",
" infer,\n",
" inputs=[audio, gr.Number(1, visible=False)],\n",
" outputs=[\n",
" detected_language[True],\n",
" transcription[True],\n",
" identification_time[True],\n",
" transcription_time[True],\n",
" ],\n",
" )\n",
"\n",
"demo = make_demo(fn=infer, quantized=to_quantize.value)\n",
"\n",
"try:\n",
" demo.queue().launch(debug=True)\n",
Expand All @@ -1680,6 +1639,17 @@
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
"# Read more in the docs: https://gradio.app/docs/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20105387",
"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 @@ -729,7 +729,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9fe048d2-0bd4-4748-aaf5-af221e069333",
"id": "3517c889",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -761,13 +761,20 @@
" if outputs.endswith(stop_str):\n",
" outputs = outputs[: -len(stop_str)]\n",
"\n",
" return outputs.strip()\n",
"\n",
"\n",
" return outputs.strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fe048d2-0bd4-4748-aaf5-af221e069333",
"metadata": {},
"outputs": [],
"source": [
"demo = gr.Interface(\n",
" generate,\n",
" [gr.Image(label=\"Image\", type=\"pil\"), gr.Textbox(label=\"Prompt\")],\n",
" gr.Textbox(),\n",
" fn=generate,\n",
" inputs=[gr.Image(label=\"Image\", type=\"pil\"), gr.Textbox(label=\"Prompt\")],\n",
" outputs=gr.Textbox(),\n",
" examples=[\n",
" [\n",
" str(IMAGE_PATH),\n",
Expand All @@ -785,6 +792,17 @@
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
"# Read more in the docs: https://gradio.app/docs/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58eb46c9",
"metadata": {},
"outputs": [],
"source": [
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
"metadata": {
Expand Down
13 changes: 12 additions & 1 deletion notebooks/music-generation/music-generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@
"import gradio as gr\n",
"\n",
"demo = gr.Interface(\n",
" _generate,\n",
" fn=_generate,\n",
" inputs=[\n",
" gr.Textbox(label=\"Text Prompt\"),\n",
" ],\n",
Expand All @@ -810,6 +810,17 @@
"# 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": "cc027753",
"metadata": {},
"outputs": [],
"source": [
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
"metadata": {
Expand Down
33 changes: 20 additions & 13 deletions notebooks/named-entity-recognition/named-entity-recognition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -505,33 +505,40 @@
"source": [
"import gradio as gr\n",
"\n",
"examples = [\n",
" \"My name is Wolfgang and I live in Berlin.\",\n",
"]\n",
"\n",
"\n",
"def run_ner(text):\n",
" output = ner_pipeline_optimized(text)\n",
" return {\"text\": text, \"entities\": output}\n",
"\n",
"\n",
"demo = gr.Interface(\n",
" run_ner,\n",
" gr.Textbox(placeholder=\"Enter sentence here...\", label=\"Input Text\"),\n",
" gr.HighlightedText(label=\"Output Text\"),\n",
" examples=examples,\n",
" fn=run_ner,\n",
" inputs=gr.Textbox(placeholder=\"Enter sentence here...\", label=\"Input Text\"),\n",
" outputs=gr.HighlightedText(label=\"Output Text\"),\n",
" examples=[\n",
" \"My name is Wolfgang and I live in Berlin.\",\n",
" ],\n",
" allow_flagging=\"never\",\n",
")\n",
"\n",
"if __name__ == \"__main__\":\n",
" try:\n",
" demo.launch(debug=True)\n",
" except Exception:\n",
" demo.launch(share=True, debug=True)\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",
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
"# Read more in the 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()"
]
}
],
"metadata": {
Expand Down
Loading