コード例 #1
0
    def add_guide(self, name, working_dir=None):
        """e.g. add_guide('functional_api')"""
        if name.endswith(".py"):
            name = name[:-3]
        ipynb_dir = Path(self.guides_dir) / "ipynb"
        if not os.path.exists(ipynb_dir):
            os.makedirs(ipynb_dir)

        md_dir = Path(self.guides_dir) / "md"
        if not os.path.exists(md_dir):
            os.makedirs(md_dir)

        img_dir = Path(self.guides_dir) / "img"
        if not os.path.exists(img_dir):
            os.makedirs(img_dir)

        py_path = Path(self.guides_dir) / (name + ".py")
        md_path = md_dir / (name + ".md")
        nb_path = ipynb_dir / (name + ".ipynb")

        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
        tutobooks.py_to_md(py_path,
                           nb_path,
                           md_path,
                           img_dir,
                           working_dir=working_dir)
        md_content = open(md_path).read()
        github_repo_dir = str(GUIDES_GH_LOCATION)
        site_img_dir = "img/guides/" + name
        md_content = self.preprocess_tutobook_md_source(
            md_content, name + ".py", github_repo_dir, img_dir, site_img_dir)
        open(md_path, "w").write(md_content)
コード例 #2
0
    def add_example(self, path, working_dir=None):
        """e.g. add_example('vision/cats_and_dogs')"""
        assert path.count(os.path.sep) == 1
        folder, name = path.split(os.path.sep)
        if name.endswith(".py"):
            name = name[:-3]

        ipynb_dir = Path(self.examples_dir) / folder / "ipynb"
        if not os.path.exists(ipynb_dir):
            os.makedirs(ipynb_dir)

        md_dir = Path(self.examples_dir) / folder / "md"
        if not os.path.exists(md_dir):
            os.makedirs(md_dir)

        img_dir = Path(self.examples_dir) / folder / "img"
        if not os.path.exists(img_dir):
            os.makedirs(img_dir)

        py_path = Path(self.examples_dir) / folder / (name + ".py")
        md_path = md_dir / (name + ".md")
        nb_path = ipynb_dir / (name + ".ipynb")
        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
        tutobooks.py_to_md(py_path,
                           nb_path,
                           md_path,
                           img_dir,
                           working_dir=working_dir)
        md_content = open(md_path).read()
        github_repo_dir = str(EXAMPLES_GH_LOCATION / folder)
        site_img_dir = os.path.join("img", "examples", folder, name)
        md_content = self.preprocess_tutobook_md_source(
            md_content, name + ".py", github_repo_dir, img_dir, site_img_dir)
        open(md_path, "w").write(md_content)
コード例 #3
0
ファイル: autogen.py プロジェクト: Maximilianpy/ai2business
def py_to_nb_md(dest_dir, dir_path="tutorials"):
    for file_path in os.listdir(f"{dir_path}/"):

        file_name = file_path
        py_path = os.path.join(dir_path, file_path)
        file_name_no_ext = os.path.splitext(file_name)[0]
        ext = os.path.splitext(file_name)[1]

        if ext != ".py":
            continue

        nb_path = os.path.join("ipynb", file_name_no_ext + ".ipynb")
        md_path = os.path.join(dest_dir, "tutorials", file_name_no_ext + ".md")

        Path(nb_path).parent.mkdir(exist_ok=True)
        Path(md_path).parent.mkdir(exist_ok=True)

        tutobooks.py_to_nb(py_path, nb_path, fill_outputs=True)
        tutobooks.py_to_md(py_path, nb_path, md_path, "templates/img")
        github_repo_dir = "ai2business/ai2business/blob/main/docs/"
        with open(md_path, "r") as md_file:
            button_lines = [
                ":material-link: "
                "[**View in Colab**](https://colab.research.google.com/github/"
                + github_repo_dir + "ipynb/" + file_name_no_ext + ".ipynb" +
                ")   &nbsp; &nbsp;" + '<span class="k-dot">•</span>' +
                ":material-github: "
                "[**GitHub source**](https://github.com/" + github_repo_dir +
                "tutorials/" + file_name_no_ext + ".py)",
                "\n",
            ]
            md_content = "".join(button_lines) + "\n" + md_file.read()

        with open(md_path, "w+") as md_file:
            md_file.write(md_content)
コード例 #4
0
def generate_single_tf_guide(source_dir, target_dir, title, source_name,
                             target_name):
    # Before we start, regenerate the ipynb.
    max_loc = tutobooks.MAX_LOC
    tutobooks.MAX_LOC = 400
    py_path = (Path(source_dir).parent / source_name).with_suffix(".py")
    nb_path = (Path(source_dir) / source_name).with_suffix(".ipynb")
    tutobooks.py_to_nb(py_path, nb_path, fill_outputs=False)
    tutobooks.MAX_LOC = max_loc

    original_ipynb = json.loads(nb_path.read_text())

    # Skip first title cell
    cells = original_ipynb["cells"][1:]
    # Strip Keras tags
    for cell in cells:
        if cell["cell_type"] == "markdown":
            new_lines = []
            lines = cell["source"]
            num_lines = len(lines)
            for i in range(num_lines - 1):
                if lines[i].startswith('<div class="k-default-codeblock">'
                                       ) and lines[i + 1].startswith("```"):
                    continue
                elif lines[i].startswith("</div>") and lines[i - 1].startswith(
                        "```"):
                    continue
                else:
                    new_lines.append(lines[i])
            if len(lines) >= 2 and not (lines[-1].startswith("</div>")
                                        and lines[-2].startswith("```")):
                new_lines.append(lines[-1])
            if len(lines) < 2:
                new_lines.append(lines[-1])
            cell["source"] = new_lines
        elif cell["cell_type"] == "code":
            lines = cell["source"]
            if not lines[0].strip():
                lines = lines[1:]
            if not lines[-1].strip():
                lines = lines[:-1]
            cell["source"] = lines

    # Add header cells
    header_cells = copy.deepcopy(TF_IPYNB_CELLS_TEMPLATE)
    # Add title cell
    header_cells.append({
        "cell_type": "markdown",
        "metadata": {
            "colab_type": "text"
        },
        "source": ["# " + title],
    })
    buttons = copy.deepcopy(TF_BUTTONS_TEMPLATE)
    for i in range(len(buttons["source"])):
        buttons["source"][i] = buttons["source"][i].replace(
            "TARGET_NAME", target_name)
        buttons["source"][i] = buttons["source"][i].replace(
            "SOURCE_NAME", source_name)
    header_cells.append(buttons)
    cells = header_cells + cells

    cell_count = 0
    for cell in cells:
        cell_count += 1
        str_to_hash = f"{cell_count} {cell['source']}"
        cell_id = hashlib.sha256(str_to_hash.encode('utf-8')).hexdigest()
        cell["metadata"]["id"] = cell_id[:12]

    notebook = {}
    for key in TF_IPYNB_BASE.keys():
        notebook[key] = TF_IPYNB_BASE[key]
    notebook["metadata"]["colab"]["name"] = target_name + ".ipynb"
    notebook["cells"] = cells

    f = open(Path(target_dir) / (target_name + ".ipynb"), "w")
    json_st = json.dumps(notebook, indent=1, sort_keys=True)

    # Apply link conversion
    json_st = json_st.replace(
        "(/api/callbacks/",
        "(https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/",
    )
    json_st = json_st.replace(
        "keras.io/api/layers/recurrent_layers/rnn/",
        "https://www.tensorflow.org/api_docs/python/tf/keras/layers/RNN/",
    )
    json_st = json_st.replace(
        "https://keras.io/api/layers/recurrent_layers/gru/",
        "https://www.tensorflow.org/api_docs/python/tf/keras/layers/GRU/",
    )
    json_st = json_st.replace(
        "https://keras.io/api/layers/recurrent_layers/lstm/",
        "https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM/",
    )
    json_st = json_st.replace(
        "https://keras.io/api/layers/recurrent_layers/bidirectional/",
        "https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional/",
    )
    json_st = json_st.replace(
        "https://keras.io/api/callbacks/",
        "https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/",
    )
    for entry in CONFIG:
        src = entry["source_name"]
        dst = entry["target_name"]
        json_st = re.sub(
            r"(?is)]\((\s*)/guides/" + src,
            "](https://www.tensorflow.org/guide/keras/" + dst,
            json_st,
        )
        json_st = re.sub(
            r"(?is)(\s+)/guides/" + src,
            "https://www.tensorflow.org/guide/keras/" + dst,
            json_st,
        )
    f.write(json_st)
    f.close()