hub_server.py 5.3 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

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

W
wuzewu 已提交
27
RESOURCE_LIST_FILE = "resource_list_file.yml"
28
CACHE_TIME = 60 * 10
W
wuzewu 已提交
29 30 31 32 33 34 35 36


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
W
wuzewu 已提交
37
        self._load_resource_list_file_if_valid()
38

W
wuzewu 已提交
39 40
    def resource_list_file_path(self):
        return os.path.join(hub.CACHE_HOME, RESOURCE_LIST_FILE)
41

W
wuzewu 已提交
42 43 44
    def _load_resource_list_file_if_valid(self):
        self.resource_list_file = {}
        if not os.path.exists(self.resource_list_file_path()):
W
wuzewu 已提交
45
            return False
W
wuzewu 已提交
46
        file_create_time = os.path.getctime(self.resource_list_file_path())
47 48 49 50
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
W
wuzewu 已提交
51
            os.remove(self.resource_list_file_path())
52
            return False
W
wuzewu 已提交
53 54 55 56 57 58
        for resource in yaml_reader.read(
                self.resource_list_file_path())['resource_list']:
            for key in resource:
                if key not in self.resource_list_file:
                    self.resource_list_file[key] = []
                self.resource_list_file[key].append(resource[key])
59 60

        # if file do not contain necessary data, remove it
W
wuzewu 已提交
61 62 63
        if "version" not in self.resource_list_file or "name" not in self.resource_list_file:
            self.resource_list_file = {}
            os.remove(self.resource_list_file_path())
64 65
            return False
        return True
W
wuzewu 已提交
66

W
wuzewu 已提交
67 68
    def search_resource(self, resource_key, resource_type=None, update=False):
        if update or not self.resource_list_file:
W
wuzewu 已提交
69 70
            self.request()

W
wuzewu 已提交
71
        match_resource_index_list = [
W
wuzewu 已提交
72
            index
W
wuzewu 已提交
73 74 75 76
            for index, resource in enumerate(self.resource_list_file['name'])
            if resource_key in resource and (
                resource_type is None
                or self.resource_list_file['type'][index] == resource_type)
W
wuzewu 已提交
77 78
        ]

W
wuzewu 已提交
79 80 81 82 83
        return [(self.resource_list_file['name'][index],
                 self.resource_list_file['type'][index],
                 self.resource_list_file['version'][index],
                 self.resource_list_file['summary'][index])
                for index in match_resource_index_list]
W
wuzewu 已提交
84

W
wuzewu 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
    def search_module(self, module_key, update=False):
        self.search_resource(
            resource_key=module_key, resource_type="Module", update=update)

    def search_model(self, module_key, update=False):
        self.search_resource(
            resource_key=module_key, resource_type="Model", update=update)

    def get_resource_url(self,
                         resource_name,
                         resource_type=None,
                         version=None,
                         update=False):
        if update or not self.resource_list_file:
W
wuzewu 已提交
99 100
            self.request()

W
wuzewu 已提交
101
        resource_index_list = [
W
wuzewu 已提交
102
            index
W
wuzewu 已提交
103 104 105 106
            for index, resource in enumerate(self.resource_list_file['name'])
            if resource == resource_name and (
                resource_type is None
                or self.resource_list_file['type'][index] == resource_type)
W
wuzewu 已提交
107
        ]
W
wuzewu 已提交
108 109 110
        resource_version_list = [
            self.resource_list_file['version'][index]
            for index in resource_index_list
W
wuzewu 已提交
111 112
        ]
        #TODO(wuzewu): version sort method
W
wuzewu 已提交
113
        resource_version_list = sorted(resource_version_list)
W
wuzewu 已提交
114
        if not version:
W
wuzewu 已提交
115
            if not resource_version_list:
W
wuzewu 已提交
116
                return None
W
wuzewu 已提交
117
            version = resource_version_list[-1]
W
wuzewu 已提交
118

W
wuzewu 已提交
119 120 121
        for index in resource_index_list:
            if self.resource_list_file['version'][index] == version:
                return self.resource_list_file['url'][index]
W
wuzewu 已提交
122 123 124

        return None

W
wuzewu 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
    def get_module_url(self, module_name, version=None, update=False):
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Module",
            version=version,
            update=update)

    def get_model_url(self, module_name, version=None, update=False):
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Model",
            version=version,
            update=update)
W
wuzewu 已提交
138

W
wuzewu 已提交
139
    def request(self):
W
wuzewu 已提交
140 141
        file_url = self.server_url + RESOURCE_LIST_FILE
        result, tips, self.resource_list_file = default_downloader.download_file(
W
wuzewu 已提交
142 143 144
            file_url, save_path=hub.CACHE_HOME)
        if not result:
            return False
W
wuzewu 已提交
145
        return self._load_resource_list_file_if_valid()
W
wuzewu 已提交
146

W
wuzewu 已提交
147 148

default_hub_server = HubServer()