client_app.py 33.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2022 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
import gradio as gr
import numpy as np

from .http_client_manager import get_metric_data
from .http_client_manager import HttpClientManager
from .http_client_manager import metrics_table_head
21
from .http_client_manager import metrics_table_head_en
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
from .visualizer import visualize_detection
from .visualizer import visualize_face_alignment
from .visualizer import visualize_face_detection
from .visualizer import visualize_headpose
from .visualizer import visualize_keypoint_detection
from .visualizer import visualize_matting
from .visualizer import visualize_ocr
from .visualizer import visualize_segmentation

_http_manager = HttpClientManager()

supported_tasks = {
    'detection': visualize_detection,
    'facedet': visualize_face_detection,
    'keypointdetection': visualize_keypoint_detection,
    'segmentation': visualize_segmentation,
    'matting': visualize_matting,
    'ocr': visualize_ocr,
    'facealignment': visualize_face_alignment,
    'headpose': visualize_headpose,
    'unspecified': lambda x: str(x)
}


def create_gradio_client_app():  # noqa:C901
    css = """
          .gradio-container {
              font-family: 'IBM Plex Sans', sans-serif;
          }
          .gr-button {
              color: white;
              border-color: black;
              background: black;
          }
          input[type='range'] {
              accent-color: black;
          }
          .dark input[type='range'] {
              accent-color: #dfdfdf;
          }
          #gallery {
              min-height: 22rem;
              margin-bottom: 15px;
              margin-left: auto;
              margin-right: auto;
              border-bottom-right-radius: .5rem !important;
              border-bottom-left-radius: .5rem !important;
          }
          #gallery>div>.h-full {
              min-height: 20rem;
          }
          .details:hover {
              text-decoration: underline;
          }
          .gr-button {
              white-space: nowrap;
          }
          .gr-button:focus {
              border-color: rgb(147 197 253 / var(--tw-border-opacity));
              outline: none;
              box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
              --tw-border-opacity: 1;
              --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) \
                var(--tw-ring-offset-color);
              --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
              --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
              --tw-ring-opacity: .5;
          }
          .footer {
              margin-bottom: 45px;
              margin-top: 35px;
              text-align: center;
              border-bottom: 1px solid #e5e5e5;
          }
          .footer>p {
              font-size: .8rem;
              display: inline-block;
              padding: 0 10px;
              transform: translateY(10px);
              background: white;
          }
          .dark .footer {
              border-color: #303030;
          }
          .dark .footer>p {
              background: #0b0f19;
          }
          .prompt h4{
              margin: 1.25em 0 .25em 0;
              font-weight: bold;
              font-size: 115%;
          }
  """

    block = gr.Blocks(css=css)

    with block:
        gr.HTML("""
              <div style="text-align: center; max-width: 650px; margin: 0 auto;">
                <div
                  style="
                    display: inline-flex;
                    gap: 0.8rem;
                    font-size: 1.75rem;
                    justify-content: center;
                  "
                >
                <h1>
                FastDeploy Client
                </h1>
                </div>
                <p font-size: 94%">
                The client is used for creating requests to fastdeploy server.
                </p>
              </div>
          """)
        with gr.Group():
            with gr.Box():
                with gr.Column():
                    with gr.Row():
                        server_addr_text = gr.Textbox(
                            label="服务ip",
                            show_label=True,
                            max_lines=1,
                            placeholder="localhost",
                        )

                        server_http_port_text = gr.Textbox(
                            label="推理服务端口",
                            show_label=True,
                            max_lines=1,
                            placeholder="8000",
                        )

                        server_metric_port_text = gr.Textbox(
                            label="性能服务端口",
                            show_label=True,
                            max_lines=1,
                            placeholder="8002",
                        )
                    with gr.Row():
                        model_name_text = gr.Textbox(
                            label="模型名称",
                            show_label=True,
                            max_lines=1,
                            placeholder="yolov5",
                        )
                        model_version_text = gr.Textbox(
                            label="模型版本",
                            show_label=True,
                            max_lines=1,
                            placeholder="1",
                        )

            with gr.Box():
                with gr.Tab("组件形式"):
                    check_button = gr.Button("获取模型输入输出")
                    component_format_column = gr.Column(visible=False)
                    with component_format_column:
                        task_radio = gr.Radio(
                            choices=list(supported_tasks.keys()),
                            value='unspecified',
                            label='任务类型',
                            visible=True)
                        gr.Markdown("根据模型需要,挑选文本框或者图像框进行输入")
                        with gr.Row():
                            with gr.Column():
                                gr.Markdown("模型输入")
                                input_accordions = []
                                input_name_texts = []
                                input_images = []
                                input_texts = []
                                for i in range(6):
                                    accordion = gr.Accordion(
                                        "输入变量 {}".format(i),
                                        open=True,
                                        visible=False)
                                    with accordion:
                                        input_name_text = gr.Textbox(
                                            label="变量名", interactive=False)
                                        input_image = gr.Image(type='numpy')
                                        input_text = gr.Textbox(
                                            label="文本框", max_lines=1000)
                                    input_accordions.append(accordion)
                                    input_name_texts.append(input_name_text)
                                    input_images.append(input_image)
                                    input_texts.append(input_text)

                            with gr.Column():
                                gr.Markdown("模型输出")
                                output_accordions = []
                                output_name_texts = []
                                output_images = []
                                output_texts = []
                                for i in range(6):
                                    accordion = gr.Accordion(
                                        "输出变量 {}".format(i),
                                        open=True,
                                        visible=False)
                                    with accordion:
                                        output_name_text = gr.Textbox(
                                            label="变量名", interactive=False)
                                        output_text = gr.Textbox(
                                            label="服务返回的原数据",
                                            interactive=False,
                                            show_label=True)
                                        output_image = gr.Image(
                                            interactive=False)
                                    output_accordions.append(accordion)
                                    output_name_texts.append(output_name_text)
                                    output_images.append(output_image)
                                    output_texts.append(output_text)
                        component_submit_button = gr.Button("提交请求")
                with gr.Tab("原始形式"):
                    gr.Markdown("模型输入")
                    raw_payload_text = gr.Textbox(
                        label="负载数据", max_lines=10000)
                    with gr.Column():
                        gr.Markdown("输出")
                        output_raw_text = gr.Textbox(
                            label="服务返回的原始数据", interactive=False)
                    raw_submit_button = gr.Button("提交请求")

            with gr.Box():
                with gr.Column():
                    gr.Markdown("服务性能统计(每次提交请求会自动更新数据,您也可以手动点击更新)")
                    output_html_table = gr.HTML(
                        label="metrics",
                        interactive=False,
                        show_label=False,
                        value=metrics_table_head.format('', ''))
                    update_metric_button = gr.Button("更新统计数据")

            status_text = gr.Textbox(
                label="status",
                show_label=True,
                max_lines=1,
                interactive=False)

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
        lang_text = gr.Textbox(
            label="lang",
            show_label=False,
            value='zh',
            max_lines=1,
            visible=False
        )  # This text box is only used for divide zh and en page

        all_input_output_components = input_accordions + input_name_texts + input_images + \
            input_texts + output_accordions + output_name_texts + output_images + output_texts

        def get_input_output_name(server_ip, server_port, model_name,
                                  model_version, lang_text):
            try:
                server_addr = server_ip + ':' + server_port
                input_metas, output_metas = _http_manager.get_model_meta(
                    server_addr, model_name, model_version)
            except Exception as e:
                return {status_text: str(e)}
            results = {
                component: None
                for component in all_input_output_components
            }
            results[component_format_column] = gr.update(visible=True)
            for input_accordio in input_accordions:
                results[input_accordio] = gr.update(visible=False)
            for output_accordio in output_accordions:
                results[output_accordio] = gr.update(visible=False)
            results[status_text] = 'Get model inputs and outputs successfully.'
            for i, input_meta in enumerate(input_metas):
                results[input_accordions[i]] = gr.update(visible=True)
                results[input_name_texts[i]] = input_meta['name']
            for i, output_meta in enumerate(output_metas):
                results[output_accordions[i]] = gr.update(visible=True)
                results[output_name_texts[i]] = output_meta['name']
            return results

        def component_inference(*args):
            server_ip = args[0]
            http_port = args[1]
            metric_port = args[2]
            model_name = args[3]
            model_version = args[4]
            names = args[5:5 + len(input_name_texts)]
            images = args[5 + len(input_name_texts):5 + len(input_name_texts) +
                          len(input_images)]
            texts = args[5 + len(input_name_texts) + len(input_images):5 +
                         len(input_name_texts) + len(input_images) +
                         len(input_texts)]
            task_type = args[-1]
            server_addr = server_ip + ':' + http_port
            if server_ip and http_port and model_name and model_version:
                inputs = {}
                for i, input_name in enumerate(names):
                    if input_name:
                        if images[i] is not None:
                            inputs[input_name] = np.array([images[i]])
                        if texts[i]:
                            inputs[input_name] = np.array(
                                [[texts[i].encode('utf-8')]], dtype=np.object_)
                try:
                    infer_results = _http_manager.infer(
                        server_addr, model_name, model_version, inputs)
                    results = {status_text: 'Inference successfully.'}
                    for i, (output_name,
                            data) in enumerate(infer_results.items()):
                        results[output_name_texts[i]] = output_name
                        results[output_texts[i]] = str(data)
                        if task_type != 'unspecified':
                            try:
                                results[output_images[i]] = supported_tasks[
                                    task_type](images[0], data)
                            except Exception:
                                results[output_images[i]] = None
                    if metric_port:
                        html_table = get_metric_data(server_ip, metric_port,
                                                     'zh')
                        results[output_html_table] = html_table
                    return results
                except Exception as e:
                    return {status_text: 'Error: {}'.format(e)}
            else:
                return {
                    status_text:
                    'Please input server addr, model name and model version.'
                }

        def raw_inference(*args):
            server_ip = args[0]
            http_port = args[1]
            metric_port = args[2]
            model_name = args[3]
            model_version = args[4]
            payload_text = args[5]
            server_addr = server_ip + ':' + http_port
            try:
                result = _http_manager.raw_infer(server_addr, model_name,
                                                 model_version, payload_text)
                results = {
                    status_text: 'Get response from server',
                    output_raw_text: result
                }
                if server_ip and metric_port:
                    html_table = get_metric_data(server_ip, metric_port, 'zh')
                    results[output_html_table] = html_table
                return results
            except Exception as e:
                return {status_text: 'Error: {}'.format(e)}

        def update_metric(server_ip, metrics_port, lang_text):
            if server_ip and metrics_port:
                try:
                    html_table = get_metric_data(server_ip, metrics_port, 'zh')
                    return {
                        output_html_table: html_table,
                        status_text: "Update metrics successfully."
                    }
                except Exception as e:
                    return {status_text: 'Error: {}'.format(e)}
            else:
                return {
                    status_text: 'Please input server ip and metrics_port.'
                }

        check_button.click(
            fn=get_input_output_name,
            inputs=[
                server_addr_text, server_http_port_text, model_name_text,
                model_version_text, lang_text
            ],
            outputs=[
                *all_input_output_components, check_button,
                component_format_column, status_text
            ])
        component_submit_button.click(
            fn=component_inference,
            inputs=[
                server_addr_text, server_http_port_text,
                server_metric_port_text, model_name_text, model_version_text,
                *input_name_texts, *input_images, *input_texts, task_radio
            ],
            outputs=[
                *output_name_texts, *output_images, *output_texts, status_text,
                output_html_table
            ])
        raw_submit_button.click(
            fn=raw_inference,
            inputs=[
                server_addr_text, server_http_port_text,
                server_metric_port_text, model_name_text, model_version_text,
                raw_payload_text
            ],
            outputs=[output_raw_text, status_text, output_html_table])
        update_metric_button.click(
            fn=update_metric,
            inputs=[server_addr_text, server_metric_port_text, lang_text],
            outputs=[output_html_table, status_text])
    return block


def create_gradio_client_app_en():  # noqa:C901
    css = """
          .gradio-container {
              font-family: 'IBM Plex Sans', sans-serif;
          }
          .gr-button {
              color: white;
              border-color: black;
              background: black;
          }
          input[type='range'] {
              accent-color: black;
          }
          .dark input[type='range'] {
              accent-color: #dfdfdf;
          }
          #gallery {
              min-height: 22rem;
              margin-bottom: 15px;
              margin-left: auto;
              margin-right: auto;
              border-bottom-right-radius: .5rem !important;
              border-bottom-left-radius: .5rem !important;
          }
          #gallery>div>.h-full {
              min-height: 20rem;
          }
          .details:hover {
              text-decoration: underline;
          }
          .gr-button {
              white-space: nowrap;
          }
          .gr-button:focus {
              border-color: rgb(147 197 253 / var(--tw-border-opacity));
              outline: none;
              box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
              --tw-border-opacity: 1;
              --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) \
                var(--tw-ring-offset-color);
              --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
              --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
              --tw-ring-opacity: .5;
          }
          .footer {
              margin-bottom: 45px;
              margin-top: 35px;
              text-align: center;
              border-bottom: 1px solid #e5e5e5;
          }
          .footer>p {
              font-size: .8rem;
              display: inline-block;
              padding: 0 10px;
              transform: translateY(10px);
              background: white;
          }
          .dark .footer {
              border-color: #303030;
          }
          .dark .footer>p {
              background: #0b0f19;
          }
          .prompt h4{
              margin: 1.25em 0 .25em 0;
              font-weight: bold;
              font-size: 115%;
          }
  """

    block = gr.Blocks(css=css)

    with block:
        gr.HTML("""
              <div style="text-align: center; max-width: 650px; margin: 0 auto;">
                <div
                  style="
                    display: inline-flex;
                    gap: 0.8rem;
                    font-size: 1.75rem;
                    justify-content: center;
                  "
                >
                <h1>
                FastDeploy Client
                </h1>
                </div>
                <p font-size: 94%">
                The client is used for creating requests to fastdeploy server.
                </p>
              </div>
          """)
        with gr.Group():
            with gr.Box():
                with gr.Column():
                    with gr.Row():
                        server_addr_text = gr.Textbox(
                            label="server ip",
                            show_label=True,
                            max_lines=1,
                            placeholder="localhost",
                        )

                        server_http_port_text = gr.Textbox(
                            label="server port",
                            show_label=True,
                            max_lines=1,
                            placeholder="8000",
                        )

                        server_metric_port_text = gr.Textbox(
                            label="metrics port",
                            show_label=True,
                            max_lines=1,
                            placeholder="8002",
                        )
                    with gr.Row():
                        model_name_text = gr.Textbox(
                            label="model name",
                            show_label=True,
                            max_lines=1,
                            placeholder="yolov5",
                        )
                        model_version_text = gr.Textbox(
                            label="model version",
                            show_label=True,
                            max_lines=1,
                            placeholder="1",
                        )

            with gr.Box():
                with gr.Tab("Component form"):
                    check_button = gr.Button("get model input and output")
                    component_format_column = gr.Column(visible=False)
                    with component_format_column:
                        task_radio = gr.Radio(
                            choices=list(supported_tasks.keys()),
                            value='unspecified',
                            label='task type',
                            visible=True)
                        gr.Markdown(
                            "Choose text or image component to input according to data type"
                        )
                        with gr.Row():
                            with gr.Column():
                                gr.Markdown("Inputs")
                                input_accordions = []
                                input_name_texts = []
                                input_images = []
                                input_texts = []
                                for i in range(6):
                                    accordion = gr.Accordion(
                                        "variable {}".format(i),
                                        open=True,
                                        visible=False)
                                    with accordion:
                                        input_name_text = gr.Textbox(
                                            label="variable name",
                                            interactive=False)
                                        input_image = gr.Image(type='numpy')
                                        input_text = gr.Textbox(
                                            label="text", max_lines=1000)
                                    input_accordions.append(accordion)
                                    input_name_texts.append(input_name_text)
                                    input_images.append(input_image)
                                    input_texts.append(input_text)

                            with gr.Column():
                                gr.Markdown("Outputs")
                                output_accordions = []
                                output_name_texts = []
                                output_images = []
                                output_texts = []
                                for i in range(6):
                                    accordion = gr.Accordion(
                                        "variable {}".format(i),
                                        open=True,
                                        visible=False)
                                    with accordion:
                                        output_name_text = gr.Textbox(
                                            label="variable name",
                                            interactive=False)
                                        output_text = gr.Textbox(
                                            label="text",
                                            interactive=False,
                                            show_label=True)
                                        output_image = gr.Image(
                                            interactive=False)
                                    output_accordions.append(accordion)
                                    output_name_texts.append(output_name_text)
                                    output_images.append(output_image)
                                    output_texts.append(output_text)
                        component_submit_button = gr.Button("submit request")
                with gr.Tab("Original form"):
                    gr.Markdown("Request")
                    raw_payload_text = gr.Textbox(
                        label="request payload", max_lines=10000)
                    with gr.Column():
                        gr.Markdown("Response")
                        output_raw_text = gr.Textbox(
                            label="raw response data", interactive=False)
                    raw_submit_button = gr.Button("submit request")

            with gr.Box():
                with gr.Column():
                    gr.Markdown(
                        "Metrics(update automatically when submit request,or click update metrics button manually)"
                    )
                    output_html_table = gr.HTML(
                        label="metrics",
                        interactive=False,
                        show_label=False,
                        value=metrics_table_head_en.format('', ''))
                    update_metric_button = gr.Button("update metrics")

            status_text = gr.Textbox(
                label="status",
                show_label=True,
                max_lines=1,
                interactive=False)

        lang_text = gr.Textbox(
            label="lang",
            show_label=False,
            value='en',
            max_lines=1,
            visible=False
        )  # This text box is only used for divide zh and en page

650 651 652 653
        all_input_output_components = input_accordions + input_name_texts + input_images + \
            input_texts + output_accordions + output_name_texts + output_images + output_texts

        def get_input_output_name(server_ip, server_port, model_name,
654
                                  model_version, lang_text):
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
            try:
                server_addr = server_ip + ':' + server_port
                input_metas, output_metas = _http_manager.get_model_meta(
                    server_addr, model_name, model_version)
            except Exception as e:
                return {status_text: str(e)}
            results = {
                component: None
                for component in all_input_output_components
            }
            results[component_format_column] = gr.update(visible=True)
            for input_accordio in input_accordions:
                results[input_accordio] = gr.update(visible=False)
            for output_accordio in output_accordions:
                results[output_accordio] = gr.update(visible=False)
670
            results[status_text] = 'Get model inputs and outputs successfully.'
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
            for i, input_meta in enumerate(input_metas):
                results[input_accordions[i]] = gr.update(visible=True)
                results[input_name_texts[i]] = input_meta['name']
            for i, output_meta in enumerate(output_metas):
                results[output_accordions[i]] = gr.update(visible=True)
                results[output_name_texts[i]] = output_meta['name']
            return results

        def component_inference(*args):
            server_ip = args[0]
            http_port = args[1]
            metric_port = args[2]
            model_name = args[3]
            model_version = args[4]
            names = args[5:5 + len(input_name_texts)]
            images = args[5 + len(input_name_texts):5 + len(input_name_texts) +
                          len(input_images)]
            texts = args[5 + len(input_name_texts) + len(input_images):5 +
                         len(input_name_texts) + len(input_images) +
                         len(input_texts)]
            task_type = args[-1]
            server_addr = server_ip + ':' + http_port
            if server_ip and http_port and model_name and model_version:
                inputs = {}
                for i, input_name in enumerate(names):
                    if input_name:
                        if images[i] is not None:
                            inputs[input_name] = np.array([images[i]])
                        if texts[i]:
                            inputs[input_name] = np.array(
                                [[texts[i].encode('utf-8')]], dtype=np.object_)
                try:
                    infer_results = _http_manager.infer(
                        server_addr, model_name, model_version, inputs)
705
                    results = {status_text: 'Inference successfully.'}
706 707 708 709 710 711 712 713 714 715 716
                    for i, (output_name,
                            data) in enumerate(infer_results.items()):
                        results[output_name_texts[i]] = output_name
                        results[output_texts[i]] = str(data)
                        if task_type != 'unspecified':
                            try:
                                results[output_images[i]] = supported_tasks[
                                    task_type](images[0], data)
                            except Exception:
                                results[output_images[i]] = None
                    if metric_port:
717 718
                        html_table = get_metric_data(server_ip, metric_port,
                                                     'en')
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
                        results[output_html_table] = html_table
                    return results
                except Exception as e:
                    return {status_text: 'Error: {}'.format(e)}
            else:
                return {
                    status_text:
                    'Please input server addr, model name and model version.'
                }

        def raw_inference(*args):
            server_ip = args[0]
            http_port = args[1]
            metric_port = args[2]
            model_name = args[3]
            model_version = args[4]
            payload_text = args[5]
            server_addr = server_ip + ':' + http_port
            try:
                result = _http_manager.raw_infer(server_addr, model_name,
                                                 model_version, payload_text)
                results = {
                    status_text: 'Get response from server',
                    output_raw_text: result
                }
                if server_ip and metric_port:
745
                    html_table = get_metric_data(server_ip, metric_port, 'en')
746 747 748 749 750
                    results[output_html_table] = html_table
                return results
            except Exception as e:
                return {status_text: 'Error: {}'.format(e)}

751
        def update_metric(server_ip, metrics_port, lang_text):
752 753
            if server_ip and metrics_port:
                try:
754
                    html_table = get_metric_data(server_ip, metrics_port, 'en')
755 756
                    return {
                        output_html_table: html_table,
757
                        status_text: "Update metrics successfully."
758 759 760 761 762 763 764 765 766 767 768 769
                    }
                except Exception as e:
                    return {status_text: 'Error: {}'.format(e)}
            else:
                return {
                    status_text: 'Please input server ip and metrics_port.'
                }

        check_button.click(
            fn=get_input_output_name,
            inputs=[
                server_addr_text, server_http_port_text, model_name_text,
770
                model_version_text, lang_text
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
            ],
            outputs=[
                *all_input_output_components, check_button,
                component_format_column, status_text
            ])
        component_submit_button.click(
            fn=component_inference,
            inputs=[
                server_addr_text, server_http_port_text,
                server_metric_port_text, model_name_text, model_version_text,
                *input_name_texts, *input_images, *input_texts, task_radio
            ],
            outputs=[
                *output_name_texts, *output_images, *output_texts, status_text,
                output_html_table
            ])
        raw_submit_button.click(
            fn=raw_inference,
            inputs=[
                server_addr_text, server_http_port_text,
                server_metric_port_text, model_name_text, model_version_text,
                raw_payload_text
            ],
            outputs=[output_raw_text, status_text, output_html_table])
        update_metric_button.click(
            fn=update_metric,
797
            inputs=[server_addr_text, server_metric_port_text, lang_text],
798 799
            outputs=[output_html_table, status_text])
    return block