Your Name commited on
Commit
e39c511
1 Parent(s): 1a301e0

程序自解析功能

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. functional_crazy.py +112 -0
  3. main.py +14 -2
  4. predict.py +53 -0
.gitignore CHANGED
@@ -134,3 +134,4 @@ dmypy.json
134
  history
135
  ssr_conf
136
  config_private.py
 
 
134
  history
135
  ssr_conf
136
  config_private.py
137
+ gpt_log
functional_crazy.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # """
2
+ # 'primary' for main call-to-action,
3
+ # 'secondary' for a more subdued style,
4
+ # 'stop' for a stop button.
5
+ # """
6
+ def 自我程序解构简单案例(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
7
+ import time
8
+ from predict import predict_no_ui_no_history
9
+ for i in range(5):
10
+ i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}'
11
+ gpt_say = predict_no_ui_no_history(inputs=i_say, top_p=top_p, temperature=temperature)
12
+ chatbot.append((i_say, gpt_say))
13
+ history.append(i_say)
14
+ history.append(gpt_say)
15
+ yield chatbot, history, '正常'
16
+ time.sleep(10)
17
+
18
+ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
19
+ import time, glob, os
20
+ from predict import predict_no_ui
21
+ file_manifest = [f for f in glob.glob('*.py')]
22
+
23
+ for index, fp in enumerate(file_manifest):
24
+ with open(fp, 'r', encoding='utf-8') as f:
25
+ file_content = f.read()
26
+
27
+ 前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
28
+ i_say = f'请对下面的程序文件做一个概述: ```{file_content}```'
29
+ i_say_show_user = 前言 + f'请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
30
+ chatbot.append((i_say_show_user, "[waiting gpt response]"))
31
+ yield chatbot, history, '正常'
32
+
33
+ # ** gpt request **
34
+ gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature)
35
+
36
+ chatbot[-1] = (i_say_show_user, gpt_say)
37
+ history.append(i_say_show_user); history.append(gpt_say)
38
+ yield chatbot, history, '正常'
39
+ time.sleep(2)
40
+
41
+ i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
42
+ chatbot.append((i_say, "[waiting gpt response]"))
43
+ yield chatbot, history, '正常'
44
+
45
+ # ** gpt request **
46
+ gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history)
47
+
48
+ chatbot[-1] = (i_say, gpt_say)
49
+ history.append(i_say); history.append(gpt_say)
50
+ yield chatbot, history, '正常'
51
+
52
+
53
+ def 解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
54
+ import time, glob, os
55
+ from predict import predict_no_ui
56
+ if os.path.exists(txt):
57
+ project_folder = txt
58
+ else:
59
+ chatbot.append((f"解析项目: {txt}", "找不到本地项目: {txt}"))
60
+ history.append(i_say_show_user); history.append(gpt_say)
61
+ return chatbot, history, '正常'
62
+
63
+ file_manifest = [f for f in glob.glob(f'{project_folder}/*.py')]
64
+ print('begin analysis on:', file_manifest)
65
+ for index, fp in enumerate(file_manifest):
66
+ with open(fp, 'r', encoding='utf-8') as f:
67
+ file_content = f.read()
68
+
69
+ 前言 = "接下来请你逐文件分析下面的Python工程" if index==0 else ""
70
+ i_say = f'请对下面的程序文件做一个概述: ```{file_content}```'
71
+ i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
72
+ chatbot.append((i_say_show_user, "[waiting gpt response]"))
73
+ print('[1] yield chatbot, history')
74
+ yield chatbot, history, '正常'
75
+
76
+ # ** gpt request **
77
+ gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature)
78
+
79
+ print('[2] end gpt req')
80
+ chatbot[-1] = (i_say_show_user, gpt_say)
81
+ history.append(i_say_show_user); history.append(gpt_say)
82
+ print('[3] yield chatbot, history')
83
+ yield chatbot, history, '正常'
84
+ print('[4] next')
85
+ time.sleep(2)
86
+
87
+ i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
88
+ chatbot.append((i_say, "[waiting gpt response]"))
89
+ yield chatbot, history, '正常'
90
+
91
+ # ** gpt request **
92
+ gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history)
93
+
94
+ chatbot[-1] = (i_say, gpt_say)
95
+ history.append(i_say); history.append(gpt_say)
96
+ yield chatbot, history, '正常'
97
+
98
+
99
+ def get_crazy_functionals():
100
+ return {
101
+ "程序解构简单案例": {
102
+ "Function": 自我程序解构简单案例
103
+ },
104
+ "请解析并解构此项目本身": {
105
+ "Function": 解析项目本身
106
+ },
107
+ "解析一整个Python项目(输入栏给定项目完整目录)": {
108
+ "Function": 解析一个Python项目
109
+ },
110
+ }
111
+
112
+
main.py CHANGED
@@ -25,8 +25,14 @@ os.makedirs('gpt_log', exist_ok=True)
25
  logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8')
26
  print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log,请注意自我隐私保护哦!')
27
 
 
28
  from functional import get_functionals
29
  functional = get_functionals()
 
 
 
 
 
30
  def reset_textbox(): return gr.update(value='')
31
 
32
  def text_divide_paragraph(text):
@@ -69,7 +75,7 @@ with gr.Blocks() as demo:
69
  with gr.Row():
70
  with gr.Column(scale=2):
71
  chatbot = gr.Chatbot()
72
- chatbot.style(height=700)
73
  chatbot.style()
74
  history = gr.State([])
75
  TRUE = gr.State(True)
@@ -84,6 +90,9 @@ with gr.Blocks() as demo:
84
  for k in functional:
85
  variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
86
  functional[k]["Button"] = gr.Button(k, variant=variant)
 
 
 
87
  from check_proxy import check_proxy
88
  statusDisplay = gr.Markdown(f"{check_proxy(proxies)}")
89
  systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True)
@@ -97,7 +106,10 @@ with gr.Blocks() as demo:
97
  # submitBtn.click(reset_textbox, [], [txt])
98
  for k in functional:
99
  functional[k]["Button"].click(predict,
100
- [txt, top_p, temperature, chatbot,history, systemPromptTxt, FALSE, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
 
 
 
101
 
102
  print(f"URL http://localhost:{PORT}")
103
  demo.title = "ChatGPT 学术优化"
 
25
  logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8')
26
  print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log,请注意自我隐私保护哦!')
27
 
28
+ # 一些普通功能
29
  from functional import get_functionals
30
  functional = get_functionals()
31
+
32
+ # 对一些丧心病狂的实验性功能进行测试
33
+ from functional_crazy import get_crazy_functionals
34
+ crazy_functional = get_crazy_functionals()
35
+
36
  def reset_textbox(): return gr.update(value='')
37
 
38
  def text_divide_paragraph(text):
 
75
  with gr.Row():
76
  with gr.Column(scale=2):
77
  chatbot = gr.Chatbot()
78
+ chatbot.style(height=1000)
79
  chatbot.style()
80
  history = gr.State([])
81
  TRUE = gr.State(True)
 
90
  for k in functional:
91
  variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
92
  functional[k]["Button"] = gr.Button(k, variant=variant)
93
+ for k in crazy_functional:
94
+ variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"
95
+ crazy_functional[k]["Button"] = gr.Button(k, variant=variant)
96
  from check_proxy import check_proxy
97
  statusDisplay = gr.Markdown(f"{check_proxy(proxies)}")
98
  systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True)
 
106
  # submitBtn.click(reset_textbox, [], [txt])
107
  for k in functional:
108
  functional[k]["Button"].click(predict,
109
+ [txt, top_p, temperature, chatbot, history, systemPromptTxt, FALSE, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
110
+ for k in crazy_functional:
111
+ crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
112
+ [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
113
 
114
  print(f"URL http://localhost:{PORT}")
115
  demo.title = "ChatGPT 学术优化"
predict.py CHANGED
@@ -14,6 +14,59 @@ except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS
14
 
15
  timeout_bot_msg = 'Request timeout, network error. please check proxy settings in config.py.'
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', retry=False,
18
  stream = True, additional_fn=None):
19
 
 
14
 
15
  timeout_bot_msg = 'Request timeout, network error. please check proxy settings in config.py.'
16
 
17
+
18
+ def predict_no_ui(inputs, top_p, temperature, history=[]):
19
+ messages = [{"role": "system", "content": ""}]
20
+
21
+ #
22
+ chat_counter = len(history) // 2
23
+ if chat_counter > 0:
24
+ for index in range(0, 2*chat_counter, 2):
25
+ what_i_have_asked = {}
26
+ what_i_have_asked["role"] = "user"
27
+ what_i_have_asked["content"] = history[index]
28
+ what_gpt_answer = {}
29
+ what_gpt_answer["role"] = "assistant"
30
+ what_gpt_answer["content"] = history[index+1]
31
+ if what_i_have_asked["content"] != "":
32
+ messages.append(what_i_have_asked)
33
+ messages.append(what_gpt_answer)
34
+ else:
35
+ messages[-1]['content'] = what_gpt_answer['content']
36
+
37
+ what_i_ask_now = {}
38
+ what_i_ask_now["role"] = "user"
39
+ what_i_ask_now["content"] = inputs
40
+ messages.append(what_i_ask_now)
41
+
42
+ # messages
43
+ payload = {
44
+ "model": "gpt-3.5-turbo",
45
+ # "model": "gpt-4",
46
+ "messages": messages,
47
+ "temperature": temperature, # 1.0,
48
+ "top_p": top_p, # 1.0,
49
+ "n": 1,
50
+ "stream": False,
51
+ "presence_penalty": 0,
52
+ "frequency_penalty": 0,
53
+ }
54
+
55
+ headers = {
56
+ "Content-Type": "application/json",
57
+ "Authorization": f"Bearer {API_KEY}"
58
+ }
59
+ try:
60
+ # make a POST request to the API endpoint using the requests.post method, passing in stream=True
61
+ response = requests.post(API_URL, headers=headers, proxies=proxies,
62
+ json=payload, stream=True, timeout=TIMEOUT_SECONDS*2)
63
+ except:
64
+ raise TimeoutError
65
+
66
+ return json.loads(response.text)["choices"][0]["message"]["content"]
67
+
68
+
69
+
70
  def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', retry=False,
71
  stream = True, additional_fn=None):
72