Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

verify_notebook_version.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import re
  2. import sys
  3. import super_gradients
  4. import nbformat
  5. def get_first_cell_content(notebook_path):
  6. # Load the notebook
  7. with open(notebook_path, "r", encoding="utf-8") as notebook_file:
  8. notebook_content = nbformat.read(notebook_file, as_version=4)
  9. # Get the content of the first cell (assuming it's a code cell)
  10. code_cells = [cell for cell in notebook_content.cells if cell.cell_type == "code"]
  11. first_cell_content = ""
  12. if code_cells:
  13. first_cell = code_cells[0]
  14. first_cell_content = first_cell.source
  15. return first_cell_content
  16. def main():
  17. """
  18. This script is used to verify that the version of the SG package matches the version of SG installed in the notebook.
  19. The script assumes that the first cell of the notebook contains a `!pip install super_gradients=={version}` command.
  20. :return: An exit code of 0 if the versions match, 1 otherwise.
  21. """
  22. notebook_path = sys.argv[1]
  23. first_cell_content = get_first_cell_content(notebook_path)
  24. print(first_cell_content)
  25. # Check if the first cell contains "!pip install super_gradients=={version}" using regex and extract the version
  26. pattern = re.compile(r"^!pip install super_gradients==([\d\.]+)")
  27. for line in first_cell_content.splitlines():
  28. match = re.search(pattern, line)
  29. if match:
  30. sg_version_in_notebook = match.group(1)
  31. if sg_version_in_notebook == super_gradients.__version__:
  32. return 0
  33. else:
  34. print(
  35. f"Version mismatch detected:\n"
  36. f"super_gradients.__version__ is {super_gradients.__version__}\n"
  37. f"Notebook uses super_gradients {sg_version_in_notebook} (notebook_path)"
  38. )
  39. return 1
  40. print("First code cell of the notebook does not contain a `!pip install super_gradients=={version} command`")
  41. return 1
  42. if __name__ == "__main__":
  43. sys.exit(main())
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...