Spaces:
Sleeping
Sleeping
refactor: 精简`request.post`调用、更改余额部分默认文字
Browse files- ChuanhuChatbot.py +1 -1
- modules/chat_func.py +11 -29
- modules/openai_func.py +12 -28
- modules/utils.py +20 -0
ChuanhuChatbot.py
CHANGED
@@ -108,7 +108,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
108 |
visible=not HIDE_MY_KEY,
|
109 |
label="API-Key",
|
110 |
)
|
111 |
-
usageTxt = gr.Markdown(
|
112 |
model_select_dropdown = gr.Dropdown(
|
113 |
label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
|
114 |
)
|
|
|
108 |
visible=not HIDE_MY_KEY,
|
109 |
label="API-Key",
|
110 |
)
|
111 |
+
usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示余额", elem_id="usage_display")
|
112 |
model_select_dropdown = gr.Dropdown(
|
113 |
label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
|
114 |
)
|
modules/chat_func.py
CHANGED
@@ -58,39 +58,21 @@ def get_response(
|
|
58 |
else:
|
59 |
timeout = timeout_all
|
60 |
|
61 |
-
|
62 |
-
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
63 |
-
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
|
64 |
-
|
65 |
-
# 如果存在代理设置,使用它们
|
66 |
-
proxies = {}
|
67 |
-
if http_proxy:
|
68 |
-
logging.info(f"使用 HTTP 代理: {http_proxy}")
|
69 |
-
proxies["http"] = http_proxy
|
70 |
-
if https_proxy:
|
71 |
-
logging.info(f"使用 HTTPS 代理: {https_proxy}")
|
72 |
-
proxies["https"] = https_proxy
|
73 |
|
74 |
# 如果有自定义的api-url,使用自定义url发送请求,否则使用默认设置发送请求
|
75 |
if shared.state.api_url != API_URL:
|
76 |
logging.info(f"使用自定义API URL: {shared.state.api_url}")
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
response = requests.post(
|
88 |
-
shared.state.api_url,
|
89 |
-
headers=headers,
|
90 |
-
json=payload,
|
91 |
-
stream=True,
|
92 |
-
timeout=timeout,
|
93 |
-
)
|
94 |
return response
|
95 |
|
96 |
|
|
|
58 |
else:
|
59 |
timeout = timeout_all
|
60 |
|
61 |
+
proxies = get_proxies()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# 如果有自定义的api-url,使用自定义url发送请求,否则使用默认设置发送请求
|
64 |
if shared.state.api_url != API_URL:
|
65 |
logging.info(f"使用自定义API URL: {shared.state.api_url}")
|
66 |
+
|
67 |
+
response = requests.post(
|
68 |
+
shared.state.api_url,
|
69 |
+
headers=headers,
|
70 |
+
json=payload,
|
71 |
+
stream=True,
|
72 |
+
timeout=timeout,
|
73 |
+
proxies=proxies,
|
74 |
+
)
|
75 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
return response
|
77 |
|
78 |
|
modules/openai_func.py
CHANGED
@@ -2,6 +2,7 @@ import requests
|
|
2 |
import logging
|
3 |
from modules.presets import timeout_all, BALANCE_API_URL,standard_error_msg,connection_timeout_prompt,error_retrieve_prompt,read_timeout_prompt
|
4 |
from modules import shared
|
|
|
5 |
import os
|
6 |
|
7 |
|
@@ -13,39 +14,19 @@ def get_usage_response(openai_api_key):
|
|
13 |
|
14 |
timeout = timeout_all
|
15 |
|
16 |
-
|
17 |
-
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
18 |
-
https_proxy = os.environ.get(
|
19 |
-
"HTTPS_PROXY") or os.environ.get("https_proxy")
|
20 |
-
|
21 |
-
# 如果存在代理设置,使用它们
|
22 |
-
proxies = {}
|
23 |
-
if http_proxy:
|
24 |
-
logging.info(f"使用 HTTP 代理: {http_proxy}")
|
25 |
-
proxies["http"] = http_proxy
|
26 |
-
if https_proxy:
|
27 |
-
logging.info(f"使用 HTTPS 代理: {https_proxy}")
|
28 |
-
proxies["https"] = https_proxy
|
29 |
-
|
30 |
-
# 如果有代理,使用代理发送请求,否则使用默认设置发送请求
|
31 |
"""
|
32 |
暂不支持修改
|
33 |
if shared.state.balance_api_url != BALANCE_API_URL:
|
34 |
logging.info(f"使用自定义BALANCE API URL: {shared.state.balance_api_url}")
|
35 |
"""
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
else:
|
44 |
-
response = requests.get(
|
45 |
-
BALANCE_API_URL,
|
46 |
-
headers=headers,
|
47 |
-
timeout=timeout,
|
48 |
-
)
|
49 |
return response
|
50 |
|
51 |
def get_usage(openai_api_key):
|
@@ -68,3 +49,6 @@ def get_usage(openai_api_key):
|
|
68 |
except requests.exceptions.ReadTimeout:
|
69 |
status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
|
70 |
return status_text
|
|
|
|
|
|
|
|
2 |
import logging
|
3 |
from modules.presets import timeout_all, BALANCE_API_URL,standard_error_msg,connection_timeout_prompt,error_retrieve_prompt,read_timeout_prompt
|
4 |
from modules import shared
|
5 |
+
from modules.utils import get_proxies
|
6 |
import os
|
7 |
|
8 |
|
|
|
14 |
|
15 |
timeout = timeout_all
|
16 |
|
17 |
+
proxies = get_proxies()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"""
|
19 |
暂不支持修改
|
20 |
if shared.state.balance_api_url != BALANCE_API_URL:
|
21 |
logging.info(f"使用自定义BALANCE API URL: {shared.state.balance_api_url}")
|
22 |
"""
|
23 |
+
response = requests.get(
|
24 |
+
BALANCE_API_URL,
|
25 |
+
headers=headers,
|
26 |
+
timeout=timeout,
|
27 |
+
proxies=proxies,
|
28 |
+
)
|
29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return response
|
31 |
|
32 |
def get_usage(openai_api_key):
|
|
|
49 |
except requests.exceptions.ReadTimeout:
|
50 |
status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
|
51 |
return status_text
|
52 |
+
except Exception as e:
|
53 |
+
logging.error(f"获取API使用情况失败:"+str(e))
|
54 |
+
return standard_error_msg + error_retrieve_prompt
|
modules/utils.py
CHANGED
@@ -437,3 +437,23 @@ def transfer_input(inputs):
|
|
437 |
gr.Button.update(visible=False),
|
438 |
gr.Button.update(visible=True),
|
439 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
gr.Button.update(visible=False),
|
438 |
gr.Button.update(visible=True),
|
439 |
)
|
440 |
+
|
441 |
+
|
442 |
+
def get_proxies():
|
443 |
+
# 获取环境变量中的代理设置
|
444 |
+
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
445 |
+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
|
446 |
+
|
447 |
+
# 如果存在代理设置,使用它们
|
448 |
+
proxies = {}
|
449 |
+
if http_proxy:
|
450 |
+
logging.info(f"使用 HTTP 代理: {http_proxy}")
|
451 |
+
proxies["http"] = http_proxy
|
452 |
+
if https_proxy:
|
453 |
+
logging.info(f"使用 HTTPS 代理: {https_proxy}")
|
454 |
+
proxies["https"] = https_proxy
|
455 |
+
|
456 |
+
if proxies == {}:
|
457 |
+
proxies = None
|
458 |
+
|
459 |
+
return proxies
|