hub_server.py 6.4 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
W
wuzewu 已提交
18

W
wuzewu 已提交
19
import os
20
import time
W
wuzewu 已提交
21 22 23 24 25

from paddlehub.common import utils
from paddlehub.common.downloader import default_downloader
from paddlehub.io.reader import csv_reader
import paddlehub as hub
W
wuzewu 已提交
26

27
MODULE_LIST_FILE = "module_list_file.csv"
W
wuzewu 已提交
28
MODEL_LIST_FILE = "model_list_file.csv"
29
CACHE_TIME = 60 * 10
W
wuzewu 已提交
30 31 32 33 34 35 36 37


class HubServer:
    def __init__(self, server_url=None):
        if not server_url:
            server_url = "https://paddlehub.bj.bcebos.com/"
        utils.check_url(server_url)
        self.server_url = server_url
38
        self._load_module_list_file_if_valid()
W
wuzewu 已提交
39
        self._load_model_list_file_if_valid()
40 41 42 43

    def module_list_file_path(self):
        return os.path.join(hub.CACHE_HOME, MODULE_LIST_FILE)

W
wuzewu 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    def model_list_file_path(self):
        return os.path.join(hub.CACHE_HOME, MODEL_LIST_FILE)

    def _load_model_list_file_if_valid(self):
        self.model_list_file = {}
        if not os.path.exists(self.model_list_file_path()):
            return False
        file_create_time = os.path.getctime(self.model_list_file_path())
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
            os.remove(self.model_list_file_path())
            return False
        self.model_list_file = csv_reader.read(self.model_list_file_path())

        # if file do not contain necessary data, remove it
        if "version" not in self.model_list_file or "model_name" not in self.model_list_file:
            self.model_list_file = {}
            os.remove(self.model_list_file_path())
            return False
        return True

67 68 69 70 71 72 73 74 75 76 77 78 79 80
    def _load_module_list_file_if_valid(self):
        self.module_list_file = {}
        if not os.path.exists(self.module_list_file_path()):
            return False
        file_create_time = os.path.getctime(self.module_list_file_path())
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
            os.remove(self.module_list_file_path())
            return False
        self.module_list_file = csv_reader.read(self.module_list_file_path())

        # if file do not contain necessary data, remove it
Z
Zeyu Chen 已提交
81
        if "version" not in self.module_list_file or "module_name" not in self.module_list_file:
82 83 84 85
            self.module_list_file = {}
            os.remove(self.module_list_file_path())
            return False
        return True
W
wuzewu 已提交
86 87

    def search_module(self, module_key, update=False):
88
        if update or not self.module_list_file:
W
wuzewu 已提交
89 90 91 92 93 94 95 96 97 98 99 100
            self.request()

        match_module_index_list = [
            index
            for index, module in enumerate(self.module_list_file['module_name'])
            if module_key in module
        ]

        return [(self.module_list_file['module_name'][index],
                 self.module_list_file['version'][index])
                for index in match_module_index_list]

W
wuzewu 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    def search_model(self, model_key, update=False):
        if update or not self.model_list_file:
            self.request_model()

        match_model_index_list = [
            index
            for index, model in enumerate(self.model_list_file['model_name'])
            if model_key in model
        ]

        return [(self.model_list_file['model_name'][index],
                 self.model_list_file['version'][index])
                for index in match_model_index_list]

W
wuzewu 已提交
115
    def get_module_url(self, module_name, version=None, update=False):
116
        if update or not self.module_list_file:
W
wuzewu 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            self.request()

        module_index_list = [
            index
            for index, module in enumerate(self.module_list_file['module_name'])
            if module == module_name
        ]
        module_version_list = [
            self.module_list_file['version'][index]
            for index in module_index_list
        ]
        #TODO(wuzewu): version sort method
        module_version_list = sorted(module_version_list)
        if not version:
            if not module_version_list:
                return None
            version = module_version_list[-1]

        for index in module_index_list:
            if self.module_list_file['version'][index] == version:
                return self.module_list_file['url'][index]

        return None

W
wuzewu 已提交
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
    def get_model_url(self, model_name, version=None, update=False):
        if update or not self.model_list_file:
            self.request_model()

        model_index_list = [
            index
            for index, model in enumerate(self.model_list_file['model_name'])
            if model == model_name
        ]
        model_version_list = [
            self.model_list_file['version'][index] for index in model_index_list
        ]
        #TODO(wuzewu): version sort method
        model_version_list = sorted(model_version_list)
        if not version:
            if not model_version_list:
                return None
            version = model_version_list[-1]

        for index in model_index_list:
            if self.model_list_file['version'][index] == version:
                return self.model_list_file['url'][index]

        return None

W
wuzewu 已提交
166 167
    def request(self):
        file_url = self.server_url + MODULE_LIST_FILE
W
wuzewu 已提交
168
        result, tips, self.module_list_file = default_downloader.download_file(
W
wuzewu 已提交
169
            file_url, save_path=hub.CACHE_HOME)
W
wuzewu 已提交
170 171
        if not result:
            return False
172
        return self._load_module_list_file_if_valid()
W
wuzewu 已提交
173

W
wuzewu 已提交
174 175 176 177 178 179 180 181
    def request_model(self):
        file_url = self.server_url + MODEL_LIST_FILE
        result, tips, self.model_list_file = default_downloader.download_file(
            file_url, save_path=hub.CACHE_HOME)
        if not result:
            return False
        return self._load_model_list_file_if_valid()

W
wuzewu 已提交
182 183

default_hub_server = HubServer()