Skip to content

Commit

Permalink
Update docs/scripts/auto-generate-examples.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarobartt committed Oct 25, 2024
1 parent e225abd commit b1390cf
Showing 1 changed file with 6 additions and 39 deletions.
45 changes: 6 additions & 39 deletions docs/scripts/auto-generate-examples.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import os
import re
# import subprocess


def current_git_branch():
return "gemma2-cloud-run"
# try:
# # First, try to get the branch name from GitHub Actions environment variables
# if "GITHUB_REF" in os.environ:
# branch = os.environ["GITHUB_REF"].split("/")[-1]
# else:
# # If not in GitHub Actions, use git command
# result = subprocess.run(
# ["git", "rev-parse", "--abbrev-ref", "HEAD"],
# capture_output=True,
# text=True,
# check=True,
# )
# branch = result.stdout.strip()
#
# # Check if the branch is 'HEAD' (detached state in CI)
# if branch == "HEAD":
# # Try to get the branch name from GitHub Actions specific environment variable
# if "GITHUB_HEAD_REF" in os.environ:
# branch = os.environ["GITHUB_HEAD_REF"]
# elif "GITHUB_REF" in os.environ:
# branch = os.environ["GITHUB_REF"].split("/")[-1]
# else:
# branch = "main"
#
# return branch
# except: # noqa: E722
# return "main"


def process_readme_files():
Expand Down Expand Up @@ -61,30 +29,27 @@ def process_file(root, file, dir):
with open(file_path, "r") as f:
content = f.read()

# Obtain the current checked out Git branch
git_branch = current_git_branch()

# For Juypter Notebooks, remove the comment i.e. `<!--` and the `--!>` but keep the metadata
content = re.sub(r"<!-- (.*?) -->", r"\1", content, flags=re.DOTALL)

# Replace image and link paths
content = re.sub(
r"\(\./(imgs|assets)/([^)]*\.png)\)",
rf"(https://raw.githubusercontent.com/huggingface/Google-Cloud-Containers/{git_branch}/"
r"(https://raw.githubusercontent.com/huggingface/Google-Cloud-Containers/main/"
+ root
+ r"/\1/\2)",
content,
)
content = re.sub(
r"\(\.\./([^)]+)\)",
rf"(https://github.com/huggingface/Google-Cloud-Containers/tree/{git_branch}/examples/"
r"(https://github.com/huggingface/Google-Cloud-Containers/tree/main/examples/"
+ dir
+ r"/\1)",
content,
)
content = re.sub(
r"\(\.\/([^)]+)\)",
rf"(https://github.com/huggingface/Google-Cloud-Containers/tree/{git_branch}/"
r"(https://github.com/huggingface/Google-Cloud-Containers/tree/main/"
+ root
+ r"/\1)",
content,
Expand Down Expand Up @@ -129,7 +94,9 @@ def replacement(match):
print("No relative paths found in the processed file.")

# Calculate the example URL
example_url = f"https://github.com/huggingface/Google-Cloud-Containers/tree/{git_branch}/{root}"
example_url = (
f"https://github.com/huggingface/Google-Cloud-Containers/tree/main/{root}"
)
if file.__contains__("ipynb"):
example_url += "/vertex-notebook.ipynb"

Expand Down

0 comments on commit b1390cf

Please sign in to comment.