qingxu99 commited on
Commit
3725122
1 Parent(s): 1f6defe

增加临时输入api-key的功能

Browse files
colorful.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import platform
2
+ from sys import stdout
3
+
4
+ if platform.system()=="Linux":
5
+ pass
6
+ else:
7
+ from colorama import init
8
+ init()
9
+
10
+ # Do you like the elegance of Chinese characters?
11
+ def print红(*kw,**kargs):
12
+ print("\033[0;31m",*kw,"\033[0m",**kargs)
13
+ def print绿(*kw,**kargs):
14
+ print("\033[0;32m",*kw,"\033[0m",**kargs)
15
+ def print黄(*kw,**kargs):
16
+ print("\033[0;33m",*kw,"\033[0m",**kargs)
17
+ def print蓝(*kw,**kargs):
18
+ print("\033[0;34m",*kw,"\033[0m",**kargs)
19
+ def print紫(*kw,**kargs):
20
+ print("\033[0;35m",*kw,"\033[0m",**kargs)
21
+ def print靛(*kw,**kargs):
22
+ print("\033[0;36m",*kw,"\033[0m",**kargs)
23
+
24
+ def print亮红(*kw,**kargs):
25
+ print("\033[1;31m",*kw,"\033[0m",**kargs)
26
+ def print亮绿(*kw,**kargs):
27
+ print("\033[1;32m",*kw,"\033[0m",**kargs)
28
+ def print亮黄(*kw,**kargs):
29
+ print("\033[1;33m",*kw,"\033[0m",**kargs)
30
+ def print亮蓝(*kw,**kargs):
31
+ print("\033[1;34m",*kw,"\033[0m",**kargs)
32
+ def print亮紫(*kw,**kargs):
33
+ print("\033[1;35m",*kw,"\033[0m",**kargs)
34
+ def print亮靛(*kw,**kargs):
35
+ print("\033[1;36m",*kw,"\033[0m",**kargs)
36
+
37
+
38
+
39
+ def print亮红(*kw,**kargs):
40
+ print("\033[1;31m",*kw,"\033[0m",**kargs)
41
+ def print亮绿(*kw,**kargs):
42
+ print("\033[1;32m",*kw,"\033[0m",**kargs)
43
+ def print亮黄(*kw,**kargs):
44
+ print("\033[1;33m",*kw,"\033[0m",**kargs)
45
+ def print亮蓝(*kw,**kargs):
46
+ print("\033[1;34m",*kw,"\033[0m",**kargs)
47
+ def print亮紫(*kw,**kargs):
48
+ print("\033[1;35m",*kw,"\033[0m",**kargs)
49
+ def print亮靛(*kw,**kargs):
50
+ print("\033[1;36m",*kw,"\033[0m",**kargs)
51
+
52
+ print_red = print红
53
+ print_green = print绿
54
+ print_yellow = print黄
55
+ print_blue = print蓝
56
+ print_purple = print紫
57
+ print_indigo = print靛
58
+
59
+ print_bold_red = print亮红
60
+ print_bold_green = print亮绿
61
+ print_bold_yellow = print亮黄
62
+ print_bold_blue = print亮蓝
63
+ print_bold_purple = print亮紫
64
+ print_bold_indigo = print亮靛
65
+
66
+ if not stdout.isatty():
67
+ # redirection, avoid a fucked up log file
68
+ print红 = print
69
+ print绿 = print
70
+ print黄 = print
71
+ print蓝 = print
72
+ print紫 = print
73
+ print靛 = print
74
+ print亮红 = print
75
+ print亮绿 = print
76
+ print亮黄 = print
77
+ print亮蓝 = print
78
+ print亮紫 = print
79
+ print亮靛 = print
80
+ print_red = print
81
+ print_green = print
82
+ print_yellow = print
83
+ print_blue = print
84
+ print_purple = print
85
+ print_indigo = print
86
+ print_bold_red = print
87
+ print_bold_green = print
88
+ print_bold_yellow = print
89
+ print_bold_blue = print
90
+ print_bold_purple = print
91
+ print_bold_indigo = print
crazy_functions/代码重写为全英文_多线程.py CHANGED
@@ -29,16 +29,16 @@ def 全项目切换英文(txt, llm_kwargs, plugin_kwargs, chatbot, history, sys_
29
 
30
  # 第2步:尝试导入依赖,如果缺少依赖,则给出安装建议
31
  try:
32
- import openai, transformers
33
  except:
34
  report_execption(chatbot, history,
35
  a = f"解析项目: {txt}",
36
- b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade openai transformers```。")
37
  yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
38
  return
39
 
40
  # 第3步:集合文件
41
- import time, glob, os, shutil, re, openai
42
  os.makedirs('gpt_log/generated_english_version', exist_ok=True)
43
  os.makedirs('gpt_log/generated_english_version/crazy_functions', exist_ok=True)
44
  file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
 
29
 
30
  # 第2步:尝试导入依赖,如果缺少依赖,则给出安装建议
31
  try:
32
+ import tiktoken
33
  except:
34
  report_execption(chatbot, history,
35
  a = f"解析项目: {txt}",
36
+ b = f"导入软件依赖失败。使用该模块需要额外依赖,安装方法```pip install --upgrade tiktoken```。")
37
  yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
38
  return
39
 
40
  # 第3步:集合文件
41
+ import time, glob, os, shutil, re
42
  os.makedirs('gpt_log/generated_english_version', exist_ok=True)
43
  os.makedirs('gpt_log/generated_english_version/crazy_functions', exist_ok=True)
44
  file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \
main.py CHANGED
@@ -4,8 +4,8 @@ from request_llm.bridge_chatgpt import predict
4
  from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
- proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT = \
8
- get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT')
9
 
10
  # 如果WEB_PORT是-1, 则随机选取WEB端口
11
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
@@ -52,7 +52,7 @@ if LAYOUT == "TOP-DOWN":
52
  cancel_handles = []
53
  with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
54
  gr.HTML(title_html)
55
- cookies = gr.State({})
56
  with gr_L1():
57
  with gr_L2(scale=2):
58
  chatbot = gr.Chatbot()
 
4
  from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
+ proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY = \
8
+ get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY')
9
 
10
  # 如果WEB_PORT是-1, 则随机选取WEB端口
11
  PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
 
52
  cancel_handles = []
53
  with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
54
  gr.HTML(title_html)
55
+ cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL})
56
  with gr_L1():
57
  with gr_L2(scale=2):
58
  chatbot = gr.Chatbot()
request_llm/bridge_chatgpt.py CHANGED
@@ -145,6 +145,16 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
145
  chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
146
  additional_fn代表点击的哪个按钮,按钮见functional.py
147
  """
 
 
 
 
 
 
 
 
 
 
148
  if additional_fn is not None:
149
  import core_functional
150
  importlib.reload(core_functional) # 热更新prompt
@@ -224,9 +234,12 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
224
  """
225
  整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
226
  """
 
 
 
227
  headers = {
228
  "Content-Type": "application/json",
229
- "Authorization": f"Bearer {API_KEY}"
230
  }
231
 
232
  conversation_cnt = len(history) // 2
@@ -254,7 +267,7 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
254
  messages.append(what_i_ask_now)
255
 
256
  payload = {
257
- "model": LLM_MODEL,
258
  "messages": messages,
259
  "temperature": llm_kwargs['temperature'], # 1.0,
260
  "top_p": llm_kwargs['top_p'], # 1.0,
@@ -263,7 +276,7 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
263
  "presence_penalty": 0,
264
  "frequency_penalty": 0,
265
  }
266
- print(f" {LLM_MODEL} : {conversation_cnt} : {inputs[:100]}")
267
  return headers,payload
268
 
269
 
 
145
  chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
146
  additional_fn代表点击的哪个按钮,按钮见functional.py
147
  """
148
+ if inputs.startswith('sk-') and len(inputs) == 51:
149
+ chatbot._cookies['api_key'] = inputs
150
+ chatbot.append(("输入已识别为openai的api_key", "api_key已导入"))
151
+ yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") # 刷新界面
152
+ return
153
+ elif len(chatbot._cookies['api_key']) != 51:
154
+ chatbot.append((inputs, "缺少api_key。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。"))
155
+ yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") # 刷新界面
156
+ return
157
+
158
  if additional_fn is not None:
159
  import core_functional
160
  importlib.reload(core_functional) # 热更新prompt
 
234
  """
235
  整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
236
  """
237
+ if len(llm_kwargs['api_key']) != 51:
238
+ raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。")
239
+
240
  headers = {
241
  "Content-Type": "application/json",
242
+ "Authorization": f"Bearer {llm_kwargs['api_key']}"
243
  }
244
 
245
  conversation_cnt = len(history) // 2
 
267
  messages.append(what_i_ask_now)
268
 
269
  payload = {
270
+ "model": llm_kwargs['llm_model'],
271
  "messages": messages,
272
  "temperature": llm_kwargs['temperature'], # 1.0,
273
  "top_p": llm_kwargs['top_p'], # 1.0,
 
276
  "presence_penalty": 0,
277
  "frequency_penalty": 0,
278
  }
279
+ print(f" {llm_kwargs['llm_model']} : {conversation_cnt} : {inputs[:100]}")
280
  return headers,payload
281
 
282
 
requirements.txt CHANGED
@@ -5,6 +5,7 @@ python-markdown-math
5
  beautifulsoup4
6
  latex2mathml
7
  mdtex2html
 
8
  tiktoken
9
  Markdown
10
  pygments
 
5
  beautifulsoup4
6
  latex2mathml
7
  mdtex2html
8
+ colorama
9
  tiktoken
10
  Markdown
11
  pygments
show_math.py DELETED
@@ -1,80 +0,0 @@
1
- # This program is written by: https://github.com/polarwinkel/mdtex2html
2
-
3
- from latex2mathml.converter import convert as tex2mathml
4
- import re
5
-
6
- incomplete = '<font style="color:orange;" class="tooltip">&#9888;<span class="tooltiptext">formula incomplete</span></font>'
7
- convError = '<font style="color:red" class="tooltip">&#9888;<span class="tooltiptext">LaTeX-convert-error</span></font>'
8
-
9
- def convert(mdtex, extensions=[], splitParagraphs=True):
10
- ''' converts recursively the Markdown-LaTeX-mixture to HTML with MathML '''
11
- found = False
12
- # handle all paragraphs separately (prevents aftereffects)
13
- if splitParagraphs:
14
- parts = re.split("\n\n", mdtex)
15
- result = ''
16
- for part in parts:
17
- result += convert(part, extensions, splitParagraphs=False)
18
- return result
19
- # find first $$-formula:
20
- parts = re.split('\${2}', mdtex, 2)
21
- if len(parts)>1:
22
- found = True
23
- result = convert(parts[0], extensions, splitParagraphs=False)+'\n'
24
- try:
25
- result += '<div class="blockformula">'+tex2mathml(parts[1])+'</div>\n'
26
- except:
27
- result += '<div class="blockformula">'+convError+'</div>'
28
- if len(parts)==3:
29
- result += convert(parts[2], extensions, splitParagraphs=False)
30
- else:
31
- result += '<div class="blockformula">'+incomplete+'</div>'
32
- # else find first $-formulas:
33
- else:
34
- parts = re.split('\${1}', mdtex, 2)
35
- if len(parts)>1 and not found:
36
- found = True
37
- try:
38
- mathml = tex2mathml(parts[1])
39
- except:
40
- mathml = convError
41
- if parts[0].endswith('\n\n') or parts[0]=='': # make sure textblock starts before formula!
42
- parts[0]=parts[0]+'&#x200b;'
43
- if len(parts)==3:
44
- result = convert(parts[0]+mathml+parts[2], extensions, splitParagraphs=False)
45
- else:
46
- result = convert(parts[0]+mathml+incomplete, extensions, splitParagraphs=False)
47
- # else find first \[..\]-equation:
48
- else:
49
- parts = re.split(r'\\\[', mdtex, 1)
50
- if len(parts)>1 and not found:
51
- found = True
52
- result = convert(parts[0], extensions, splitParagraphs=False)+'\n'
53
- parts = re.split(r'\\\]', parts[1], 1)
54
- try:
55
- result += '<div class="blockformula">'+tex2mathml(parts[0])+'</div>\n'
56
- except:
57
- result += '<div class="blockformula">'+convError+'</div>'
58
- if len(parts)==2:
59
- result += convert(parts[1], extensions, splitParagraphs=False)
60
- else:
61
- result += '<div class="blockformula">'+incomplete+'</div>'
62
- # else find first \(..\)-equation:
63
- else:
64
- parts = re.split(r'\\\(', mdtex, 1)
65
- if len(parts)>1 and not found:
66
- found = True
67
- subp = re.split(r'\\\)', parts[1], 1)
68
- try:
69
- mathml = tex2mathml(subp[0])
70
- except:
71
- mathml = convError
72
- if parts[0].endswith('\n\n') or parts[0]=='': # make sure textblock starts before formula!
73
- parts[0]=parts[0]+'&#x200b;'
74
- if len(subp)==2:
75
- result = convert(parts[0]+mathml+subp[1], extensions, splitParagraphs=False)
76
- else:
77
- result = convert(parts[0]+mathml+incomplete, extensions, splitParagraphs=False)
78
- if not found:
79
- result = mdtex
80
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
toolbox.py CHANGED
@@ -12,7 +12,7 @@ from functools import wraps, lru_cache
12
  ############################### 插件输入输出接驳区 #######################################
13
  class ChatBotWithCookies(list):
14
  def __init__(self, cookie):
15
- self._cookie = cookie
16
 
17
  def write_list(self, list):
18
  for t in list:
@@ -22,7 +22,7 @@ class ChatBotWithCookies(list):
22
  return [t for t in self]
23
 
24
  def get_cookies(self):
25
- return self._cookie
26
 
27
  def ArgsGeneralWrapper(f):
28
  """
@@ -37,10 +37,13 @@ def ArgsGeneralWrapper(f):
37
  'temperature':temperature,
38
  })
39
  llm_kwargs = {
 
 
40
  'top_p':top_p,
41
  'temperature':temperature,
42
  }
43
  plugin_kwargs = {
 
44
  }
45
  chatbot_with_cookie = ChatBotWithCookies(cookies)
46
  chatbot_with_cookie.write_list(chatbot)
@@ -473,27 +476,30 @@ def on_report_generated(files, chatbot):
473
  chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。'])
474
  return report_files, chatbot
475
 
 
 
 
 
476
 
477
  @lru_cache(maxsize=128)
478
  def read_single_conf_with_lru_cache(arg):
 
479
  try:
480
  r = getattr(importlib.import_module('config_private'), arg)
481
  except:
482
  r = getattr(importlib.import_module('config'), arg)
483
  # 在读取API_KEY时,检查一下是不是忘了改config
484
  if arg == 'API_KEY':
485
- # 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
486
- API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", r)
487
- if API_MATCH:
488
- print(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功")
489
  else:
490
- assert False, "正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
491
- "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
492
  if arg == 'proxies':
493
  if r is None:
494
- print('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。')
495
  else:
496
- print('[PROXY] 网络代理状态:已配置。配置信息如下:', r)
497
  assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。'
498
  return r
499
 
 
12
  ############################### 插件输入输出接驳区 #######################################
13
  class ChatBotWithCookies(list):
14
  def __init__(self, cookie):
15
+ self._cookies = cookie
16
 
17
  def write_list(self, list):
18
  for t in list:
 
22
  return [t for t in self]
23
 
24
  def get_cookies(self):
25
+ return self._cookies
26
 
27
  def ArgsGeneralWrapper(f):
28
  """
 
37
  'temperature':temperature,
38
  })
39
  llm_kwargs = {
40
+ 'api_key': cookies['api_key'],
41
+ 'llm_model': cookies['llm_model'],
42
  'top_p':top_p,
43
  'temperature':temperature,
44
  }
45
  plugin_kwargs = {
46
+ # 目前还没有
47
  }
48
  chatbot_with_cookie = ChatBotWithCookies(cookies)
49
  chatbot_with_cookie.write_list(chatbot)
 
476
  chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。'])
477
  return report_files, chatbot
478
 
479
+ def is_openai_api_key(key):
480
+ # 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
481
+ API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", key)
482
+ return API_MATCH
483
 
484
  @lru_cache(maxsize=128)
485
  def read_single_conf_with_lru_cache(arg):
486
+ from colorful import print亮红, print亮绿
487
  try:
488
  r = getattr(importlib.import_module('config_private'), arg)
489
  except:
490
  r = getattr(importlib.import_module('config'), arg)
491
  # 在读取API_KEY时,检查一下是不是忘了改config
492
  if arg == 'API_KEY':
493
+ if is_openai_api_key(r):
494
+ print亮绿(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功")
 
 
495
  else:
496
+ print亮红( "[API_KEY] 正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
497
+ "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)")
498
  if arg == 'proxies':
499
  if r is None:
500
+ print亮红('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。')
501
  else:
502
+ print亮绿('[PROXY] 网络代理状态:已配置。配置信息如下:', r)
503
  assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。'
504
  return r
505