Spaces:
Running
Running
file IO
Browse files- .gitignore +1 -0
- crazy_functions/解析项目源代码.py +20 -0
- functional_crazy.py +35 -9
- main.py +12 -8
- requirements.txt +1 -1
- toolbox.py +41 -1
.gitignore
CHANGED
@@ -136,3 +136,4 @@ ssr_conf
|
|
136 |
config_private.py
|
137 |
gpt_log
|
138 |
private.md
|
|
|
|
136 |
config_private.py
|
137 |
gpt_log
|
138 |
private.md
|
139 |
+
private_upload
|
crazy_functions/解析项目源代码.py
CHANGED
@@ -127,3 +127,23 @@ def 解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, s
|
|
127 |
return
|
128 |
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
return
|
128 |
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
129 |
|
130 |
+
@CatchException
|
131 |
+
def 解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
132 |
+
history = [] # 清空历史,以免输入溢出
|
133 |
+
import glob, os
|
134 |
+
if os.path.exists(txt):
|
135 |
+
project_folder = txt
|
136 |
+
else:
|
137 |
+
if txt == "": txt = '空空如也的输入栏'
|
138 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
139 |
+
yield chatbot, history, '正常'
|
140 |
+
return
|
141 |
+
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.h', recursive=True)] + \
|
142 |
+
[f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)] + \
|
143 |
+
[f for f in glob.glob(f'{project_folder}/**/*.c', recursive=True)]
|
144 |
+
if len(file_manifest) == 0:
|
145 |
+
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.h头文件: {txt}")
|
146 |
+
yield chatbot, history, '正常'
|
147 |
+
return
|
148 |
+
yield from 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)
|
149 |
+
|
functional_crazy.py
CHANGED
@@ -5,25 +5,30 @@ def get_crazy_functionals():
|
|
5 |
from crazy_functions.解析项目源代码 import 解析项目本身
|
6 |
from crazy_functions.解析项目源代码 import 解析一个Python项目
|
7 |
from crazy_functions.解析项目源代码 import 解析一个C项目的头文件
|
|
|
8 |
from crazy_functions.高级功能函数模板 import 高阶功能模板函数
|
9 |
|
10 |
return {
|
11 |
"[实验] 请解析并解构此项目本身": {
|
12 |
"Function": 解析项目本身
|
13 |
},
|
14 |
-
"[实验] 解析整个py
|
15 |
"Color": "stop", # 按钮颜色
|
16 |
"Function": 解析一个Python项目
|
17 |
},
|
18 |
-
"[实验] 解析整个C
|
19 |
"Color": "stop", # 按钮颜色
|
20 |
"Function": 解析一个C项目的头文件
|
21 |
},
|
22 |
-
"[实验]
|
|
|
|
|
|
|
|
|
23 |
"Color": "stop", # 按钮颜色
|
24 |
"Function": 读文章写摘要
|
25 |
},
|
26 |
-
"[实验]
|
27 |
"Color": "stop", # 按钮颜色
|
28 |
"Function": 批量生成函数注释
|
29 |
},
|
@@ -33,8 +38,29 @@ def get_crazy_functionals():
|
|
33 |
},
|
34 |
}
|
35 |
|
36 |
-
def on_file_uploaded(
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from crazy_functions.解析项目源代码 import 解析项目本身
|
6 |
from crazy_functions.解析项目源代码 import 解析一个Python项目
|
7 |
from crazy_functions.解析项目源代码 import 解析一个C项目的头文件
|
8 |
+
from crazy_functions.解析项目源代码 import 解析一个C项目
|
9 |
from crazy_functions.高级功能函数模板 import 高阶功能模板函数
|
10 |
|
11 |
return {
|
12 |
"[实验] 请解析并解构此项目本身": {
|
13 |
"Function": 解析项目本身
|
14 |
},
|
15 |
+
"[实验] 解析整个py项目(配合input输入框)": {
|
16 |
"Color": "stop", # 按钮颜色
|
17 |
"Function": 解析一个Python项目
|
18 |
},
|
19 |
+
"[实验] 解析整个C++项目头文件(配合input输入框)": {
|
20 |
"Color": "stop", # 按钮颜色
|
21 |
"Function": 解析一个C项目的头文件
|
22 |
},
|
23 |
+
"[实验] 解析整个C++项目(配合input输入框)": {
|
24 |
+
"Color": "stop", # 按钮颜色
|
25 |
+
"Function": 解析一个C项目
|
26 |
+
},
|
27 |
+
"[实验] 读tex论文写摘要(配合input输入框)": {
|
28 |
"Color": "stop", # 按钮颜色
|
29 |
"Function": 读文章写摘要
|
30 |
},
|
31 |
+
"[实验] 批量生成函数注释(配合input输入框)": {
|
32 |
"Color": "stop", # 按钮颜色
|
33 |
"Function": 批量生成函数注释
|
34 |
},
|
|
|
38 |
},
|
39 |
}
|
40 |
|
41 |
+
def on_file_uploaded(files, chatbot, txt):
|
42 |
+
import shutil, os, time, glob
|
43 |
+
from toolbox import extract_archive
|
44 |
+
try: shutil.rmtree('./private_upload/')
|
45 |
+
except: pass
|
46 |
+
time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
|
47 |
+
os.makedirs(f'private_upload/{time_tag}', exist_ok=True)
|
48 |
+
for file in files:
|
49 |
+
file_origin_name = os.path.basename(file.orig_name)
|
50 |
+
shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}')
|
51 |
+
extract_archive(f'private_upload/{time_tag}/{file_origin_name}',
|
52 |
+
dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract')
|
53 |
+
moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)]
|
54 |
+
txt = f'private_upload/{time_tag}'
|
55 |
+
moved_files_str = '\t\n\n'.join(moved_files)
|
56 |
+
chatbot.append(['我上传了文件,请查收',
|
57 |
+
f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能'])
|
58 |
+
return chatbot, txt
|
59 |
+
|
60 |
+
def on_report_generated(files, chatbot):
|
61 |
+
from toolbox import find_recent_files
|
62 |
+
report_files = find_recent_files('gpt_log')
|
63 |
+
# files.extend(report_files)
|
64 |
+
chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。'])
|
65 |
+
return report_files, chatbot
|
66 |
+
|
main.py
CHANGED
@@ -25,7 +25,7 @@ from functional import get_functionals
|
|
25 |
functional = get_functionals()
|
26 |
|
27 |
# 对一些丧心病狂的实验性功能模块进行测试
|
28 |
-
from functional_crazy import get_crazy_functionals, on_file_uploaded
|
29 |
crazy_functional = get_crazy_functionals()
|
30 |
|
31 |
# 处理markdown文本格式的转变
|
@@ -59,15 +59,15 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
|
|
59 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
60 |
functional[k]["Button"] = gr.Button(k, variant=variant)
|
61 |
with gr.Row():
|
62 |
-
gr.Markdown("
|
63 |
with gr.Row():
|
64 |
for k in crazy_functional:
|
65 |
variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"
|
66 |
crazy_functional[k]["Button"] = gr.Button(k, variant=variant)
|
67 |
with gr.Row():
|
68 |
-
gr.Markdown("
|
69 |
with gr.Row():
|
70 |
-
file_upload = gr.Files(file_count="multiple")
|
71 |
|
72 |
from check_proxy import check_proxy
|
73 |
statusDisplay = gr.Markdown(f"{check_proxy(proxies)}")
|
@@ -82,11 +82,15 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
|
|
82 |
for k in functional:
|
83 |
functional[k]["Button"].click(predict,
|
84 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
|
|
|
85 |
for k in crazy_functional:
|
86 |
-
crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
87 |
-
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay]
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
90 |
# 延迟函数,做一些准备工作,最后尝试打开浏览器
|
91 |
def auto_opentab_delay():
|
92 |
import threading, webbrowser, time
|
|
|
25 |
functional = get_functionals()
|
26 |
|
27 |
# 对一些丧心病狂的实验性功能模块进行测试
|
28 |
+
from functional_crazy import get_crazy_functionals, on_file_uploaded, on_report_generated
|
29 |
crazy_functional = get_crazy_functionals()
|
30 |
|
31 |
# 处理markdown文本格式的转变
|
|
|
59 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
60 |
functional[k]["Button"] = gr.Button(k, variant=variant)
|
61 |
with gr.Row():
|
62 |
+
gr.Markdown("以下部分实验性功能需读取路径.")
|
63 |
with gr.Row():
|
64 |
for k in crazy_functional:
|
65 |
variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"
|
66 |
crazy_functional[k]["Button"] = gr.Button(k, variant=variant)
|
67 |
with gr.Row():
|
68 |
+
gr.Markdown("上传本地文件,调用实验函数.")
|
69 |
with gr.Row():
|
70 |
+
file_upload = gr.Files(label='任何文件,但推荐上传压缩文件(zip, tar)', file_count="multiple")
|
71 |
|
72 |
from check_proxy import check_proxy
|
73 |
statusDisplay = gr.Markdown(f"{check_proxy(proxies)}")
|
|
|
82 |
for k in functional:
|
83 |
functional[k]["Button"].click(predict,
|
84 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
|
85 |
+
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
|
86 |
for k in crazy_functional:
|
87 |
+
click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
88 |
+
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay]
|
89 |
+
)
|
90 |
+
try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
|
91 |
+
except: pass
|
92 |
+
|
93 |
+
|
94 |
# 延迟函数,做一些准备工作,最后尝试打开浏览器
|
95 |
def auto_opentab_delay():
|
96 |
import threading, webbrowser, time
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
gradio
|
2 |
requests[socks]
|
3 |
mdtex2html
|
|
|
1 |
+
gradio>=3.23
|
2 |
requests[socks]
|
3 |
mdtex2html
|
toolbox.py
CHANGED
@@ -142,4 +142,44 @@ def find_free_port():
|
|
142 |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
143 |
s.bind(('', 0))
|
144 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
145 |
-
return s.getsockname()[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
143 |
s.bind(('', 0))
|
144 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
145 |
+
return s.getsockname()[1]
|
146 |
+
|
147 |
+
|
148 |
+
def extract_archive(file_path, dest_dir):
|
149 |
+
import zipfile
|
150 |
+
import tarfile
|
151 |
+
import os
|
152 |
+
# Get the file extension of the input file
|
153 |
+
file_extension = os.path.splitext(file_path)[1]
|
154 |
+
|
155 |
+
# Extract the archive based on its extension
|
156 |
+
if file_extension == '.zip':
|
157 |
+
with zipfile.ZipFile(file_path, 'r') as zipobj:
|
158 |
+
zipobj.extractall(path=dest_dir)
|
159 |
+
print("Successfully extracted zip archive to {}".format(dest_dir))
|
160 |
+
|
161 |
+
elif file_extension in ['.tar', '.gz', '.bz2']:
|
162 |
+
with tarfile.open(file_path, 'r:*') as tarobj:
|
163 |
+
tarobj.extractall(path=dest_dir)
|
164 |
+
print("Successfully extracted tar archive to {}".format(dest_dir))
|
165 |
+
else:
|
166 |
+
return
|
167 |
+
|
168 |
+
def find_recent_files(directory):
|
169 |
+
"""
|
170 |
+
me: find files that is created with in one minutes under a directory with python, write a function
|
171 |
+
gpt: here it is!
|
172 |
+
"""
|
173 |
+
import os
|
174 |
+
import time
|
175 |
+
current_time = time.time()
|
176 |
+
one_minute_ago = current_time - 60
|
177 |
+
recent_files = []
|
178 |
+
|
179 |
+
for filename in os.listdir(directory):
|
180 |
+
file_path = os.path.join(directory, filename)
|
181 |
+
created_time = os.path.getctime(file_path)
|
182 |
+
if created_time >= one_minute_ago:
|
183 |
+
recent_files.append(file_path)
|
184 |
+
|
185 |
+
return recent_files
|