Skip to content

Commit

Permalink
Add Gradio helpers - part 5 (openvinotoolkit#2323)
Browse files Browse the repository at this point in the history
Ticket: CVS-147626

Notebooks:

1. film-slowmo/film-slowmo.ipynb
2. freevc-voice-conversion/freevc-voice-conversion.ipynb
3. grammar-correction/grammar-correction.ipynb
4. hunyuan-dit-image-generation/hunyuan-dit-image-generation.ipynb
5. instant-id/instant-id.ipynb
6. instruct-pix2pix-image-editing/instruct-pix2pix-image-editing.ipynb
7.
kosmos2-multimodal-large-language-model/kosmos2-multimodal-large-language-model.ipynb
8.
latent-consistency-models-image-generation/latent-consistency-models-image-generation.ipynb
9. latent-consistency-models-image-generation/lcm-lora-controlnet.ipynb
10. llava-multimodal-chatbot/llava-multimodal-chatbot.ipynb
11. llava-multimodal-chatbot/videollava-multimodal-chatbot.ipynb
12. llava-next-multimodal-chatbot/llava-next-multimodal-chatbot.ipynb
  • Loading branch information
yatarkan authored Aug 27, 2024
1 parent bad12b0 commit eaa7610
Show file tree
Hide file tree
Showing 22 changed files with 1,053 additions and 735 deletions.
26 changes: 8 additions & 18 deletions notebooks/film-slowmo/film-slowmo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -715,24 +715,14 @@
" return filename\n",
"\n",
"\n",
"demo = gr.Interface(\n",
" generate,\n",
" [\n",
" gr.Image(label=\"First image\"),\n",
" gr.Image(label=\"Last image\"),\n",
" gr.Slider(\n",
" 1,\n",
" 8,\n",
" step=1,\n",
" label=\"Times to interpolate\",\n",
" info=\"\"\"Controls the number of times the frame interpolator is invoked.\n",
" The output will be the interpolation video with (2^value + 1) frames, fps of 30.\"\"\",\n",
" ),\n",
" ],\n",
" gr.Video(),\n",
" examples=[[*IMAGES.values(), 5]],\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/film-slowmo/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.queue().launch(debug=True)\n",
"except Exception:\n",
Expand Down
26 changes: 26 additions & 0 deletions notebooks/film-slowmo/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Callable
import gradio as gr

examples = [["data/one.jpg", "data/two.jpg", 5]]


def make_demo(fn: Callable):
demo = gr.Interface(
fn=fn,
inputs=[
gr.Image(label="First image"),
gr.Image(label="Last image"),
gr.Slider(
1,
8,
step=1,
label="Times to interpolate",
info="""Controls the number of times the frame interpolator is invoked.
The output will be the interpolation video with (2^value + 1) frames, fps of 30.""",
),
],
outputs=gr.Video(),
examples=examples,
allow_flagging="never",
)
return demo
44 changes: 16 additions & 28 deletions notebooks/freevc-voice-conversion/freevc-voice-conversion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1044,42 +1044,29 @@
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"\n",
"\n",
"audio1 = gr.Audio(label=\"Source Audio\", type=\"filepath\")\n",
"audio2 = gr.Audio(label=\"Reference Audio\", type=\"filepath\")\n",
"outputs = gr.Audio(label=\"Output Audio\", type=\"filepath\")\n",
"examples = [[audio1_name, audio2_name]]\n",
"\n",
"title = \"FreeVC with Gradio\"\n",
"description = 'Gradio Demo for FreeVC and OpenVINO™. Upload a source audio and a reference audio, then click the \"Submit\" button to inference.'\n",
"\n",
"\n",
"def infer(src, tgt):\n",
" output_audio = synthesize_audio(src, tgt)\n",
"\n",
" timestamp = time.strftime(\"%m-%d_%H-%M\", time.localtime())\n",
" result_name = f\"{timestamp}.wav\"\n",
" write(result_name, hps.data.sampling_rate, output_audio)\n",
"\n",
" return result_name\n",
"\n",
"\n",
"iface = gr.Interface(\n",
" infer,\n",
" [audio1, audio2],\n",
" outputs,\n",
" title=title,\n",
" description=description,\n",
" examples=examples,\n",
")\n",
"iface.launch()\n",
"if not Path(\"gradio_helper.py\").exists():\n",
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/freevc-voice-conversion/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(debug=True, share=True)\n",
"# if you are launching remotely, specify server_name and server_port\n",
"# iface.launch(server_name='your server name', server_port='server port in int')\n",
"# if you have any issue to launch on your platform, you can pass share=True to launch method:\n",
"# iface.launch(share=True)\n",
"# it creates a publicly shareable link for the interface. Read more in the docs: https://gradio.app/docs/"
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
"# Read more in the docs: https://gradio.app/docs/\""
]
},
{
Expand All @@ -1090,7 +1077,8 @@
},
"outputs": [],
"source": [
"iface.close()"
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
Expand Down
22 changes: 22 additions & 0 deletions notebooks/freevc-voice-conversion/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Callable
import gradio as gr


def make_demo(fn: Callable):
audio1 = gr.Audio(label="Source Audio", type="filepath")
audio2 = gr.Audio(label="Reference Audio", type="filepath")
outputs = gr.Audio(label="Output Audio", type="filepath")
examples = [["p225_001.wav", "p226_002.wav"]]

title = "FreeVC with Gradio"
description = 'Gradio Demo for FreeVC and OpenVINO™. Upload a source audio and a reference audio, then click the "Submit" button to inference.'

demo = gr.Interface(
fn=fn,
inputs=[audio1, audio2],
outputs=outputs,
title=title,
description=description,
examples=examples,
)
return demo
34 changes: 34 additions & 0 deletions notebooks/grammar-correction/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Callable
import gradio as gr
from functools import partial

default_text = (
"Most of the course is about semantic or content of language but there are also interesting"
" topics to be learned from the servicefeatures except statistics in characters in documents.At"
" this point, He introduces herself as his native English speaker and goes on to say that if"
" you contine to work on social scnce"
)


def make_demo(fn: Callable, quantized: bool):
model_type = "optimized" if quantized else "original"
with gr.Blocks() as demo:
gr.Markdown("# Interactive demo")
with gr.Row():
gr.Markdown(f"## Run {model_type} grammar correction pipeline")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="Text")
with gr.Column():
output_text = gr.Textbox(label="Correction", interactive=False, show_copy_button=True)
correction_time = gr.Textbox(label="Time (seconds)", interactive=False)
with gr.Row():
gr.Examples(examples=[default_text], inputs=[input_text])
with gr.Row():
button = gr.Button(f"Run {model_type}")
button.click(
fn=partial(fn, quantized=quantized),
inputs=[input_text, gr.Number(quantized, visible=False)],
outputs=[output_text, correction_time],
)
return demo
55 changes: 22 additions & 33 deletions notebooks/grammar-correction/grammar-correction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1193,54 +1193,43 @@
"source": [
"import gradio as gr\n",
"import time\n",
"import requests\n",
"\n",
"\n",
"def correct(text, quantized, progress=gr.Progress(track_tqdm=True)):\n",
" grammar_corrector = grammar_corrector_pipe_int8 if quantized else grammar_corrector_pipe\n",
"\n",
" start_time = time.perf_counter()\n",
" corrected_text = correct_text(text, grammar_checker_pipe, grammar_corrector)\n",
" end_time = time.perf_counter()\n",
"\n",
" return corrected_text, f\"{end_time - start_time:.2f}\"\n",
"\n",
"\n",
"def create_demo_block(quantized: bool, show_model_type: bool):\n",
" model_type = (\" optimized\" if quantized else \" original\") if show_model_type else \"\"\n",
" with gr.Row():\n",
" gr.Markdown(f\"## Run{model_type} grammar correction pipeline\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" input_text = gr.Textbox(label=\"Text\")\n",
" with gr.Column():\n",
" output_text = gr.Textbox(label=\"Correction\")\n",
" correction_time = gr.Textbox(label=\"Time (seconds)\")\n",
" with gr.Row():\n",
" gr.Examples(examples=[default_text], inputs=[input_text])\n",
" with gr.Row():\n",
" button = gr.Button(f\"Run{model_type}\")\n",
" button.click(\n",
" correct,\n",
" inputs=[input_text, gr.Number(quantized, visible=False)],\n",
" outputs=[output_text, correction_time],\n",
" )\n",
"\n",
"\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# Interactive demo\")\n",
" quantization_is_present = grammar_corrector_pipe_int8 is not None\n",
" create_demo_block(quantized=False, show_model_type=quantization_is_present)\n",
" if quantization_is_present:\n",
" create_demo_block(quantized=True, show_model_type=True)\n",
"if not Path(\"gradio_helper.py\").exists():\n",
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/grammar-correction/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=correct, quantized=grammar_corrector_pipe_int8 is not None)\n",
"\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/\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",
"# 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": "f289a071",
"metadata": {},
"outputs": [],
"source": [
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
Expand Down
97 changes: 97 additions & 0 deletions notebooks/hunyuan-dit-image-generation/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from typing import Callable
import gradio as gr
from hydit.constants import NEGATIVE_PROMPT

examples = [
["一只小猫"],
["a kitten"],
["一只聪明的狐狸走在阔叶树林里, 旁边是一条小溪, 细节真实, 摄影"],
["A clever fox walks in a broadleaf forest next to a stream, realistic details, photography"],
["请将“杞人忧天”的样子画出来"],
['Please draw a picture of "unfounded worries"'],
["枯藤老树昏鸦,小桥流水人家"],
["Withered vines, old trees and dim crows, small bridges and flowing water, people's houses"],
["湖水清澈,天空湛蓝,阳光灿烂。一只优雅的白天鹅在湖边游泳。它周围有几只小鸭子,看起来非常可爱,整个画面给人一种宁静祥和的感觉。"],
[
"The lake is clear, the sky is blue, and the sun is bright. An elegant white swan swims by the lake. There are several little ducks around it, which look very cute, and the whole picture gives people a sense of peace and tranquility."
],
["一朵鲜艳的红色玫瑰花,花瓣撒有一些水珠,晶莹剔透,特写镜头"],
["A bright red rose flower with petals sprinkled with some water drops, crystal clear, close-up"],
["风格是写实,画面主要描述一个亚洲戏曲艺术家正在表演,她穿着华丽的戏服,脸上戴着精致的面具,身姿优雅,背景是古色古香的舞台,镜头是近景"],
[
"The style is realistic. The picture mainly depicts an Asian opera artist performing. She is wearing a gorgeous costume and a delicate mask on her face. Her posture is elegant. The background is an antique stage and the camera is a close-up."
],
]


def make_demo(fn: Callable):
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Input prompt", lines=3)
with gr.Row():
infer_steps = gr.Slider(
label="Number Inference steps",
minimum=1,
maximum=200,
value=15,
step=1,
)
seed = gr.Number(
label="Seed",
minimum=-1,
maximum=1_000_000_000,
value=42,
step=1,
precision=0,
)
with gr.Accordion("Advanced settings", open=False):
with gr.Row():
negative_prompt = gr.Textbox(
label="Negative prompt",
value=NEGATIVE_PROMPT,
lines=2,
)
with gr.Row():
oriW = gr.Number(
label="Width",
minimum=768,
maximum=1024,
value=880,
step=16,
precision=0,
min_width=80,
)
oriH = gr.Number(
label="Height",
minimum=768,
maximum=1024,
value=880,
step=16,
precision=0,
min_width=80,
)
cfg_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=16.0, value=7.5, step=0.5)
with gr.Row():
advanced_button = gr.Button()
with gr.Column():
output_img = gr.Image(
label="Generated image",
interactive=False,
)
advanced_button.click(
fn=fn,
inputs=[
prompt,
negative_prompt,
seed,
infer_steps,
oriH,
oriW,
],
outputs=output_img,
)

with gr.Row():
gr.Examples(examples=examples, inputs=[prompt])
return demo
Loading

0 comments on commit eaa7610

Please sign in to comment.