server.py 2.9 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#coding:utf-8
# Copyright (c) 2020  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 collections import OrderedDict
W
wuzewu 已提交
17
from typing import List
W
wuzewu 已提交
18 19 20 21 22 23 24

from paddlehub.server import ServerSource, GitSource

PADDLEHUB_PUBLIC_SERVER = 'http://paddlepaddle.org.cn/paddlehub'


class HubServer(object):
W
wuzewu 已提交
25
    '''PaddleHub server'''
W
wuzewu 已提交
26

W
wuzewu 已提交
27 28 29
    def __init__(self):
        self.sources = OrderedDict()

W
wuzewu 已提交
30 31
    def _generate_source(self, url: str, source_type: str = 'server'):
        if source_type == 'server':
W
wuzewu 已提交
32
            source = ServerSource(url)
W
wuzewu 已提交
33
        elif source_type == 'git':
W
wuzewu 已提交
34 35 36 37 38
            source = GitSource(url)
        else:
            raise RuntimeError()
        return source

W
wuzewu 已提交
39
    def add_source(self, url: str, key: str = None, source_type: str = 'server'):
W
wuzewu 已提交
40
        '''Add a module source(GitSource or ServerSource)'''
W
wuzewu 已提交
41
        key = "source_{}".format(len(self.sources)) if not key else key
W
wuzewu 已提交
42
        self.sources[key] = self._generate_source(url, source_type)
W
wuzewu 已提交
43

W
wuzewu 已提交
44 45
    def remove_source(self, url: str = None, key: str = None):
        '''Remove a module source'''
W
wuzewu 已提交
46 47
        self.sources.pop(key)

W
wuzewu 已提交
48
    def search_module(self, name: str, version: str = None, source: str = None) -> List[dict]:
W
wuzewu 已提交
49 50 51 52 53 54 55
        '''
        Search PaddleHub module

        Args:
            name(str) : PaddleHub module name
            version(str) : PaddleHub module version
        '''
W
wuzewu 已提交
56
        return self.search_resource(type='module', name=name, version=version, source=source)
W
wuzewu 已提交
57

W
wuzewu 已提交
58
    def search_resource(self, type: str, name: str, version: str = None, source: str = None) -> List[dict]:
W
wuzewu 已提交
59 60 61 62 63 64 65 66
        '''
        Search PaddleHub Resource

        Args:
            type(str) : Resource type
            name(str) : Resource name
            version(str) : Resource version
        '''
W
wuzewu 已提交
67 68
        sources = self.sources.values() if not source else [self._generate_source(source)]
        for source in sources:
W
wuzewu 已提交
69 70 71
            result = source.search_resource(name=name, type=type, version=version)
            if result:
                return result
W
wuzewu 已提交
72
        return []
W
wuzewu 已提交
73 74 75 76 77 78 79

    def get_module_info(self, name: str, source: str = None) -> dict:
        '''
        '''
        sources = self.sources.values() if not source else [self._generate_source(source)]
        for source in sources:
            result = source.get_module_info(name=name)
W
wuzewu 已提交
80 81
            if result:
                return result
W
wuzewu 已提交
82
        return {}
W
wuzewu 已提交
83 84 85 86


module_server = HubServer()
module_server.add_source(PADDLEHUB_PUBLIC_SERVER)