Skip to content

Commit e00d4dc

Browse files
authored
Add Gradio helpers - part 7 (openvinotoolkit#2334)
Ticket: CVS-147626 Notebooks: 1. photo-maker/photo-maker.ipynb 2. pix2struct-docvqa/pix2struct-docvqa.ipynb 3. pixart/pixart.ipynb 4. qrcode-monster/qrcode-monster.ipynb 5. riffusion-text-to-music/riffusion-text-to-music.ipynb 6. rmbg-background-removal/rmbg-background-removal.ipynb 7. sdxl-turbo/sdxl-turbo.ipynb 8. sketch-to-image-pix2pix-turbo/sketch-to-image-pix2pix-turbo.ipynb 9. softvc-voice-conversion/softvc-voice-conversion.ipynb 10. sound-generation-audioldm2/sound-generation-audioldm2.ipynb
1 parent b83f303 commit e00d4dc

20 files changed

+954
-677
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Callable
2+
import gradio as gr
3+
4+
5+
trigger_word = "img"
6+
prompt = "sci-fi, closeup portrait photo of a man img in Iron man suit, face"
7+
negative_prompt = "(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth"
8+
9+
10+
def make_demo(fn: Callable):
11+
with gr.Blocks() as demo:
12+
with gr.Column():
13+
with gr.Row():
14+
input_image = gr.Image(label="Your image", sources=["upload"], type="pil")
15+
output_image = gr.Image(label="Generated Images", type="pil")
16+
positive_input = gr.Textbox(label=f"Text prompt, Trigger words is '{trigger_word}'")
17+
neg_input = gr.Textbox(label="Negative prompt")
18+
with gr.Row():
19+
seed_input = gr.Slider(0, 10_000_000, value=42, label="Seed")
20+
steps_input = gr.Slider(label="Steps", value=10, minimum=5, maximum=50, step=1)
21+
style_strength_ratio_input = gr.Slider(label="Style strength ratio", value=20, minimum=5, maximum=100, step=5)
22+
btn = gr.Button()
23+
btn.click(
24+
fn=fn,
25+
inputs=[
26+
positive_input,
27+
input_image,
28+
neg_input,
29+
seed_input,
30+
steps_input,
31+
style_strength_ratio_input,
32+
],
33+
outputs=output_image,
34+
)
35+
gr.Examples(
36+
[
37+
[prompt, negative_prompt],
38+
[
39+
"A woman img wearing a Christmas hat",
40+
negative_prompt,
41+
],
42+
[
43+
"A man img in a helmet and vest riding a motorcycle",
44+
negative_prompt,
45+
],
46+
[
47+
"photo of a middle-aged man img sitting on a plush leather couch, and watching television show",
48+
negative_prompt,
49+
],
50+
[
51+
"photo of a skilled doctor img in a pristine white lab coat enjoying a delicious meal in a sophisticated dining room",
52+
negative_prompt,
53+
],
54+
[
55+
"photo of superman img flying through a vibrant sunset sky, with his cape billowing in the wind",
56+
negative_prompt,
57+
],
58+
],
59+
[positive_input, neg_input],
60+
)
61+
return demo

notebooks/photo-maker/photo-maker.ipynb

+26-62
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,10 @@
975975
{
976976
"cell_type": "code",
977977
"execution_count": null,
978-
"id": "38788603-90b8-40e1-9bee-6743a0175417",
979-
"metadata": {
980-
"scrolled": true
981-
},
978+
"id": "ae437050",
979+
"metadata": {},
982980
"outputs": [],
983981
"source": [
984-
"import gradio as gr\n",
985-
"\n",
986-
"\n",
987982
"def generate_from_text(text_promt, input_image, neg_prompt, seed, num_steps, style_strength_ratio):\n",
988983
" \"\"\"\n",
989984
" Helper function for generating result image from prompt text\n",
@@ -1014,62 +1009,30 @@
10141009
" width=1024,\n",
10151010
" ).images[0]\n",
10161011
"\n",
1017-
" return result\n",
1018-
"\n",
1019-
"\n",
1020-
"with gr.Blocks() as demo:\n",
1021-
" with gr.Column():\n",
1022-
" with gr.Row():\n",
1023-
" input_image = gr.Image(label=\"Your image\", sources=[\"upload\"], type=\"pil\")\n",
1024-
" output_image = gr.Image(label=\"Generated Images\", type=\"pil\")\n",
1025-
" positive_input = gr.Textbox(label=f\"Text prompt, Trigger words is '{trigger_word}'\")\n",
1026-
" neg_input = gr.Textbox(label=\"Negative prompt\")\n",
1027-
" with gr.Row():\n",
1028-
" seed_input = gr.Slider(0, 10_000_000, value=42, label=\"Seed\")\n",
1029-
" steps_input = gr.Slider(label=\"Steps\", value=10, minimum=5, maximum=50, step=1)\n",
1030-
" style_strength_ratio_input = gr.Slider(label=\"Style strength ratio\", value=20, minimum=5, maximum=100, step=5)\n",
1031-
" btn = gr.Button()\n",
1032-
" btn.click(\n",
1033-
" generate_from_text,\n",
1034-
" [\n",
1035-
" positive_input,\n",
1036-
" input_image,\n",
1037-
" neg_input,\n",
1038-
" seed_input,\n",
1039-
" steps_input,\n",
1040-
" style_strength_ratio_input,\n",
1041-
" ],\n",
1042-
" output_image,\n",
1043-
" )\n",
1044-
" gr.Examples(\n",
1045-
" [\n",
1046-
" [prompt, negative_prompt],\n",
1047-
" [\n",
1048-
" \"A woman img wearing a Christmas hat\",\n",
1049-
" negative_prompt,\n",
1050-
" ],\n",
1051-
" [\n",
1052-
" \"A man img in a helmet and vest riding a motorcycle\",\n",
1053-
" negative_prompt,\n",
1054-
" ],\n",
1055-
" [\n",
1056-
" \"photo of a middle-aged man img sitting on a plush leather couch, and watching television show\",\n",
1057-
" negative_prompt,\n",
1058-
" ],\n",
1059-
" [\n",
1060-
" \"photo of a skilled doctor img in a pristine white lab coat enjoying a delicious meal in a sophisticated dining room\",\n",
1061-
" negative_prompt,\n",
1062-
" ],\n",
1063-
" [\n",
1064-
" \"photo of superman img flying through a vibrant sunset sky, with his cape billowing in the wind\",\n",
1065-
" negative_prompt,\n",
1066-
" ],\n",
1067-
" ],\n",
1068-
" [positive_input, neg_input],\n",
1069-
" )\n",
1012+
" return result"
1013+
]
1014+
},
1015+
{
1016+
"cell_type": "code",
1017+
"execution_count": null,
1018+
"id": "38788603-90b8-40e1-9bee-6743a0175417",
1019+
"metadata": {
1020+
"scrolled": true
1021+
},
1022+
"outputs": [],
1023+
"source": [
1024+
"if not Path(\"gradio_helper.py\").exists():\n",
1025+
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/photo-maker/gradio_helper.py\")\n",
1026+
" open(\"gradio_helper.py\", \"w\").write(r.text)\n",
1027+
"\n",
1028+
"from gradio_helper import make_demo\n",
10701029
"\n",
1030+
"demo = make_demo(fn=generate_from_text)\n",
10711031
"\n",
1072-
"demo.queue().launch()\n",
1032+
"try:\n",
1033+
" demo.queue().launch(debug=True)\n",
1034+
"except Exception:\n",
1035+
" demo.queue().launch(debug=True, share=True)\n",
10731036
"# if you are launching remotely, specify server_name and server_port\n",
10741037
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
10751038
"# Read more in the docs: https://gradio.app/docs/"
@@ -1084,7 +1047,8 @@
10841047
},
10851048
"outputs": [],
10861049
"source": [
1087-
"demo.close()"
1050+
"# please uncomment and run this cell for stopping gradio interface\n",
1051+
"# demo.close()"
10881052
]
10891053
}
10901054
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from pathlib import Path
2+
from typing import Callable
3+
import gradio as gr
4+
import requests
5+
from PIL import Image
6+
7+
8+
example_images_urls = [
9+
"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/94ef687c-aebb-452b-93fe-c7f29ce19503",
10+
"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/70b2271c-9295-493b-8a5c-2f2027dcb653",
11+
"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/1e2be134-0d45-4878-8e6c-08cfc9c8ea3d",
12+
]
13+
14+
file_names = ["eiffel_tower.png", "exsibition.jpeg", "population_table.jpeg"]
15+
16+
for img_url, image_file in zip(example_images_urls, file_names):
17+
if not Path(image_file).exists():
18+
Image.open(requests.get(img_url, stream=True).raw).save(image_file)
19+
20+
questions = [
21+
"What is Eiffel tower tall?",
22+
"When is the coffee break?",
23+
"What the population of Stoddard?",
24+
]
25+
26+
examples = [list(pair) for pair in zip(file_names, questions)]
27+
28+
29+
def make_demo(fn: Callable):
30+
demo = gr.Interface(
31+
fn=fn,
32+
inputs=["image", "text"],
33+
outputs="text",
34+
title="Pix2Struct for DocVQA",
35+
examples=examples,
36+
cache_examples=False,
37+
allow_flagging="never",
38+
)
39+
return demo

notebooks/pix2struct-docvqa/pix2struct-docvqa.ipynb

+18-31
Original file line numberDiff line numberDiff line change
@@ -303,43 +303,19 @@
303303
"metadata": {},
304304
"outputs": [],
305305
"source": [
306-
"import gradio as gr\n",
307-
"\n",
308-
"example_images_urls = [\n",
309-
" \"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/94ef687c-aebb-452b-93fe-c7f29ce19503\",\n",
310-
" \"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/70b2271c-9295-493b-8a5c-2f2027dcb653\",\n",
311-
" \"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/1e2be134-0d45-4878-8e6c-08cfc9c8ea3d\",\n",
312-
"]\n",
313-
"\n",
314-
"file_names = [\"eiffel_tower.png\", \"exsibition.jpeg\", \"population_table.jpeg\"]\n",
315-
"\n",
316-
"for img_url, image_file in zip(example_images_urls, file_names):\n",
317-
" load_image(img_url).save(image_file)\n",
318-
"\n",
319-
"questions = [\n",
320-
" \"What is Eiffel tower tall?\",\n",
321-
" \"When is the coffee break?\",\n",
322-
" \"What the population of Stoddard?\",\n",
323-
"]\n",
324-
"\n",
325-
"examples = [list(pair) for pair in zip(file_names, questions)]\n",
326-
"\n",
327-
"\n",
328306
"def generate(img, question):\n",
329307
" inputs = processor(images=img, text=question, return_tensors=\"pt\")\n",
330308
" predictions = ov_model.generate(**inputs, max_new_tokens=256)\n",
331309
" return processor.decode(predictions[0], skip_special_tokens=True)\n",
332310
"\n",
333311
"\n",
334-
"demo = gr.Interface(\n",
335-
" fn=generate,\n",
336-
" inputs=[\"image\", \"text\"],\n",
337-
" outputs=\"text\",\n",
338-
" title=\"Pix2Struct for DocVQA\",\n",
339-
" examples=examples,\n",
340-
" cache_examples=False,\n",
341-
" allow_flagging=\"never\",\n",
342-
")\n",
312+
"if not Path(\"gradio_helper.py\").exists():\n",
313+
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/pix2struct-docvqa/gradio_helper.py\")\n",
314+
" open(\"gradio_helper.py\", \"w\").write(r.text)\n",
315+
"\n",
316+
"from gradio_helper import make_demo\n",
317+
"\n",
318+
"demo = make_demo(fn=generate)\n",
343319
"\n",
344320
"try:\n",
345321
" demo.queue().launch(debug=True)\n",
@@ -349,6 +325,17 @@
349325
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
350326
"# Read more in the docs: https://gradio.app/docs/"
351327
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"execution_count": null,
332+
"id": "582a1703",
333+
"metadata": {},
334+
"outputs": [],
335+
"source": [
336+
"# please uncomment and run this cell for stopping gradio interface\n",
337+
"# demo.close()"
338+
]
352339
}
353340
],
354341
"metadata": {

notebooks/pixart/gradio_helper.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Callable
2+
import gradio as gr
3+
import numpy as np
4+
5+
MAX_SEED = np.iinfo(np.int32).max
6+
7+
examples = [
8+
["A small cactus with a happy face in the Sahara desert.", 42],
9+
["an astronaut sitting in a diner, eating fries, cinematic, analog film", 42],
10+
[
11+
"Pirate ship trapped in a cosmic maelstrom nebula, rendered in cosmic beach whirlpool engine, volumetric lighting, spectacular, ambient lights, light pollution, cinematic atmosphere, art nouveau style, illustration art artwork by SenseiJaye, intricate detail.",
12+
0,
13+
],
14+
["professional portrait photo of an anthropomorphic cat wearing fancy gentleman hat and jacket walking in autumn forest.", 0],
15+
]
16+
17+
18+
def make_demo(fn: Callable):
19+
demo = gr.Interface(
20+
fn=fn,
21+
inputs=[
22+
gr.Textbox(label="Caption"),
23+
gr.Slider(0, MAX_SEED, label="Seed"),
24+
gr.Textbox(label="Negative prompt"),
25+
gr.Slider(2, 20, step=1, label="Number of inference steps", value=4),
26+
],
27+
outputs="image",
28+
examples=examples,
29+
allow_flagging="never",
30+
)
31+
return demo

notebooks/pixart/pixart.ipynb

+19-26
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@
131131
"outputs": [],
132132
"source": [
133133
"from pathlib import Path\n",
134-
"\n",
135-
"import numpy as np\n",
136134
"import torch\n",
137-
"\n",
138135
"import openvino as ov\n",
139136
"\n",
140137
"\n",
@@ -529,35 +526,20 @@
529526
},
530527
"outputs": [],
531528
"source": [
532-
"import gradio as gr\n",
533-
"\n",
534-
"\n",
535529
"def generate(prompt, seed, negative_prompt, num_inference_steps):\n",
536530
" generator = torch.Generator().manual_seed(seed)\n",
537531
" image = pipe(prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, generator=generator, guidance_scale=0.0).images[0]\n",
538532
" return image\n",
539533
"\n",
540534
"\n",
541-
"demo = gr.Interface(\n",
542-
" generate,\n",
543-
" [\n",
544-
" gr.Textbox(label=\"Caption\"),\n",
545-
" gr.Slider(0, np.iinfo(np.int32).max, label=\"Seed\"),\n",
546-
" gr.Textbox(label=\"Negative prompt\"),\n",
547-
" gr.Slider(2, 20, step=1, label=\"Number of inference steps\", value=4),\n",
548-
" ],\n",
549-
" \"image\",\n",
550-
" examples=[\n",
551-
" [\"A small cactus with a happy face in the Sahara desert.\", 42],\n",
552-
" [\"an astronaut sitting in a diner, eating fries, cinematic, analog film\", 42],\n",
553-
" [\n",
554-
" \"Pirate ship trapped in a cosmic maelstrom nebula, rendered in cosmic beach whirlpool engine, volumetric lighting, spectacular, ambient lights, light pollution, cinematic atmosphere, art nouveau style, illustration art artwork by SenseiJaye, intricate detail.\",\n",
555-
" 0,\n",
556-
" ],\n",
557-
" [\"professional portrait photo of an anthropomorphic cat wearing fancy gentleman hat and jacket walking in autumn forest.\", 0],\n",
558-
" ],\n",
559-
" allow_flagging=\"never\",\n",
560-
")\n",
535+
"if not Path(\"gradio_helper.py\").exists():\n",
536+
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/pixart/gradio_helper.py\")\n",
537+
" open(\"gradio_helper.py\", \"w\").write(r.text)\n",
538+
"\n",
539+
"from gradio_helper import make_demo\n",
540+
"\n",
541+
"demo = make_demo(fn=generate)\n",
542+
"\n",
561543
"try:\n",
562544
" demo.queue().launch(debug=True)\n",
563545
"except Exception:\n",
@@ -566,6 +548,17 @@
566548
"# demo.launch(server_name='your server name', server_port='server port in int')\n",
567549
"# Read more in the docs: https://gradio.app/docs/"
568550
]
551+
},
552+
{
553+
"cell_type": "code",
554+
"execution_count": null,
555+
"id": "1d4e6624",
556+
"metadata": {},
557+
"outputs": [],
558+
"source": [
559+
"# please uncomment and run this cell for stopping gradio interface\n",
560+
"# demo.close()"
561+
]
569562
}
570563
],
571564
"metadata": {

0 commit comments

Comments
 (0)