2019-03-02 11:21:56 +08:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
import gradio
|
2019-03-06 14:53:59 +08:00
|
|
|
import numpy as np
|
2019-03-10 09:47:21 +08:00
|
|
|
import signal
|
|
|
|
import time
|
2019-03-02 11:21:56 +08:00
|
|
|
|
|
|
|
parser = ArgumentParser(description='Arguments for Building Interface')
|
|
|
|
parser.add_argument('-i', '--inputs', type=str, help="name of input interface")
|
|
|
|
parser.add_argument('-o', '--outputs', type=str, help="name of output interface")
|
2019-03-10 09:15:04 +08:00
|
|
|
parser.add_argument('-d', '--delay', type=int, help="delay in processing", default=0)
|
2019-03-05 14:35:47 +08:00
|
|
|
share_parser = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
share_parser.add_argument('--share', dest='share', action='store_true')
|
|
|
|
share_parser.add_argument('--no-share', dest='share', action='store_false')
|
|
|
|
parser.set_defaults(share=False)
|
2019-03-02 11:21:56 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
2019-03-10 09:15:04 +08:00
|
|
|
def mdl(input):
|
2019-03-10 09:48:52 +08:00
|
|
|
time.sleep(args.delay)
|
2019-04-11 01:50:43 +08:00
|
|
|
return np.array(1)
|
2019-03-02 11:21:56 +08:00
|
|
|
|
2019-03-10 09:48:52 +08:00
|
|
|
|
2019-03-02 11:21:56 +08:00
|
|
|
def launch_interface(args):
|
2019-04-11 01:50:43 +08:00
|
|
|
io = gradio.Interface(inputs=args.inputs, outputs=args.outputs, model=mdl, model_type='pyfunc')
|
2019-04-11 00:30:41 +08:00
|
|
|
httpd, _, _ = io.launch(share=args.share, validate=False)
|
2019-03-10 09:47:21 +08:00
|
|
|
|
|
|
|
class ServiceExit(Exception):
|
|
|
|
"""
|
|
|
|
Custom exception which is used to trigger the clean exit
|
|
|
|
of all running threads and the main program.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def service_shutdown(signum, frame):
|
2020-06-10 17:34:24 +08:00
|
|
|
print('Shutting server down due to signal {}'.format(signum))
|
2019-03-10 09:47:21 +08:00
|
|
|
httpd.shutdown()
|
|
|
|
raise ServiceExit
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, service_shutdown)
|
|
|
|
signal.signal(signal.SIGINT, service_shutdown)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Keep the main thread running, otherwise signals are ignored.
|
|
|
|
while True:
|
|
|
|
time.sleep(0.5)
|
|
|
|
except ServiceExit:
|
|
|
|
pass
|
2019-03-02 11:21:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
launch_interface(args)
|