Spaces:
Running
Running
File size: 5,968 Bytes
e841ba5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import os
import re
import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms
import modelscope_studio.components.legacy as mgr
from .env import is_modelscope_studio
from .parse_markdown import parse_markdown
class Docs:
def __init__(self, file_path: str):
self.file_path = 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(
lambda x: x.endswith("-zh_CN.md") or
(f"{'.'.join(x.split('.')[:-1])}-zh_CN.md" not in self.
markdown_files), self.markdown_files))
else:
self.markdown_files = list(
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:
return f.read()
def _render_demo(self,
demo_name,
prefix='',
suffix='',
fixed=False,
title=''):
content = self._read_file(f"./demos/{demo_name}.py")
module = self.demo_modules[demo_name]
with antd.Card(styles=dict(body=dict(padding=10)),
elem_style=dict(margin="8px 0")):
if title:
with ms.Slot("title"):
ms.Text(title)
with antd.Row(align="stretch", wrap=True, gutter=8):
with antd.Col(sm=dict(span=10, order=1),
xs=dict(span=24, order=2)):
with antd.Row(elem_style=dict(height='100%'),
gutter=[8, 8]):
with antd.Col(sm=0, xs=24):
antd.Divider(type="horizontal",
variant="dashed",
elem_style=dict(width='100%',
margin='8px 0 0'))
with antd.Col(sm=23, xs=24):
prefix = prefix + "\n" if prefix else ""
suffix = "\n" + suffix if suffix else ""
gr.Markdown(f"""{prefix}```python
{content}
```{suffix}""",
header_links=True)
with antd.Col(sm=1,
xs=0,
elem_style=dict(height="100%")):
with ms.Div(
elem_style=dict(display="flex",
justifyContent="center",
width="100%",
height="100%")):
antd.Divider(type="vertical",
variant="dashed",
elem_style=dict(height='100%',
margin=0))
with antd.Col(
sm=dict(span=14, order=2),
xs=dict(span=24, order=1),
elem_style=dict(
width='100%',
transform="translate(0, 0)" if fixed else None)):
module.demo.render()
def _render_markdown(self, markdown_file):
items = parse_markdown(self._remove_formatter(
self._read_file(markdown_file)),
read_file=self._read_file)
for item in items:
if item["type"] == "text":
mgr.Markdown(item["value"], header_links=True, preview=False)
elif item["type"] == "demo":
self._render_demo(item["name"],
prefix=item["prefix"],
suffix=item["suffix"],
fixed=item["fixed"],
title=item["title"])
def render(self):
with gr.Blocks() as demo:
self._render_markdown(self.markdown_files[0])
return demo
|