Spaces:
Running
Running
用gpt给自己生成注释
Browse files- toolbox.py +24 -0
toolbox.py
CHANGED
@@ -3,6 +3,9 @@ from show_math import convert as convert_math
|
|
3 |
from functools import wraps
|
4 |
|
5 |
def write_results_to_file(history, file_name=None):
|
|
|
|
|
|
|
6 |
import os, time
|
7 |
if file_name is None:
|
8 |
file_name = time.strftime("chatGPT分析报告%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md'
|
@@ -18,12 +21,18 @@ def write_results_to_file(history, file_name=None):
|
|
18 |
return res
|
19 |
|
20 |
def regular_txt_to_markdown(text):
|
|
|
|
|
|
|
21 |
text = text.replace('\n', '\n\n')
|
22 |
text = text.replace('\n\n\n', '\n\n')
|
23 |
text = text.replace('\n\n\n', '\n\n')
|
24 |
return text
|
25 |
|
26 |
def CatchException(f):
|
|
|
|
|
|
|
27 |
@wraps(f)
|
28 |
def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
29 |
try:
|
@@ -39,10 +48,16 @@ def CatchException(f):
|
|
39 |
return decorated
|
40 |
|
41 |
def report_execption(chatbot, history, a, b):
|
|
|
|
|
|
|
42 |
chatbot.append((a, b))
|
43 |
history.append(a); history.append(b)
|
44 |
|
45 |
def text_divide_paragraph(text):
|
|
|
|
|
|
|
46 |
if '```' in text:
|
47 |
# careful input
|
48 |
return text
|
@@ -55,6 +70,9 @@ def text_divide_paragraph(text):
|
|
55 |
return text
|
56 |
|
57 |
def markdown_convertion(txt):
|
|
|
|
|
|
|
58 |
if ('$' in txt) and ('```' not in txt):
|
59 |
return markdown.markdown(txt,extensions=['fenced_code','tables']) + '<br><br>' + \
|
60 |
markdown.markdown(convert_math(txt, splitParagraphs=False),extensions=['fenced_code','tables'])
|
@@ -63,6 +81,9 @@ def markdown_convertion(txt):
|
|
63 |
|
64 |
|
65 |
def format_io(self, y):
|
|
|
|
|
|
|
66 |
if y is None: return []
|
67 |
i_ask, gpt_reply = y[-1]
|
68 |
i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波
|
@@ -74,6 +95,9 @@ def format_io(self, y):
|
|
74 |
|
75 |
|
76 |
def find_free_port():
|
|
|
|
|
|
|
77 |
import socket
|
78 |
from contextlib import closing
|
79 |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
|
|
3 |
from functools import wraps
|
4 |
|
5 |
def write_results_to_file(history, file_name=None):
|
6 |
+
"""
|
7 |
+
将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
|
8 |
+
"""
|
9 |
import os, time
|
10 |
if file_name is None:
|
11 |
file_name = time.strftime("chatGPT分析报告%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md'
|
|
|
21 |
return res
|
22 |
|
23 |
def regular_txt_to_markdown(text):
|
24 |
+
"""
|
25 |
+
将普通文本转换为Markdown格式的文本。
|
26 |
+
"""
|
27 |
text = text.replace('\n', '\n\n')
|
28 |
text = text.replace('\n\n\n', '\n\n')
|
29 |
text = text.replace('\n\n\n', '\n\n')
|
30 |
return text
|
31 |
|
32 |
def CatchException(f):
|
33 |
+
"""
|
34 |
+
装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
|
35 |
+
"""
|
36 |
@wraps(f)
|
37 |
def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
38 |
try:
|
|
|
48 |
return decorated
|
49 |
|
50 |
def report_execption(chatbot, history, a, b):
|
51 |
+
"""
|
52 |
+
向chatbot中添加错误信息
|
53 |
+
"""
|
54 |
chatbot.append((a, b))
|
55 |
history.append(a); history.append(b)
|
56 |
|
57 |
def text_divide_paragraph(text):
|
58 |
+
"""
|
59 |
+
将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
|
60 |
+
"""
|
61 |
if '```' in text:
|
62 |
# careful input
|
63 |
return text
|
|
|
70 |
return text
|
71 |
|
72 |
def markdown_convertion(txt):
|
73 |
+
"""
|
74 |
+
将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。
|
75 |
+
"""
|
76 |
if ('$' in txt) and ('```' not in txt):
|
77 |
return markdown.markdown(txt,extensions=['fenced_code','tables']) + '<br><br>' + \
|
78 |
markdown.markdown(convert_math(txt, splitParagraphs=False),extensions=['fenced_code','tables'])
|
|
|
81 |
|
82 |
|
83 |
def format_io(self, y):
|
84 |
+
"""
|
85 |
+
将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。
|
86 |
+
"""
|
87 |
if y is None: return []
|
88 |
i_ask, gpt_reply = y[-1]
|
89 |
i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波
|
|
|
95 |
|
96 |
|
97 |
def find_free_port():
|
98 |
+
"""
|
99 |
+
返回当前系统中可用的未使用端口。
|
100 |
+
"""
|
101 |
import socket
|
102 |
from contextlib import closing
|
103 |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|