codycjy commited on
Commit
e200709
1 Parent(s): a9a4892

Parse and generate ipynb (Issue #501)

Browse files

Implemented code to parse and generate the ipynb files. The solution addresses Issue #501.

crazy_functional.py CHANGED
@@ -19,12 +19,18 @@ def get_crazy_functions():
19
  from crazy_functions.解析项目源代码 import 解析一个Lua项目
20
  from crazy_functions.解析项目源代码 import 解析一个CSharp项目
21
  from crazy_functions.总结word文档 import 总结word文档
 
22
  function_plugins = {
23
 
24
  "解析整个Python项目": {
25
  "Color": "stop", # 按钮颜色
26
  "Function": HotReload(解析一个Python项目)
27
  },
 
 
 
 
 
28
  "批量总结Word文档": {
29
  "Color": "stop",
30
  "Function": HotReload(总结word文档)
 
19
  from crazy_functions.解析项目源代码 import 解析一个Lua项目
20
  from crazy_functions.解析项目源代码 import 解析一个CSharp项目
21
  from crazy_functions.总结word文档 import 总结word文档
22
+ from crazy_functions.解析JupyterNotebook import 解析ipynb文件
23
  function_plugins = {
24
 
25
  "解析整个Python项目": {
26
  "Color": "stop", # 按钮颜色
27
  "Function": HotReload(解析一个Python项目)
28
  },
29
+ "解析Jupyter Notebook文件": {
30
+ "Color": "stop",
31
+ "AsButton":False,
32
+ "Function": HotReload(解析ipynb文件),
33
+ },
34
  "批量总结Word文档": {
35
  "Color": "stop",
36
  "Function": HotReload(总结word文档)
crazy_functions/解析JupyterNotebook.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from toolbox import update_ui
2
+ from toolbox import CatchException, report_execption
3
+ fast_debug = True
4
+
5
+
6
+ class PaperFileGroup():
7
+ def __init__(self):
8
+ self.file_paths = []
9
+ self.file_contents = []
10
+ self.sp_file_contents = []
11
+ self.sp_file_index = []
12
+ self.sp_file_tag = []
13
+
14
+ # count_token
15
+ from request_llm.bridge_all import model_info
16
+ enc = model_info["gpt-3.5-turbo"]['tokenizer']
17
+ def get_token_num(txt): return len(
18
+ enc.encode(txt, disallowed_special=()))
19
+ self.get_token_num = get_token_num
20
+
21
+ def run_file_split(self, max_token_limit=1900):
22
+ """
23
+ 将长文本分离开来
24
+ """
25
+ for index, file_content in enumerate(self.file_contents):
26
+ if self.get_token_num(file_content) < max_token_limit:
27
+ self.sp_file_contents.append(file_content)
28
+ self.sp_file_index.append(index)
29
+ self.sp_file_tag.append(self.file_paths[index])
30
+ else:
31
+ from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
32
+ segments = breakdown_txt_to_satisfy_token_limit_for_pdf(
33
+ file_content, self.get_token_num, max_token_limit)
34
+ for j, segment in enumerate(segments):
35
+ self.sp_file_contents.append(segment)
36
+ self.sp_file_index.append(index)
37
+ self.sp_file_tag.append(
38
+ self.file_paths[index] + f".part-{j}.txt")
39
+
40
+
41
+
42
+ def parseNotebook(filename, enable_markdown=1):
43
+ import json
44
+
45
+ CodeBlocks = []
46
+ with open(filename, 'r', encoding='utf-8', errors='replace') as f:
47
+ notebook = json.load(f)
48
+ for cell in notebook['cells']:
49
+ if cell['cell_type'] == 'code' and cell['source']:
50
+ # remove blank lines
51
+ cell['source'] = [line for line in cell['source'] if line.strip()
52
+ != '']
53
+ CodeBlocks.append("".join(cell['source']))
54
+ elif enable_markdown and cell['cell_type'] == 'markdown' and cell['source']:
55
+ cell['source'] = [line for line in cell['source'] if line.strip()
56
+ != '']
57
+ CodeBlocks.append("Markdown:"+"".join(cell['source']))
58
+
59
+ Code = ""
60
+ for idx, code in enumerate(CodeBlocks):
61
+ Code += f"This is {idx+1}th code block: \n"
62
+ Code += code+"\n"
63
+
64
+ return Code
65
+
66
+
67
+ def ipynb解释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
68
+ from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
69
+
70
+ pfg = PaperFileGroup()
71
+
72
+ print(file_manifest)
73
+ for fp in file_manifest:
74
+ file_content = parseNotebook(fp, enable_markdown=1)
75
+ pfg.file_paths.append(fp)
76
+ pfg.file_contents.append(file_content)
77
+
78
+ # <-------- 拆分过长的IPynb文件 ---------->
79
+ pfg.run_file_split(max_token_limit=1024)
80
+ n_split = len(pfg.sp_file_contents)
81
+
82
+ inputs_array = [f"This is a Jupyter Notebook file, tell me about Each Block in Chinese. Focus Just On Code." +
83
+ f"If a block starts with `Markdown` which means it's a markdown block in ipynbipynb. " +
84
+ f"Start a new line for a block and block num use Chinese." +
85
+ f"\n\n{frag}" for frag in pfg.sp_file_contents]
86
+ inputs_show_user_array = [f"{f}的分析如下" for f in pfg.sp_file_tag]
87
+ sys_prompt_array = ["You are a professional programmer."] * n_split
88
+
89
+ gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
90
+ inputs_array=inputs_array,
91
+ inputs_show_user_array=inputs_show_user_array,
92
+ llm_kwargs=llm_kwargs,
93
+ chatbot=chatbot,
94
+ history_array=[[""] for _ in range(n_split)],
95
+ sys_prompt_array=sys_prompt_array,
96
+ # max_workers=5, # OpenAI所允许的最大并行过载
97
+ scroller_max_len=80
98
+ )
99
+
100
+ # <-------- 整理结果,退出 ---------->
101
+ block_result = " \n".join(gpt_response_collection)
102
+ chatbot.append(("解析的结果如下", block_result))
103
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
104
+
105
+
106
+ @CatchException
107
+ def 解析ipynb文件(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
108
+ chatbot.append([
109
+ "函数插件功能?",
110
+ "对IPynb文件进行解析"])
111
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
112
+
113
+ history = [] # 清空历史
114
+ import glob
115
+ import os
116
+ if os.path.exists(txt):
117
+ project_folder = txt
118
+ else:
119
+ if txt == "":
120
+ txt = '空空如也的输入栏'
121
+ report_execption(chatbot, history,
122
+ a=f"解析项目: {txt}", b=f"找不到本地项目或无权访问: {txt}")
123
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
124
+ return
125
+ if txt.endswith('.ipynb'):
126
+ file_manifest = [txt]
127
+ else:
128
+ file_manifest = [f for f in glob.glob(
129
+ f'{project_folder}/**/*.ipynb', recursive=True)]
130
+ if len(file_manifest) == 0:
131
+ report_execption(chatbot, history,
132
+ a=f"解析项目: {txt}", b=f"找不到任何.ipynb文件: {txt}")
133
+ yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
134
+ return
135
+ yield from ipynb解释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, )