Skip to content

Commit

Permalink
docs: build docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Col0ring committed Nov 4, 2024
1 parent a0176a5 commit f28deb1
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 108 deletions.
74 changes: 12 additions & 62 deletions docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os

import gradio as gr
from helper.Site import Site

import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms


def get_docs(file_path: str):
Expand Down Expand Up @@ -53,63 +52,14 @@ def get_docs(file_path: str):
}]
}]


def render_docs(items: list):
for item in items:
if "children" in item:
render_docs(item["children"])
elif "key" in item:
key = item["key"].replace("-", "_")
if key in docs:
with antd.Tabs.Item(key=key):
docs[key].docs.render()


def on_menu_select(e: gr.EventData):
selected_menu = e._data["payload"][0]["key"]
return gr.update(selected_keys=[selected_menu]), gr.update(
active_key=selected_menu)


with gr.Blocks(css="""
.gradio-container {
max-width: 100% !important;
padding: 0 !important;
}
""") as demo:
with ms.Application():
with antd.ConfigProvider():
with antd.Layout(elem_style=dict(
height=
"calc(100vh - var(--size-4) - var(--body-text-size) * 1.5)"
)):
with antd.Layout.Sider(elem_style=dict(
height="100vh", overflow="auto", position="relative")):
with antd.Flex(
justify="center",
elem_style=dict(
position="sticky",
top=0,
padding="8px 4px",
marginBottom=8,
borderBottom="1px solid rgba(255,255,255,0.2)")
):
antd.Image(os.path.join(os.path.dirname(__file__),
"./resources/modelscope.png"),
preview=False,
width="100%")
layout_menu = antd.Menu(
default_selected_keys=[default_active_key],
mode="inline",
theme="dark",
items=layout_menu_items)
with antd.Layout():
with antd.Layout.Content(
elem_style=dict(padding=12, overflow="auto")):
with antd.Tabs(
default_active_key=default_active_key,
render_tab_bar="() => null") as layout_tabs:
render_docs(layout_menu_items)
layout_menu.select(fn=on_menu_select, outputs=[layout_menu, layout_tabs])

demo.launch()
site = Site(menus=layout_menu_items,
docs=docs,
default_active_key=default_active_key,
logo=antd.Image(os.path.join(os.path.dirname(__file__),
"./resources/modelscope.png"),
preview=False,
width="100%"))
demo = site.render()

if __name__ == "__main__":
demo.launch()
87 changes: 41 additions & 46 deletions docs/helper/Docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,19 @@

from .parse_markdown import parse_markdown


def remove_formatter(markdown_text):
pattern = r"^ *---[\s\S]*?---"

replaced_text = re.sub(pattern, "", markdown_text)

return replaced_text


def list_demos(dir_path: str, prefix=''):
result = []
if (not os.path.isdir(dir_path)):
return result
for name in os.listdir(dir_path):
path = os.path.join(dir_path, name)

if os.path.isfile(path):
result.append(prefix + name)
elif os.path.isdir(path):
sub_prefix = prefix + name + '/'
result.extend(list_demos(path, sub_prefix))

return result


def get_demo_modules(file_path: str):
import importlib.util

demos = [
demo for demo in list_demos(
os.path.join(os.path.dirname(file_path), "demos"))
if demo.endswith(".py") and not demo.startswith("__")
]
demo_modules = {}
for demo in demos:
demo_name = demo.split(".")[0]
spec = importlib.util.spec_from_file_location(
"demo", os.path.join(os.path.dirname(file_path), "demos", demo))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
demo_modules[demo_name] = module
return demo_modules


is_modelscope_studio = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'


class Docs:

def __init__(self, file_path: str):
self.file_path = file_path
self.demo_modules = get_demo_modules(file_path)
# default current directory
self.markdown_files = [
filename for filename in os.listdir(os.path.dirname(file_path))
if filename.endswith(".md")
]
self.demo_modules = self._get_demo_modules()
if is_modelscope_studio:
self.markdown_files = list(
filter(
Expand All @@ -77,6 +33,45 @@ def __init__(self, file_path: str):
filter(lambda x: not x.endswith("-zh_CN.md"),
self.markdown_files))

def _remove_formatter(self, markdown_text):
pattern = r"^ *---[\s\S]*?---"
replaced_text = re.sub(pattern, "", markdown_text)
return replaced_text

def _list_demos(self, dir_path: str, prefix=''):
result = []
if (not os.path.isdir(dir_path)):
return result
for name in os.listdir(dir_path):
path = os.path.join(dir_path, name)

if os.path.isfile(path):
result.append(prefix + name)
elif os.path.isdir(path):
sub_prefix = prefix + name + '/'
result.extend(self._list_demos(path, sub_prefix))

return result

def _get_demo_modules(self):
import importlib.util

demos = [
demo for demo in self._list_demos(
os.path.join(os.path.dirname(self.file_path), "demos"))
if demo.endswith(".py") and not demo.startswith("__")
]
demo_modules = {}
for demo in demos:
demo_name = demo.split(".")[0]
spec = importlib.util.spec_from_file_location(
"demo",
os.path.join(os.path.dirname(self.file_path), "demos", demo))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
demo_modules[demo_name] = module
return demo_modules

def _read_file(self, relative_path: str):
with open(os.path.join(os.path.dirname(self.file_path), relative_path),
"r") as f:
Expand Down Expand Up @@ -106,7 +101,7 @@ def _render_demo(self, demo_name, prefix='', suffix='', fixed=False):
module.demo.render()

def _render_markdown(self, markdown_file):
items = parse_markdown(remove_formatter(
items = parse_markdown(self._remove_formatter(
self._read_file(markdown_file)),
read_file=self._read_file)
for item in items:
Expand Down
82 changes: 82 additions & 0 deletions docs/helper/Site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import gradio as gr
from gradio.components.base import Component

import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms


class Site:

def __init__(self,
menus: list,
docs: dict,
default_active_key: str | None = None,
logo: Component | None = None):
self.menus = menus
self.docs = docs
self.default_active_key = default_active_key
self.logo = logo

def _render_docs(self, items: list):
for item in items:
if "children" in item:
self._render_docs(item["children"])
elif "key" in item:
key = item["key"].replace("-", "_")
if key in self.docs:
with antd.Tabs.Item(key=key):
self.docs[key].docs.render()

def render(self):

def on_menu_select(e: gr.EventData):
selected_menu = e._data["payload"][0]["key"]
return gr.update(selected_keys=[selected_menu]), gr.update(
active_key=selected_menu)

with gr.Blocks(css="""
.gradio-container {
max-width: 100% !important;
padding: 0 !important;
}
""") as demo:
with ms.Application():
with antd.ConfigProvider():
with antd.Layout(elem_style=dict(
height=
"calc(100vh - var(--size-4) - var(--body-text-size) * 1.5)"
)):
with antd.Layout.Sider(
elem_style=dict(height="100vh",
overflow="auto",
position="relative")):
if self.logo:
with antd.Flex(
justify="center",
elem_style=dict(
position="sticky",
top=0,
padding="8px 4px",
marginBottom=8,
borderBottom=
"1px solid rgba(255,255,255,0.2)")
):
self.logo.render()

layout_menu = antd.Menu(default_selected_keys=[
self.default_active_key
],
mode="inline",
theme="dark",
items=self.menus)
with antd.Layout():
with antd.Layout.Content(elem_style=dict(
padding=12, overflow="auto")):
with antd.Tabs(default_active_key=self.
default_active_key,
render_tab_bar="() => null"
) as layout_tabs:
self._render_docs(self.menus)
layout_menu.select(fn=on_menu_select,
outputs=[layout_menu, layout_tabs])
return demo

0 comments on commit f28deb1

Please sign in to comment.