smgc commited on
Commit
fd1af17
1 Parent(s): b9c5d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -81,17 +81,20 @@ def calculate_tokens_via_tiktoken(text, model="gpt-3.5-turbo"):
81
  使用 tiktoken 库根据 GPT 模型计算 token 数量。
82
  Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
83
  """
84
- # 临时禁用代理
85
- original_proxies = requests.Session().proxies # 保存原始代理配置
86
- requests.Session().proxies = {} # 禁用代理
87
 
88
  try:
89
  encoding = tiktoken.encoding_for_model(model) # 获取模型的编码器
90
  tokens = encoding.encode(text) # 对文本进行 tokenization
91
  return len(tokens)
92
  finally:
93
- # 恢复代理设置
94
- requests.Session().proxies = original_proxies
 
 
 
95
 
96
  @app.route('/')
97
  def root():
 
81
  使用 tiktoken 库根据 GPT 模型计算 token 数量。
82
  Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
83
  """
84
+ # 临时禁用代理环境变量
85
+ original_http_proxy = os.environ.pop('http_proxy', None)
86
+ original_https_proxy = os.environ.pop('https_proxy', None)
87
 
88
  try:
89
  encoding = tiktoken.encoding_for_model(model) # 获取模型的编码器
90
  tokens = encoding.encode(text) # 对文本进行 tokenization
91
  return len(tokens)
92
  finally:
93
+ # 恢复代理
94
+ if original_http_proxy:
95
+ os.environ['http_proxy'] = original_http_proxy
96
+ if original_https_proxy:
97
+ os.environ['https_proxy'] = original_https_proxy
98
 
99
  @app.route('/')
100
  def root():