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 2.6 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  1. import re
  2. import sys
  3. from typing import Optional
  4. import super_gradients
  5. def get_first_cell_content(notebook_path):
  6. import nbformat
  7. # Load the notebook
  8. with open(notebook_path, "r", encoding="utf-8") as notebook_file:
  9. notebook_content = nbformat.read(notebook_file, as_version=4)
  10. # Get the content of the first cell (assuming it's a code cell)
  11. code_cells = [cell for cell in notebook_content.cells if cell.cell_type == "code"]
  12. first_cell_content = ""
  13. if code_cells:
  14. first_cell = code_cells[0]
  15. first_cell_content = first_cell.source
  16. return first_cell_content
  17. def try_extract_super_gradients_version_from_pip_install_command(input: str) -> Optional[str]:
  18. """
  19. Extracts the version of super_gradients from a string like `!pip install super_gradients=={version}` command.
  20. A pip install may contain extra arguments, e.g. `!pip install -q super_gradients=={version} torch=={another version}`.
  21. :param input: A string that contains a `!pip install super_gradients=={version}` command.
  22. :return: The version of super_gradients.
  23. """
  24. pattern = re.compile(r"pip\s+install.*?super[-_]gradients==([0-9]+(?:\.[0-9]+)*(?:\.[0-9]+)?)")
  25. match = re.search(pattern, input)
  26. if match:
  27. return match.group(1)
  28. else:
  29. return None
  30. def main():
  31. """
  32. This script is used to verify that the version of the SG package matches the version of SG installed in the notebook.
  33. The script assumes that the first cell of the notebook contains a `!pip install super_gradients=={version}` command.
  34. :return: An exit code of 0 if the versions match, 1 otherwise.
  35. """
  36. notebook_path = sys.argv[1]
  37. first_cell_content = get_first_cell_content(notebook_path)
  38. expected_version = super_gradients.__version__
  39. for line in first_cell_content.splitlines():
  40. sg_version_in_notebook = try_extract_super_gradients_version_from_pip_install_command(line)
  41. if sg_version_in_notebook is not None:
  42. if sg_version_in_notebook == expected_version:
  43. return 0
  44. else:
  45. print(
  46. f"Version mismatch detected in {notebook_path}:\n"
  47. f"super_gradients.__version__ is {expected_version}\n"
  48. f"Notebook uses super_gradients {sg_version_in_notebook} (notebook_path)"
  49. )
  50. return 1
  51. print(f"First code cell of the notebook {notebook_path} does not contain a `!pip install super_gradients=={expected_version} command`")
  52. print("First code cell content:")
  53. print(first_cell_content)
  54. return 1
  55. if __name__ == "__main__":
  56. sys.exit(main())
Tip!

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

Comments

Loading...