Spaces:
Runtime error
Runtime error
Delete check_proxy.py
Browse files- check_proxy.py +0 -176
check_proxy.py
DELETED
|
@@ -1,176 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
def check_proxy(proxies):
|
| 3 |
-
import requests
|
| 4 |
-
proxies_https = proxies['https'] if proxies is not None else '无'
|
| 5 |
-
try:
|
| 6 |
-
response = requests.get("https://ipapi.co/json/", proxies=proxies, timeout=4)
|
| 7 |
-
data = response.json()
|
| 8 |
-
if 'country_name' in data:
|
| 9 |
-
country = data['country_name']
|
| 10 |
-
result = f"代理配置 {proxies_https}, 代理所在地:{country}"
|
| 11 |
-
elif 'error' in data:
|
| 12 |
-
alternative = _check_with_backup_source(proxies)
|
| 13 |
-
if alternative is None:
|
| 14 |
-
result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限"
|
| 15 |
-
else:
|
| 16 |
-
result = f"代理配置 {proxies_https}, 代理所在地:{alternative}"
|
| 17 |
-
else:
|
| 18 |
-
result = f"代理配置 {proxies_https}, 代理数据解析失败:{data}"
|
| 19 |
-
print(result)
|
| 20 |
-
return result
|
| 21 |
-
except:
|
| 22 |
-
result = f"代理配置 {proxies_https}, 代理所在地查询超时,代理可能无效"
|
| 23 |
-
print(result)
|
| 24 |
-
return result
|
| 25 |
-
|
| 26 |
-
def _check_with_backup_source(proxies):
|
| 27 |
-
import random, string, requests
|
| 28 |
-
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=32))
|
| 29 |
-
try: return requests.get(f"http://{random_string}.edns.ip-api.com/json", proxies=proxies, timeout=4).json()['dns']['geo']
|
| 30 |
-
except: return None
|
| 31 |
-
|
| 32 |
-
def backup_and_download(current_version, remote_version):
|
| 33 |
-
"""
|
| 34 |
-
一键更新协议:备份和下载
|
| 35 |
-
"""
|
| 36 |
-
from toolbox import get_conf
|
| 37 |
-
import shutil
|
| 38 |
-
import os
|
| 39 |
-
import requests
|
| 40 |
-
import zipfile
|
| 41 |
-
os.makedirs(f'./history', exist_ok=True)
|
| 42 |
-
backup_dir = f'./history/backup-{current_version}/'
|
| 43 |
-
new_version_dir = f'./history/new-version-{remote_version}/'
|
| 44 |
-
if os.path.exists(new_version_dir):
|
| 45 |
-
return new_version_dir
|
| 46 |
-
os.makedirs(new_version_dir)
|
| 47 |
-
shutil.copytree('./', backup_dir, ignore=lambda x, y: ['history'])
|
| 48 |
-
proxies = get_conf('proxies')
|
| 49 |
-
try: r = requests.get('https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True)
|
| 50 |
-
except: r = requests.get('https://public.gpt-academic.top/publish/master.zip', proxies=proxies, stream=True)
|
| 51 |
-
zip_file_path = backup_dir+'/master.zip'
|
| 52 |
-
with open(zip_file_path, 'wb+') as f:
|
| 53 |
-
f.write(r.content)
|
| 54 |
-
dst_path = new_version_dir
|
| 55 |
-
with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
|
| 56 |
-
for zip_info in zip_ref.infolist():
|
| 57 |
-
dst_file_path = os.path.join(dst_path, zip_info.filename)
|
| 58 |
-
if os.path.exists(dst_file_path):
|
| 59 |
-
os.remove(dst_file_path)
|
| 60 |
-
zip_ref.extract(zip_info, dst_path)
|
| 61 |
-
return new_version_dir
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def patch_and_restart(path):
|
| 65 |
-
"""
|
| 66 |
-
一键更新协议:覆盖和重启
|
| 67 |
-
"""
|
| 68 |
-
from distutils import dir_util
|
| 69 |
-
import shutil
|
| 70 |
-
import os
|
| 71 |
-
import sys
|
| 72 |
-
import time
|
| 73 |
-
import glob
|
| 74 |
-
from colorful import print亮黄, print亮绿, print亮红
|
| 75 |
-
# if not using config_private, move origin config.py as config_private.py
|
| 76 |
-
if not os.path.exists('config_private.py'):
|
| 77 |
-
print亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,',
|
| 78 |
-
'另外您可以随时在history子文件夹下找回旧版的程序。')
|
| 79 |
-
shutil.copyfile('config.py', 'config_private.py')
|
| 80 |
-
path_new_version = glob.glob(path + '/*-master')[0]
|
| 81 |
-
dir_util.copy_tree(path_new_version, './')
|
| 82 |
-
print亮绿('代码已经更新,即将更新pip包依赖……')
|
| 83 |
-
for i in reversed(range(5)): time.sleep(1); print(i)
|
| 84 |
-
try:
|
| 85 |
-
import subprocess
|
| 86 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
|
| 87 |
-
except:
|
| 88 |
-
print亮红('pip包依赖安装出现问题,需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')
|
| 89 |
-
print亮绿('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启')
|
| 90 |
-
print亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')
|
| 91 |
-
print(' ------------------------------ -----------------------------------')
|
| 92 |
-
for i in reversed(range(8)): time.sleep(1); print(i)
|
| 93 |
-
os.execl(sys.executable, sys.executable, *sys.argv)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def get_current_version():
|
| 97 |
-
import json
|
| 98 |
-
try:
|
| 99 |
-
with open('./version', 'r', encoding='utf8') as f:
|
| 100 |
-
current_version = json.loads(f.read())['version']
|
| 101 |
-
except:
|
| 102 |
-
current_version = ""
|
| 103 |
-
return current_version
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def auto_update(raise_error=False):
|
| 107 |
-
"""
|
| 108 |
-
一键更新协议:查询版本和用户意见
|
| 109 |
-
"""
|
| 110 |
-
try:
|
| 111 |
-
from toolbox import get_conf
|
| 112 |
-
import requests
|
| 113 |
-
import json
|
| 114 |
-
proxies = get_conf('proxies')
|
| 115 |
-
try: response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=5)
|
| 116 |
-
except: response = requests.get("https://public.gpt-academic.top/publish/version", proxies=proxies, timeout=5)
|
| 117 |
-
remote_json_data = json.loads(response.text)
|
| 118 |
-
remote_version = remote_json_data['version']
|
| 119 |
-
if remote_json_data["show_feature"]:
|
| 120 |
-
new_feature = "新功能:" + remote_json_data["new_feature"]
|
| 121 |
-
else:
|
| 122 |
-
new_feature = ""
|
| 123 |
-
with open('./version', 'r', encoding='utf8') as f:
|
| 124 |
-
current_version = f.read()
|
| 125 |
-
current_version = json.loads(current_version)['version']
|
| 126 |
-
if (remote_version - current_version) >= 0.01-1e-5:
|
| 127 |
-
from colorful import print亮黄
|
| 128 |
-
print亮黄(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}')
|
| 129 |
-
print('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
|
| 130 |
-
user_instruction = input('(2)是否一键更新代码(Y+回车=确认,输入其他/无输入+回车=不更新)?')
|
| 131 |
-
if user_instruction in ['Y', 'y']:
|
| 132 |
-
path = backup_and_download(current_version, remote_version)
|
| 133 |
-
try:
|
| 134 |
-
patch_and_restart(path)
|
| 135 |
-
except:
|
| 136 |
-
msg = '更新失败。'
|
| 137 |
-
if raise_error:
|
| 138 |
-
from toolbox import trimmed_format_exc
|
| 139 |
-
msg += trimmed_format_exc()
|
| 140 |
-
print(msg)
|
| 141 |
-
else:
|
| 142 |
-
print('自动更新程序:已禁用')
|
| 143 |
-
return
|
| 144 |
-
else:
|
| 145 |
-
return
|
| 146 |
-
except:
|
| 147 |
-
msg = '自动更新程序:已禁用。建议排查:代理网络配置。'
|
| 148 |
-
if raise_error:
|
| 149 |
-
from toolbox import trimmed_format_exc
|
| 150 |
-
msg += trimmed_format_exc()
|
| 151 |
-
print(msg)
|
| 152 |
-
|
| 153 |
-
def warm_up_modules():
|
| 154 |
-
print('正在执行一些模块的预热 ...')
|
| 155 |
-
from toolbox import ProxyNetworkActivate
|
| 156 |
-
from request_llms.bridge_all import model_info
|
| 157 |
-
with ProxyNetworkActivate("Warmup_Modules"):
|
| 158 |
-
enc = model_info["gpt-3.5-turbo"]['tokenizer']
|
| 159 |
-
enc.encode("模块预热", disallowed_special=())
|
| 160 |
-
enc = model_info["gpt-4"]['tokenizer']
|
| 161 |
-
enc.encode("模块预热", disallowed_special=())
|
| 162 |
-
|
| 163 |
-
def warm_up_vectordb():
|
| 164 |
-
print('正在执行一些模块的预热 ...')
|
| 165 |
-
from toolbox import ProxyNetworkActivate
|
| 166 |
-
with ProxyNetworkActivate("Warmup_Modules"):
|
| 167 |
-
import nltk
|
| 168 |
-
with ProxyNetworkActivate("Warmup_Modules"): nltk.download("punkt")
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
if __name__ == '__main__':
|
| 172 |
-
import os
|
| 173 |
-
os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
| 174 |
-
from toolbox import get_conf
|
| 175 |
-
proxies = get_conf('proxies')
|
| 176 |
-
check_proxy(proxies)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|