2020-07-17 03:56:36 +08:00
|
|
|
import json
|
2020-08-06 01:42:52 +08:00
|
|
|
from gradio.inputs import InputComponent
|
|
|
|
from gradio.outputs import OutputComponent
|
2020-07-17 03:56:36 +08:00
|
|
|
from gradio.interface import Interface
|
|
|
|
import inspect
|
2020-08-22 07:17:11 +08:00
|
|
|
from os import listdir
|
|
|
|
from os.path import join
|
|
|
|
import re
|
|
|
|
|
|
|
|
in_demos, out_demos = {}, {}
|
|
|
|
demo_regex = "# Demo: \((.*)\) -> \((.*)\)"
|
|
|
|
for demo in listdir("demo"):
|
|
|
|
if demo.endswith(".py"):
|
2020-08-26 07:36:45 +08:00
|
|
|
screenshots = listdir(join("demo/screenshots", demo[:-3]))[0]
|
|
|
|
demoset = [demo, [screenshots]]
|
2020-08-22 07:17:11 +08:00
|
|
|
with open(join("demo", demo)) as demo_file:
|
|
|
|
first_line = demo_file.readline()
|
|
|
|
match = re.match(demo_regex, first_line)
|
|
|
|
inputs = match.group(1).split(", ")
|
|
|
|
outputs = match.group(2).split(", ")
|
|
|
|
for i in inputs:
|
|
|
|
if i not in in_demos:
|
|
|
|
in_demos[i] = []
|
2020-08-26 07:36:45 +08:00
|
|
|
if demoset not in in_demos[i]:
|
2020-08-22 07:17:11 +08:00
|
|
|
in_demos[i].append(demoset)
|
|
|
|
for o in outputs:
|
|
|
|
if o not in out_demos:
|
|
|
|
out_demos[o] = []
|
2020-08-26 07:36:45 +08:00
|
|
|
if demoset not in out_demos[o]:
|
2020-08-22 07:17:11 +08:00
|
|
|
out_demos[o].append(demoset)
|
2020-07-17 03:56:36 +08:00
|
|
|
|
|
|
|
def get_params(func):
|
|
|
|
params_str = inspect.getdoc(func)
|
|
|
|
params_doc = []
|
|
|
|
documented_params = {"self"}
|
|
|
|
for param_line in params_str.split("\n")[1:]:
|
2020-08-11 07:51:40 +08:00
|
|
|
if param_line.strip() == "Returns":
|
|
|
|
break
|
2020-07-17 03:56:36 +08:00
|
|
|
space_index = param_line.index(" ")
|
|
|
|
colon_index = param_line.index(":")
|
|
|
|
name = param_line[:space_index]
|
|
|
|
documented_params.add(name)
|
|
|
|
params_doc.append((name, param_line[space_index+2:colon_index-1], param_line[colon_index+2:]))
|
|
|
|
params = inspect.getfullargspec(func)
|
|
|
|
param_set = []
|
|
|
|
for i in range(len(params.args)):
|
|
|
|
neg_index = -1 - i
|
|
|
|
if params.args[neg_index] not in documented_params:
|
|
|
|
continue
|
|
|
|
if i < len(params.defaults):
|
|
|
|
default = params.defaults[neg_index]
|
|
|
|
if type(default) == str:
|
|
|
|
default = '"' + default + '"'
|
|
|
|
else:
|
|
|
|
default = str(default)
|
|
|
|
param_set.insert(0, (params.args[neg_index], default))
|
|
|
|
else:
|
|
|
|
param_set.insert(0, (params.args[neg_index],))
|
|
|
|
return param_set, params_doc
|
|
|
|
|
2020-08-22 07:17:11 +08:00
|
|
|
def document(cls_set, demos):
|
2020-07-17 03:56:36 +08:00
|
|
|
docset = []
|
|
|
|
for cls in cls_set:
|
|
|
|
inp = {}
|
|
|
|
inp["name"] = cls.__name__
|
|
|
|
doc = inspect.getdoc(cls)
|
2020-08-14 03:59:23 +08:00
|
|
|
if doc.startswith("DEPRECATED"):
|
|
|
|
continue
|
2020-07-17 03:56:36 +08:00
|
|
|
inp["doc"] = "\n".join(doc.split("\n")[:-1])
|
|
|
|
inp["type"] = doc.split("\n")[-1].split("type: ")[-1]
|
|
|
|
inp["params"], inp["params_doc"] = get_params(cls.__init__)
|
|
|
|
inp["shortcuts"] = list(cls.get_shortcut_implementations().items())
|
2020-08-22 07:17:11 +08:00
|
|
|
cls_name = cls.__name__
|
|
|
|
if cls_name in demos:
|
|
|
|
inp["demos"] = demos.get(cls_name, [])
|
2020-07-17 03:56:36 +08:00
|
|
|
docset.append(inp)
|
|
|
|
return docset
|
|
|
|
|
2020-08-22 07:17:11 +08:00
|
|
|
inputs = document(InputComponent.__subclasses__(), in_demos)
|
|
|
|
outputs = document(OutputComponent.__subclasses__(), out_demos)
|
2020-07-17 03:56:36 +08:00
|
|
|
interface_params = get_params(Interface.__init__)
|
|
|
|
interface = {
|
|
|
|
"doc": inspect.getdoc(Interface),
|
|
|
|
"params": interface_params[0],
|
|
|
|
"params_doc": interface_params[1],
|
|
|
|
}
|
|
|
|
launch_params = get_params(Interface.launch)
|
|
|
|
launch = {
|
|
|
|
"params": launch_params[0],
|
|
|
|
"params_doc": launch_params[1],
|
|
|
|
}
|
|
|
|
|
|
|
|
with open("docs.json", "w") as docs:
|
|
|
|
json.dump({
|
|
|
|
"inputs": inputs,
|
|
|
|
"outputs": outputs,
|
|
|
|
"interface": interface,
|
2020-08-22 07:17:11 +08:00
|
|
|
"launch": launch,
|
2020-07-17 03:56:36 +08:00
|
|
|
}, docs)
|
|
|
|
|