-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
67 lines (49 loc) · 1.75 KB
/
preprocessing.py
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
import logging
from io import StringIO
import nbformat
import tiktoken
from nbformat.reader import NotJSONError
def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""
Calculates the number of tokens in a string using the specified encoding.
Args:
string (str): The input string.
encoding_name (str): The name of the encoding to use.
Returns:
int: The number of tokens in the string.
"""
encoding = tiktoken.get_encoding(encoding_name)
return len(encoding.encode(string))
def is_valid_notebook(content):
"""
Check if the given content is a valid Jupyter notebook.
Args:
content (str): The content of the notebook.
Returns:
bool: True if the content is a valid notebook, False otherwise.
"""
try:
nbformat.reads(content, as_version=4)
return True
except NotJSONError:
return False
def remove_outputs_from_notebook(notebook_content):
"""
Removes the outputs and execution counts from a Jupyter Notebook.
Args:
notebook_content (str): The content of the Jupyter Notebook.
Returns:
str: The modified content of the Jupyter Notebook with outputs and execution counts removed.
"""
if not is_valid_notebook(notebook_content):
raise ValueError("The content is not a valid Jupyter Notebook")
notebook = nbformat.reads(notebook_content, as_version=4)
for cell in notebook.cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'execution_count' in cell:
cell['execution_count'] = None
logging.info("Removed outputs from Jupyter Notebook")
output_stream = StringIO()
nbformat.write(notebook, output_stream)
return output_stream.getvalue()