Skip to content

Commit

Permalink
change get_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-husky committed Oct 28, 2023
1 parent 12b2a22 commit 527f9d2
Show file tree
Hide file tree
Showing 32 changed files with 85 additions and 50 deletions.
6 changes: 3 additions & 3 deletions check_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def backup_and_download(current_version, remote_version):
return new_version_dir
os.makedirs(new_version_dir)
shutil.copytree('./', backup_dir, ignore=lambda x, y: ['history'])
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
r = requests.get(
'https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True)
zip_file_path = backup_dir+'/master.zip'
Expand Down Expand Up @@ -113,7 +113,7 @@ def auto_update(raise_error=False):
import requests
import time
import json
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
response = requests.get(
"https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=5)
remote_json_data = json.loads(response.text)
Expand Down Expand Up @@ -167,5 +167,5 @@ def warm_up_modules():
import os
os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
from toolbox import get_conf
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
check_proxy(proxies)
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
"Warmup_Modules", "Nougat_Download", "AutoGen"]


# *实验性功能*: 自动检测并屏蔽失效的KEY,请勿使用
BLOCK_INVALID_APIKEY = False


# 自定义按钮的最大数量限制
NUM_CUSTOM_BASIC_BTN = 4

Expand Down
2 changes: 1 addition & 1 deletion crazy_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def get_crazy_functions():

try:
from toolbox import get_conf
ENABLE_AUDIO, = get_conf('ENABLE_AUDIO')
ENABLE_AUDIO = get_conf('ENABLE_AUDIO')
if ENABLE_AUDIO:
from crazy_functions.语音助手 import 语音助手
function_plugins.update({
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/Latex输出PDF结果.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def is_float(s):
yield from update_ui_lastest_msg("调用缓存", chatbot=chatbot, history=history) # 刷新界面
else:
yield from update_ui_lastest_msg("开始下载", chatbot=chatbot, history=history) # 刷新界面
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
r = requests.get(url_tar, proxies=proxies)
with open(dst, 'wb+') as f:
f.write(r.content)
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/agent_fns/pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, w
self.system_prompt = system_prompt
self.web_port = web_port
self.alive = True
self.use_docker, = get_conf('AUTOGEN_USE_DOCKER')
self.use_docker = get_conf('AUTOGEN_USE_DOCKER')

# create a thread to monitor self.heartbeat, terminate the instance if no heartbeat for a long time
timeout_seconds = 5*60
Expand Down
4 changes: 2 additions & 2 deletions crazy_functions/crazy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
assert len(inputs_array) == len(history_array)
assert len(inputs_array) == len(sys_prompt_array)
if max_workers == -1: # 读取配置文件
try: max_workers, = get_conf('DEFAULT_WORKER_NUM')
try: max_workers = get_conf('DEFAULT_WORKER_NUM')
except: max_workers = 8
if max_workers <= 0: max_workers = 3
# 屏蔽掉 chatglm的多线程,可能会导致严重卡顿
Expand Down Expand Up @@ -602,7 +602,7 @@ def get_files_from_everything(txt, type): # type='.md'
import requests
from toolbox import get_conf
from toolbox import get_log_folder, gen_time_str
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
try:
r = requests.get(txt, proxies=proxies)
except:
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/pdf_fns/parse_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class GROBID_OFFLINE_EXCEPTION(Exception): pass

def get_avail_grobid_url():
GROBID_URLS, = get_conf('GROBID_URLS')
GROBID_URLS = get_conf('GROBID_URLS')
if len(GROBID_URLS) == 0: return None
try:
_grobid_url = random.choice(GROBID_URLS) # 随机负载均衡
Expand Down
4 changes: 2 additions & 2 deletions crazy_functions/vt_fns/vt_modify_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def modify_configuration_hot(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_intention):
ALLOW_RESET_CONFIG, = get_conf('ALLOW_RESET_CONFIG')
ALLOW_RESET_CONFIG = get_conf('ALLOW_RESET_CONFIG')
if not ALLOW_RESET_CONFIG:
yield from update_ui_lastest_msg(
lastmsg=f"当前配置不允许被修改!如需激活本功能,请在config.py中设置ALLOW_RESET_CONFIG=True后重启软件。",
Expand Down Expand Up @@ -66,7 +66,7 @@ class ModifyConfigurationIntention(BaseModel):
)

def modify_configuration_reboot(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_intention):
ALLOW_RESET_CONFIG, = get_conf('ALLOW_RESET_CONFIG')
ALLOW_RESET_CONFIG = get_conf('ALLOW_RESET_CONFIG')
if not ALLOW_RESET_CONFIG:
yield from update_ui_lastest_msg(
lastmsg=f"当前配置不允许被修改!如需激活本功能,请在config.py中设置ALLOW_RESET_CONFIG=True后重启软件。",
Expand Down
4 changes: 2 additions & 2 deletions crazy_functions/下载arxiv论文翻译摘要.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def download_arxiv_(url_pdf):
file_path = download_dir+title_str

print('下载中')
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
r = requests.get(requests_pdf_url, proxies=proxies)
with open(file_path, 'wb+') as f:
f.write(r.content)
Expand Down Expand Up @@ -77,7 +77,7 @@ def get_name(_url_):
# print('在缓存中')
# return arxiv_recall[_url_]

proxies, = get_conf('proxies')
proxies = get_conf('proxies')
res = requests.get(_url_, proxies=proxies)

bs = BeautifulSoup(res.text, 'html.parser')
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/图片生成.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def gen_image(llm_kwargs, prompt, resolution="256x256"):
import requests, json, time, os
from request_llms.bridge_all import model_info

proxies, = get_conf('proxies')
proxies = get_conf('proxies')
# Set up OpenAI API key and model
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
chat_endpoint = model_info[llm_kwargs['llm_model']]['endpoint']
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/多智能体.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def 多智能体终端(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_
return

# 检查当前的模型是否符合要求
API_URL_REDIRECT, = get_conf('API_URL_REDIRECT')
API_URL_REDIRECT = get_conf('API_URL_REDIRECT')
if len(API_URL_REDIRECT) > 0:
chatbot.append([f"处理任务: {txt}", f"暂不支持中转."])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/总结音视频.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def AnalyAudio(parse_prompt, file_manifest, llm_kwargs, chatbot, history):

chatbot.append([f"将 {i} 发送到openai音频解析终端 (whisper),当前参数:{parse_prompt}", "正在处理 ..."])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
response = requests.post(url, headers=headers, files=files, data=data, proxies=proxies).text

chatbot.append(["音频解析结果", response])
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/批量Markdown翻译.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_files_from_everything(txt, preference=''):
if txt.startswith('http'):
import requests
from toolbox import get_conf
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
# 网络的远程文件
if preference == 'Github':
logging.info('正在从github下载资源 ...')
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/联网的ChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def 连接网络回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, history, s

# ------------- < 第1步:爬取搜索引擎的结果 > -------------
from toolbox import get_conf
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
urls = google(txt, proxies)
history = []
if len(urls) == 0:
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/联网的ChatGPT_bing版.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def 连接bing搜索回答问题(txt, llm_kwargs, plugin_kwargs, chatbot, histor

# ------------- < 第1步:爬取搜索引擎的结果 > -------------
from toolbox import get_conf
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
urls = bing_search(txt, proxies)
history = []
if len(urls) == 0:
Expand Down
2 changes: 1 addition & 1 deletion crazy_functions/谷歌检索小助手.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_meta_information(url, chatbot, history):
from urllib.parse import urlparse
session = requests.session()

proxies, = get_conf('proxies')
proxies = get_conf('proxies')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
Expand Down
4 changes: 2 additions & 2 deletions docs/WithFastapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ nano config.py
+ demo.queue(concurrency_count=CONCURRENT_COUNT)

- # 如果需要在二级路径下运行
- # CUSTOM_PATH, = get_conf('CUSTOM_PATH')
- # CUSTOM_PATH = get_conf('CUSTOM_PATH')
- # if CUSTOM_PATH != "/":
- # from toolbox import run_gradio_in_subpath
- # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
- # else:
- # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png")

+ 如果需要在二级路径下运行
+ CUSTOM_PATH, = get_conf('CUSTOM_PATH')
+ CUSTOM_PATH = get_conf('CUSTOM_PATH')
+ if CUSTOM_PATH != "/":
+ from toolbox import run_gradio_in_subpath
+ run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
Expand Down
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main():
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME')
DARK_MODE, NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('DARK_MODE', 'NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE')
INIT_SYS_PROMPT, = get_conf('INIT_SYS_PROMPT')
INIT_SYS_PROMPT = get_conf('INIT_SYS_PROMPT')

# 如果WEB_PORT是-1, 则随机选取WEB端口
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
Expand Down Expand Up @@ -48,7 +48,7 @@ def main():

# 高级函数插件
from crazy_functional import get_crazy_functions
DEFAULT_FN_GROUPS, = get_conf('DEFAULT_FN_GROUPS')
DEFAULT_FN_GROUPS = get_conf('DEFAULT_FN_GROUPS')
plugins = get_crazy_functions()
all_plugin_groups = list(set([g for _, plugin in plugins.items() for g in plugin['Group'].split('|')]))
match_group = lambda tags, groups: any([g in groups for g in tags.split('|')])
Expand Down Expand Up @@ -436,7 +436,7 @@ def warm_up_mods(): time.sleep(4); warm_up_modules()
blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])

# 如果需要在二级路径下运行
# CUSTOM_PATH, = get_conf('CUSTOM_PATH')
# CUSTOM_PATH = get_conf('CUSTOM_PATH')
# if CUSTOM_PATH != "/":
# from toolbox import run_gradio_in_subpath
# run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
Expand Down
2 changes: 1 addition & 1 deletion multi_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

CACHE_ONLY = os.environ.get('CACHE_ONLY', False)

CACHE_FOLDER, = get_conf('PATH_LOGGING')
CACHE_FOLDER = get_conf('PATH_LOGGING')

blacklist = ['multi-language', CACHE_FOLDER, '.git', 'private_upload', 'multi_language.py', 'build', '.github', '.vscode', '__pycache__', 'venv']

Expand Down
4 changes: 2 additions & 2 deletions request_llms/bridge_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def decode(self, *args, **kwargs):
azure_endpoint = AZURE_ENDPOINT + f'openai/deployments/{AZURE_ENGINE}/chat/completions?api-version=2023-05-15'
# 兼容旧版的配置
try:
API_URL, = get_conf("API_URL")
API_URL = get_conf("API_URL")
if API_URL != "https://api.openai.com/v1/chat/completions":
openai_endpoint = API_URL
print("警告!API_URL配置选项将被弃用,请更换为API_URL_REDIRECT配置")
Expand Down Expand Up @@ -501,7 +501,7 @@ def decode(self, *args, **kwargs):
print(trimmed_format_exc())

# <-- 用于定义和切换多个azure模型 -->
AZURE_CFG_ARRAY, = get_conf("AZURE_CFG_ARRAY")
AZURE_CFG_ARRAY = get_conf("AZURE_CFG_ARRAY")
if len(AZURE_CFG_ARRAY) > 0:
for azure_model_name, azure_cfg_dict in AZURE_CFG_ARRAY.items():
# 可能会覆盖之前的配置,但这是意料之中的
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_chatglmft.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run(self):
# if not os.path.exists(conf): raise RuntimeError('找不到微调模型信息')
# with open(conf, 'r', encoding='utf8') as f:
# model_args = json.loads(f.read())
CHATGLM_PTUNING_CHECKPOINT, = get_conf('CHATGLM_PTUNING_CHECKPOINT')
CHATGLM_PTUNING_CHECKPOINT = get_conf('CHATGLM_PTUNING_CHECKPOINT')
assert os.path.exists(CHATGLM_PTUNING_CHECKPOINT), "找不到微调模型检查点"
conf = os.path.join(CHATGLM_PTUNING_CHECKPOINT, "config.json")
with open(conf, 'r', encoding='utf8') as f:
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_internlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_model_and_tokenizer(self):
# 🏃‍♂️🏃‍♂️🏃‍♂️ 子进程执行
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
device, = get_conf('LOCAL_MODEL_DEVICE')
device = get_conf('LOCAL_MODEL_DEVICE')
if self._model is None:
tokenizer = AutoTokenizer.from_pretrained("internlm/internlm-chat-7b", trust_remote_code=True)
if device=='cpu':
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_jittorllms_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_model():
import types
try:
if self.jittorllms_model is None:
device, = get_conf('LOCAL_MODEL_DEVICE')
device = get_conf('LOCAL_MODEL_DEVICE')
from .jittorllms.models import get_model
# availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"]
args_dict = {'model': 'llama'}
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_jittorllms_pangualpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_model():
import types
try:
if self.jittorllms_model is None:
device, = get_conf('LOCAL_MODEL_DEVICE')
device = get_conf('LOCAL_MODEL_DEVICE')
from .jittorllms.models import get_model
# availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"]
args_dict = {'model': 'pangualpha'}
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_jittorllms_rwkv.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_model():
import types
try:
if self.jittorllms_model is None:
device, = get_conf('LOCAL_MODEL_DEVICE')
device = get_conf('LOCAL_MODEL_DEVICE')
from .jittorllms.models import get_model
# availabel_models = ["chatglm", "pangualpha", "llama", "chatrwkv"]
args_dict = {'model': 'chatrwkv'}
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_newbingfree.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def ready(self):

async def async_run(self):
# 读取配置
NEWBING_STYLE, = get_conf('NEWBING_STYLE')
NEWBING_STYLE = get_conf('NEWBING_STYLE')
from request_llms.bridge_all import model_info
endpoint = model_info['newbing']['endpoint']
while True:
Expand Down
4 changes: 2 additions & 2 deletions request_llms/bridge_stackclaude.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ def run(self):
self.local_history = []
if (self.claude_model is None) or (not self.success):
# 代理设置
proxies, = get_conf('proxies')
proxies = get_conf('proxies')
if proxies is None:
self.proxies_https = None
else:
self.proxies_https = proxies['https']

try:
SLACK_CLAUDE_USER_TOKEN, = get_conf('SLACK_CLAUDE_USER_TOKEN')
SLACK_CLAUDE_USER_TOKEN = get_conf('SLACK_CLAUDE_USER_TOKEN')
self.claude_model = SlackClient(token=SLACK_CLAUDE_USER_TOKEN, proxy=self.proxies_https)
print('Claude组件初始化成功。')
except:
Expand Down
2 changes: 1 addition & 1 deletion request_llms/bridge_zhipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
model_name = '智谱AI大模型'

def validate_key():
ZHIPUAI_API_KEY, = get_conf("ZHIPUAI_API_KEY")
ZHIPUAI_API_KEY = get_conf("ZHIPUAI_API_KEY")
if ZHIPUAI_API_KEY == '': return False
return True

Expand Down
29 changes: 29 additions & 0 deletions request_llms/key_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random

def Singleton(cls):
_instance = {}

def _singleton(*args, **kargs):
if cls not in _instance:
_instance[cls] = cls(*args, **kargs)
return _instance[cls]

return _singleton


@Singleton
class OpenAI_ApiKeyManager():
def __init__(self, mode='blacklist') -> None:
# self.key_avail_list = []
self.key_black_list = []

def add_key_to_blacklist(self, key):
self.key_black_list.append(key)

def select_avail_key(self, key_list):
# select key from key_list, but avoid keys also in self.key_black_list, raise error if no key can be found
available_keys = [key for key in key_list if key not in self.key_black_list]
if not available_keys:
raise KeyError("No available key found.")
selected_key = random.choice(available_keys)
return selected_key
2 changes: 1 addition & 1 deletion themes/gradios.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def adjust_theme():
set_theme = gr.themes.ThemeClass()
with ProxyNetworkActivate('Download_Gradio_Theme'):
logging.info('正在下载Gradio主题,请稍等。')
THEME, = get_conf('THEME')
THEME = get_conf('THEME')
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')
if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-')
set_theme = set_theme.from_hub(THEME.lower())
Expand Down
2 changes: 1 addition & 1 deletion themes/theme.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gradio as gr
from toolbox import get_conf
THEME, = get_conf('THEME')
THEME = get_conf('THEME')

def load_dynamic_theme(THEME):
adjust_dynamic_theme = None
Expand Down
Loading

0 comments on commit 527f9d2

Please sign in to comment.