main.html 10.2 KB
Newer Older
走神的阿圆's avatar
走神的阿圆 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{# coding: utf-8 #}
{# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved. #}

{# 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. #}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
走神的阿圆's avatar
走神的阿圆 已提交
19 20
    <title>Hub-Serving</title>
    <link rel="shortcut icon" href="https://paddlepaddle-org-cn.bj.bcebos.com/paddle-site-front/favicon.ico"/>
走神的阿圆's avatar
走神的阿圆 已提交
21 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
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<div class="input-group mb-3">
    <table class="table table-striped table-dark table-hover">
        <tr>
            <td style="width: 17%"></td>
            <td style="width: 5%" valign="top">
                <label class="input-group-text" for="inputGroupSelect01" style="color: white;
                background-color: rgba(0,0,0,0); border: 0px; font-size: 20px">分类
                </label>
            </td>
            <td style="width: 25%">
                <select class="custom-select" id="inputGroupSelect01"
                        onchange="select_category(this.options[this.options.selectedIndex].value)">
                    <option selected>Choose...</option>
                </select>
            </td>
            <td style="width: 6%"></td>
            <td style="width: 5%">
                <label class="input-group-text" for="inputGroupSelect02" style="color: white;
                background-color: rgba(0,0,0,0); border: 0px; font-size: 20px">模型
                </label>
            </td>
            <td style="width: 25%">
                <select class="custom-select" id="inputGroupSelect02"
                        onchange="select_module(this.options[this.options.selectedIndex].value)">
                    <option selected value="Choose...">请先选择分类...</option>
                </select>
            </td>
            <td style="width: 17%"></td>
        </tr>
    </table>
</div>
<form id="main_form">
    <div class="input-group mb-3" id="display_div">

    </div>
</form>
<script>
走神的阿圆's avatar
走神的阿圆 已提交
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
    var module_info = {};

    $.ajax({
        type: "POST",
        url: "/get/modules",
        data: "",
        dataType: "json",
        async: false,
        success: function (res) {
            module_info = res.module_info;
            console.log(res);
            console.log("mo=", module_info);
            if (module_info.hasOwnProperty("nlp_module"))
            {
                s = document.getElementById("inputGroupSelect01");
                s.options.add(new Option("nlp", "nlp_module"));
            }
            if (module_info.hasOwnProperty("cv_module"))
            {
                s = document.getElementById("inputGroupSelect01");
                s.options.add(new Option("cv", "cv_module"));
            }
        }
    });

走神的阿圆's avatar
走神的阿圆 已提交
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 261
    function get_module_option(module_categoty) {
        options = module_info[module_categoty];
        html = "";
        for (var i=0, len=options.length; i<len; i++){
            option_name = Object.keys(options[i])[0];
            option_value = options[i][option_name];
            html = html
            + "<option value='"
            + option_value
            + "'>"
            + option_name
            + "</option>";
        }
        return html;
    }
    function get_single_text_html() {
        html = ""
        + '<p style="width: 5%"></p>'
        + '<p>请输入文字或者上传一个文本文件</p>'
        + '<p style="width: 2%"></p>'
        + '<input type="file" id="file" onchange="handle_text(this.files)"/>'
        +
            '<table class="table table-striped table-dark table-hover" style="width: 95%"; align="center">'
            + '<tr>'
                + '<td>'
                    + '<textarea cols="100" rows="30"'
                        + 'class="form-control"'
                        + 'id="file_text"'
                        + 'onblur="blur_input_text()"'
                        + 'onfocus="focus_input_text()"'
                        + 'name="input_text">'
                        + '在此键入文本或上传文本文件'
                    + '</textarea>'
                + '</td>'
                + '<td>'
                    +
            '<input type="button" value="Go!" onclick="sub_text()" class="btn-circle"></input>'
                + '</td>'
                + '<td>'
                    + '<textarea cols="100" rows="30"'
                        + 'class="form-control"'
                        + 'id="result_text"'

                        + 'name="result_text_name">'

                    + '</textarea>'
                + '</td>'
            + '</tr>'
        + '</table>';
        return html;
    }
    function blur_input_text()
    {
        text = document.getElementById("file_text");
        if (text.value == ""){
            text.value = "在此键入文本或上传文本文件";
        }
    }
    function focus_input_text(){
        text = document.getElementById("file_text");
        if (text.value == "在此键入文本或上传文本文件"){
            text.value = "";
        }
    }
    function get_single_img_html() {
        html = ""
        + '<p>请上传一个图片文件</p>'
        + '<input type="file" id="file" onchange="handle_img(this.files)"/>'
        + '<table class="table table-striped table-dark table-hover">'
            + '<tr>'
                + '<td style="width: 45%">'
                    + '<img width="100%" id="file_img" name="input_img" value="kitten.jpg"/>'
                + '</td>'
                + '<td>'
                    + '<input type="button" value="Start!" onclick="sub_img()"></input>'
                + '</td>'
                + '<td style="width: 45%"><table>'
                    + '<tr><td>'
                        + '<img width="100%" id="result_img" name="output_img"/>'
                    + '</td></tr>'
                    + '<tr><td>'
                        + '<textarea cols="100" rows="2"'
                            + 'class="form-control"'
                            + 'id="result_text"'
                            + 'name="result_text_name">'
                        + '</textarea>'
                    + '</td></tr>'
                + '</table></td>'
            + '</tr>'
        + '</table>';
        return html;
    }
    function select_category(module_categoty) {
        select_module("Choose...");
        option_html = get_module_option(module_categoty);
        document.getElementById("inputGroupSelect02").innerHTML = option_html;
    }
    function select_module(module_name){
        if (module_name == "Choose..."){
            display_html = ""
        }else{
            if (document.getElementById("inputGroupSelect01").value == "nlp_module"){
                display_html = get_single_text_html();
            }else if (document.getElementById("inputGroupSelect01").value == "cv_module"){
                display_html = get_single_img_html();
            }

        }
        document.getElementById("display_div").innerHTML = display_html;
    }
    function handle_text(files) {
        if (files.length){
            let file = files[0];
            let reader = new FileReader();
            reader.onload = function(){
                document.getElementById('file_text').value = this.result;
            };
            reader.readAsText(file);
        }
    }
    function handle_img(files) {
        if (files.length){
            let file = files[0];
            let reader = new FileReader();
            reader.onload = function(){
                document.getElementById('file_img').src = this.result;
            };
            reader.readAsDataURL(file);
        }
    }
    function get_result_html(results) {
        html = "";
        for (var i=0, len=results.length; i<len; ++i){
            html = html
            + JSON.stringify(results[i])
            + '\r\n'
        }
        return html;
    }
    function sub_text() {
        var formParam = $("#main_form").serialize();
        to_url = "/predict/text/" + document.getElementById("inputGroupSelect02").value;
        $.ajax({
                cache: true,
                type: "POST",
                url:to_url,
                data:formParam,
                async: false,
                error: function(request) {
                    alert("Connection error:"+request.error);
                },
                success: function(data) {
                    html = get_result_html(data["result"]);
                    document.getElementById("result_text").value = html;
                }
            });
    }
    function sub_img() {
        var formParam = {
            "input_img": document.getElementById("file_img").src,
            "input_file":document.getElementById("file").value
        };
        to_url = "/predict/image/" + document.getElementById("inputGroupSelect02").value;
        $.ajax({
                cache: true,
                type: "POST",
                url:to_url,
                data:formParam,
                async: false,
                error: function(request) {
                    alert("Connection error:"+request.error);
                },
                success: function(data) {
                    data = data["result"];
走神的阿圆's avatar
走神的阿圆 已提交
262
                    document.getElementById("result_text").value = data["desc"];
走神的阿圆's avatar
走神的阿圆 已提交
263 264 265 266 267 268 269
                    document.getElementById("result_img").src = data["output_img"];
                }
            });
    }
</script>
</body>
</html>