Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
d78604b5
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
1 年多 前同步成功
通知
283
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d78604b5
编写于
11月 16, 2020
作者:
走神的阿圆
提交者:
GitHub
11月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update cache info if necessary
上级
35b00882
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
149 addition
and
12 deletion
+149
-12
paddlehub/commands/convert.py
paddlehub/commands/convert.py
+2
-0
paddlehub/commands/download.py
paddlehub/commands/download.py
+2
-0
paddlehub/commands/install.py
paddlehub/commands/install.py
+2
-0
paddlehub/commands/run.py
paddlehub/commands/run.py
+2
-0
paddlehub/commands/search.py
paddlehub/commands/search.py
+3
-1
paddlehub/commands/serving.py
paddlehub/commands/serving.py
+12
-8
paddlehub/config.py
paddlehub/config.py
+52
-1
paddlehub/module/module.py
paddlehub/module/module.py
+3
-0
paddlehub/server/server.py
paddlehub/server/server.py
+68
-0
paddlehub/serving/device.py
paddlehub/serving/device.py
+3
-2
未找到文件。
paddlehub/commands/convert.py
浏览文件 @
d78604b5
...
...
@@ -22,6 +22,7 @@ from string import Template
from
paddlehub.env
import
TMP_HOME
as
tmp_dir
from
paddlehub.commands
import
register
from
paddlehub.utils.xarfile
import
XarFile
from
paddlehub.server.server
import
CacheUpdater
INIT_FILE
=
'__init__.py'
MODULE_FILE
=
'module.py'
...
...
@@ -113,6 +114,7 @@ class ConvertCommand:
return
False
self
.
module
=
args
.
module_name
self
.
version
=
args
.
module_version
if
args
.
module_version
is
not
None
else
'1.0.0'
CacheUpdater
(
"hub_convert"
,
module
=
self
.
module
,
version
=
self
.
version
).
start
()
self
.
src
=
args
.
model_dir
if
not
os
.
path
.
isdir
(
self
.
src
):
print
(
'`{}` is not exists or not a directory path'
.
format
(
self
.
src
))
...
...
paddlehub/commands/download.py
浏览文件 @
d78604b5
...
...
@@ -19,6 +19,7 @@ import paddlehub as hub
from
paddlehub.commands
import
register
from
paddlehub.server
import
module_server
from
paddlehub.utils
import
utils
,
log
from
paddlehub.server.server
import
CacheUpdater
@
register
(
name
=
'hub.download'
,
description
=
'Download PaddlePaddle pretrained module files.'
)
...
...
@@ -30,6 +31,7 @@ class DownloadCommand:
for
_arg
in
argv
:
result
=
module_server
.
search_module
(
_arg
)
CacheUpdater
(
"hub_download"
,
_arg
).
start
()
if
result
:
url
=
result
[
0
][
'url'
]
with
log
.
ProgressBar
(
'Download {}'
.
format
(
url
))
as
bar
:
...
...
paddlehub/commands/install.py
浏览文件 @
d78604b5
...
...
@@ -20,6 +20,7 @@ from typing import List
from
paddlehub.commands
import
register
from
paddlehub.module.manager
import
LocalModuleManager
from
paddlehub.utils
import
xarfile
from
paddlehub.server.server
import
CacheUpdater
@
register
(
name
=
'hub.install'
,
description
=
'Install PaddleHub module.'
)
...
...
@@ -39,5 +40,6 @@ class InstallCommand:
_arg
=
_arg
.
split
(
'=='
)
name
=
_arg
[
0
]
version
=
None
if
len
(
_arg
)
==
1
else
_arg
[
1
]
CacheUpdater
(
"hub_install"
,
name
,
version
).
start
()
manager
.
install
(
name
=
name
,
version
=
version
)
return
True
paddlehub/commands/run.py
浏览文件 @
d78604b5
...
...
@@ -22,6 +22,7 @@ from paddlehub.compat.module.module_v1 import ModuleV1
from
paddlehub.commands
import
register
from
paddlehub.module.manager
import
LocalModuleManager
from
paddlehub.module.module
import
Module
,
InvalidHubModule
from
paddlehub.server.server
import
CacheUpdater
@
register
(
name
=
'hub.run'
,
description
=
'Run the specific module.'
)
...
...
@@ -31,6 +32,7 @@ class RunCommand:
print
(
'ERROR: You must give one module to run.'
)
return
False
module_name
=
argv
[
0
]
CacheUpdater
(
"hub_run"
,
module_name
).
start
()
if
os
.
path
.
exists
(
module_name
)
and
os
.
path
.
isdir
(
module_name
):
try
:
...
...
paddlehub/commands/search.py
浏览文件 @
d78604b5
...
...
@@ -21,6 +21,7 @@ from paddlehub.commands import register
from
paddlehub.module.manager
import
LocalModuleManager
from
paddlehub.server.server
import
module_server
from
paddlehub.utils
import
log
,
platform
from
paddlehub.server.server
import
CacheUpdater
@
register
(
name
=
'hub.search'
,
description
=
'Search PaddleHub pretrained model through model keywords.'
)
...
...
@@ -31,8 +32,9 @@ class SearchCommand:
widths
=
[
20
,
8
,
30
]
if
platform
.
is_windows
()
else
[
30
,
8
,
40
]
table
=
log
.
Table
(
widths
=
widths
)
table
.
append
(
*
[
'ModuleName'
,
'Version'
,
'Summary'
],
aligns
=
[
'^'
,
'^'
,
'^'
],
colors
=
[
"blue"
,
"blue"
,
"blue"
])
CacheUpdater
(
"hub_search"
,
argv
).
start
()
results
=
module_server
.
search_module
(
name
=
argv
)
for
result
in
results
:
table
.
append
(
result
[
'name'
],
result
[
'version'
],
result
[
'summary'
])
...
...
paddlehub/commands/serving.py
浏览文件 @
d78604b5
...
...
@@ -16,7 +16,6 @@
import
argparse
import
os
import
platform
import
socket
import
json
import
multiprocessing
import
time
...
...
@@ -29,6 +28,7 @@ from paddlehub.env import CONF_HOME
from
paddlehub.serving.http_server
import
run_all
,
StandaloneApplication
from
paddlehub.utils
import
log
from
paddlehub.utils.utils
import
is_port_occupied
from
paddlehub.server.server
import
CacheUpdater
def
number_of_workers
():
...
...
@@ -103,6 +103,9 @@ class ServingCommand:
if
info
is
False
:
return
pid
=
info
[
"pid"
]
module
=
info
[
"module"
]
start_time
=
info
[
"start_time"
]
CacheUpdater
(
"hub_serving_stop"
,
module
=
module
,
addition
=
{
"period_time"
:
time
.
time
()
-
start_time
}).
start
()
if
os
.
path
.
exists
(
filepath
):
os
.
remove
(
filepath
)
...
...
@@ -110,7 +113,6 @@ class ServingCommand:
log
.
logger
.
info
(
"PaddleHub Serving has been stopped."
)
return
log
.
logger
.
info
(
"PaddleHub Serving will stop."
)
# CacheUpdater("hub_serving_stop", module=module, addition={"period_time": time.time() - start_time}).start()
if
platform
.
system
()
==
"Windows"
:
os
.
kill
(
pid
,
signal
.
SIGTERM
)
else
:
...
...
@@ -132,7 +134,7 @@ class ServingCommand:
from
paddle_gpu_serving.run
import
BertServer
bs
=
BertServer
(
with_gpu
=
args
.
use_gpu
)
bs
.
with_model
(
model_name
=
args
.
modules
[
0
])
#
CacheUpdater("hub_bert_service", module=args.modules[0], version="0.0.0").start()
CacheUpdater
(
"hub_bert_service"
,
module
=
args
.
modules
[
0
],
version
=
"0.0.0"
).
start
()
bs
.
run
(
gpu_index
=
args
.
gpu
,
port
=
int
(
args
.
port
))
def
preinstall_modules
(
self
):
...
...
@@ -141,8 +143,7 @@ class ServingCommand:
'''
for
key
,
value
in
self
.
modules_info
.
items
():
init_args
=
value
[
"init_args"
]
# CacheUpdater("hub_serving_start", module=key, version=init_args.get("version", "0.0.0")).start()
CacheUpdater
(
"hub_serving_start"
,
module
=
key
,
version
=
init_args
.
get
(
"version"
,
"0.0.0"
)).
start
()
if
"directory"
not
in
init_args
:
init_args
.
update
({
"name"
:
key
})
m
=
hub
.
Module
(
**
init_args
)
...
...
@@ -183,19 +184,22 @@ class ServingCommand:
Start one PaddleHub-Serving instance by arguments with zmq.
'''
if
self
.
modules_info
is
not
None
:
for
module
,
info
in
self
.
modules_info
.
items
():
CacheUpdater
(
"hub_serving_start"
,
module
=
module
,
version
=
info
[
'init_args'
][
'version'
]).
start
()
front_port
=
self
.
args
.
port
if
is_port_occupied
(
"127.0.0.1"
,
front_port
)
is
True
:
log
.
logger
.
error
(
"Port %s is occupied, please change it."
%
front_port
)
return
False
back_port
=
int
(
front_port
)
+
1
for
index
in
range
(
100
):
if
is_port_occupied
(
"127.0.0.1"
,
back_port
):
if
not
is_port_occupied
(
"127.0.0.1"
,
back_port
):
break
else
:
back_port
=
int
(
back_port
)
+
1
else
:
raise
RuntimeError
(
"Port from %s to %s is occupied, please use another port"
%
int
(
front_port
)
+
1
,
back_port
)
raise
RuntimeError
(
"Port from %s to %s is occupied, please use another port"
%
(
int
(
front_port
)
+
1
,
back_port
))
self
.
dump_pid_file
()
run_all
(
self
.
modules_info
,
self
.
args
.
gpu
,
front_port
,
back_port
)
else
:
...
...
paddlehub/config.py
浏览文件 @
d78604b5
...
...
@@ -13,15 +13,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
hashlib
import
os
import
time
import
json
import
uuid
import
yaml
from
easydict
import
EasyDict
import
paddlehub.env
as
hubenv
def
md5
(
text
:
str
):
'''Calculate the md5 value of the input text.'''
md5code
=
hashlib
.
md5
(
text
.
encode
())
return
md5code
.
hexdigest
()
class
HubConfig
:
'''
PaddleHub configuration management class. Each time the PaddleHub package is loaded, PaddleHub will set the
...
...
@@ -106,6 +114,48 @@ class HubConfig:
return
yaml
.
dump
(
cfg
)
class
CacheConfig
(
object
):
def
__init__
(
self
):
self
.
_initialize
()
self
.
file
=
os
.
path
.
join
(
hubenv
.
CONF_HOME
,
'cache.yaml'
)
if
not
os
.
path
.
exists
(
self
.
file
):
self
.
flush
()
return
with
open
(
self
.
file
,
'r'
)
as
file
:
try
:
cfg
=
yaml
.
load
(
file
,
Loader
=
yaml
.
FullLoader
)
self
.
data
.
update
(
cfg
)
except
:
...
def
_initialize
(
self
):
# Set default configuration values.
self
.
data
=
EasyDict
()
hub_name
=
md5
(
str
(
uuid
.
uuid1
())[
-
12
:])
+
"-"
+
str
(
int
(
time
.
time
()))
self
.
data
.
hub_name
=
hub_name
@
property
def
hub_name
(
self
):
return
self
.
data
.
hub_name
@
hub_name
.
setter
def
hub_name
(
self
,
url
:
str
):
self
.
data
.
server
=
url
self
.
flush
()
def
flush
(
self
):
'''Flush the current configuration into the configuration file.'''
with
open
(
self
.
file
,
'w'
)
as
file
:
# convert EasyDict to dict
cfg
=
json
.
loads
(
json
.
dumps
(
self
.
data
))
yaml
.
dump
(
cfg
,
file
)
def
__str__
(
self
):
cfg
=
json
.
loads
(
json
.
dumps
(
self
.
data
))
return
yaml
.
dump
(
cfg
)
def
_load_old_config
(
config
:
HubConfig
):
# The old version of the configuration file is obsolete, read the configuration value and delete it.
old_cfg_file
=
os
.
path
.
join
(
hubenv
.
CONF_HOME
,
'config.json'
)
...
...
@@ -122,3 +172,4 @@ def _load_old_config(config: HubConfig):
config
=
HubConfig
()
_load_old_config
(
config
)
cache_config
=
CacheConfig
()
paddlehub/module/module.py
浏览文件 @
d78604b5
...
...
@@ -159,12 +159,15 @@ class Module(object):
branch
:
str
=
None
,
**
kwargs
):
if
cls
.
__name__
==
'Module'
:
from
paddlehub.server.server
import
CacheUpdater
# This branch come from hub.Module(name='xxx') or hub.Module(directory='xxx')
if
name
:
module
=
cls
.
init_with_name
(
name
=
name
,
version
=
version
,
source
=
source
,
update
=
update
,
branch
=
branch
,
**
kwargs
)
CacheUpdater
(
"update_cache"
,
module
=
name
,
version
=
version
).
start
()
elif
directory
:
module
=
cls
.
init_with_directory
(
directory
=
directory
,
**
kwargs
)
CacheUpdater
(
"update_cache"
,
module
=
directory
,
version
=
"0.0.0"
).
start
()
else
:
module
=
object
.
__new__
(
cls
)
...
...
paddlehub/server/server.py
浏览文件 @
d78604b5
...
...
@@ -13,10 +13,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
json
import
os
import
requests
import
threading
import
time
import
yaml
from
collections
import
OrderedDict
from
typing
import
List
import
paddle
import
paddlehub
import
paddlehub.config
as
hubconf
from
paddlehub.config
import
cache_config
from
paddlehub.server
import
ServerSource
,
GitSource
from
paddlehub.utils
import
utils
...
...
@@ -115,5 +125,63 @@ class HubServer(object):
return
{}
def
uri_path
(
server_url
,
api
):
srv
=
server_url
if
server_url
.
endswith
(
'/'
):
srv
=
server_url
[:
-
1
]
if
api
.
startswith
(
'/'
):
srv
+=
api
else
:
api
=
'/'
+
api
srv
+=
api
return
srv
def
hub_request
(
api
,
params
,
extra
=
None
,
timeout
=
8
):
params
[
'hub_version'
]
=
paddlehub
.
__version__
.
split
(
'-'
)[
0
]
params
[
'paddle_version'
]
=
paddle
.
__version__
.
split
(
'-'
)[
0
]
params
[
"extra"
]
=
json
.
dumps
(
extra
)
r
=
requests
.
get
(
api
,
params
,
timeout
=
timeout
)
return
r
.
json
()
class
CacheUpdater
(
threading
.
Thread
):
def
__init__
(
self
,
command
=
"update_cache"
,
module
=
None
,
version
=
None
,
addition
=
None
):
threading
.
Thread
.
__init__
(
self
)
self
.
command
=
command
self
.
module
=
module
self
.
version
=
version
self
.
addition
=
addition
def
update_resource_list_file
(
self
,
command
=
"update_cache"
,
module
=
None
,
version
=
None
,
addition
=
None
):
payload
=
{
'word'
:
module
}
if
version
:
payload
[
'version'
]
=
version
api_url
=
uri_path
(
hubconf
.
server
,
'search'
)
cache_path
=
os
.
path
.
join
(
"~"
)
hub_name
=
cache_config
.
hub_name
if
os
.
path
.
exists
(
cache_path
):
extra
=
{
"command"
:
command
,
"mtime"
:
os
.
stat
(
cache_path
).
st_mtime
,
"hub_name"
:
hub_name
}
else
:
extra
=
{
"command"
:
command
,
"mtime"
:
time
.
strftime
(
"%Y-%m-%d %H:%M:%S"
,
time
.
localtime
()),
"hub_name"
:
hub_name
}
if
addition
is
not
None
:
extra
.
update
({
"addition"
:
addition
})
try
:
r
=
hub_request
(
api_url
,
payload
,
extra
,
timeout
=
1
)
if
r
.
get
(
"update_cache"
,
0
)
==
1
:
with
open
(
cache_path
,
'w+'
)
as
fp
:
yaml
.
safe_dump
({
'resource_list'
:
r
[
'data'
]},
fp
)
except
Exception
as
err
:
pass
def
run
(
self
):
self
.
update_resource_list_file
(
self
.
command
,
self
.
module
,
self
.
version
,
self
.
addition
)
module_server
=
HubServer
()
module_server
.
add_source
(
hubconf
.
server
,
source_type
=
'server'
,
key
=
'default_hub_server'
)
paddlehub/serving/device.py
浏览文件 @
d78604b5
...
...
@@ -99,12 +99,13 @@ class InferenceServer(object):
if
platform
.
system
()
==
"Windows"
:
back_port
=
int
(
port
)
+
1
for
index
in
range
(
100
):
if
is_port_occupied
(
"127.0.0.1"
,
back_port
):
if
not
is_port_occupied
(
"127.0.0.1"
,
back_port
):
break
else
:
back_port
=
int
(
back_port
)
+
1
else
:
raise
RuntimeError
(
"Port from %s to %s is occupied, please use another port"
%
int
(
port
)
+
1
,
back_port
)
raise
RuntimeError
(
"Port from %s to %s is occupied, please use another port"
%
(
int
(
port
)
+
1
,
back_port
))
worker_backend
=
"tcp://localhost:%s"
%
back_port
backend
=
"tcp://*:%s"
%
back_port
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录