gradio/build-interface.py

52 lines
1.6 KiB
Python
Raw Normal View History

from argparse import ArgumentParser
import gradio
import numpy as np
import signal
import time
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)
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-10 09:48:52 +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)
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):
print('Shutting server down due to signal %d' % signum)
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
if __name__ == "__main__":
launch_interface(args)