Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
94f0d7d
1
Parent(s):
18f19f4
去除免费额度显示功能(由于OpenAI API的变化,不再可用啦),只显示当月用量
Browse files- modules/openai_func.py +12 -36
modules/openai_func.py
CHANGED
|
@@ -19,7 +19,7 @@ def get_billing_data(openai_api_key, billing_url):
|
|
| 19 |
"Content-Type": "application/json",
|
| 20 |
"Authorization": f"Bearer {openai_api_key}"
|
| 21 |
}
|
| 22 |
-
|
| 23 |
timeout = timeout_all
|
| 24 |
with retrieve_proxy():
|
| 25 |
response = requests.get(
|
|
@@ -27,51 +27,27 @@ def get_billing_data(openai_api_key, billing_url):
|
|
| 27 |
headers=headers,
|
| 28 |
timeout=timeout,
|
| 29 |
)
|
| 30 |
-
|
| 31 |
if response.status_code == 200:
|
| 32 |
data = response.json()
|
| 33 |
return data
|
| 34 |
else:
|
| 35 |
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
|
| 36 |
-
|
| 37 |
|
| 38 |
def get_usage(openai_api_key):
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
total_used = balance_data["total_used"] if balance_data["total_used"] else 0
|
| 45 |
-
usage_percent = round(total_used / (total_used+balance) * 100, 2) if balance > 0 else 0
|
| 46 |
except Exception as e:
|
| 47 |
-
logging.error(f"API
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if balance == 0:
|
| 52 |
-
curr_time = datetime.datetime.now()
|
| 53 |
-
last_day_of_month = get_last_day_of_month(curr_time).strftime("%Y-%m-%d")
|
| 54 |
-
first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d")
|
| 55 |
-
usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}"
|
| 56 |
-
try:
|
| 57 |
-
usage_data = get_billing_data(openai_api_key, usage_url)
|
| 58 |
-
except Exception as e:
|
| 59 |
-
logging.error(f"获取API使用情况失败:"+str(e))
|
| 60 |
-
return f"**获取API使用情况失败**"
|
| 61 |
-
usage_rounded = round(usage_data['total_usage'] / 100, 2)
|
| 62 |
-
return f"**本月使用金额** \u3000 ${usage_rounded}"
|
| 63 |
-
|
| 64 |
-
# return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
|
| 65 |
-
return f"""\
|
| 66 |
-
<b>免费额度使用情况</b>
|
| 67 |
-
<div class="progress-bar">
|
| 68 |
-
<div class="progress" style="width: {usage_percent}%;">
|
| 69 |
-
<span class="progress-text">{usage_percent}%</span>
|
| 70 |
-
</div>
|
| 71 |
-
</div>
|
| 72 |
-
<div style="display: flex; justify-content: space-between;"><span>已用 ${total_used}</span><span>可用 ${balance}</span></div>
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
except requests.exceptions.ConnectTimeout:
|
| 76 |
status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
| 77 |
return status_text
|
|
|
|
| 19 |
"Content-Type": "application/json",
|
| 20 |
"Authorization": f"Bearer {openai_api_key}"
|
| 21 |
}
|
| 22 |
+
|
| 23 |
timeout = timeout_all
|
| 24 |
with retrieve_proxy():
|
| 25 |
response = requests.get(
|
|
|
|
| 27 |
headers=headers,
|
| 28 |
timeout=timeout,
|
| 29 |
)
|
| 30 |
+
|
| 31 |
if response.status_code == 200:
|
| 32 |
data = response.json()
|
| 33 |
return data
|
| 34 |
else:
|
| 35 |
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
|
| 36 |
+
|
| 37 |
|
| 38 |
def get_usage(openai_api_key):
|
| 39 |
try:
|
| 40 |
+
curr_time = datetime.datetime.now()
|
| 41 |
+
last_day_of_month = get_last_day_of_month(curr_time).strftime("%Y-%m-%d")
|
| 42 |
+
first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d")
|
| 43 |
+
usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}"
|
| 44 |
try:
|
| 45 |
+
usage_data = get_billing_data(openai_api_key, usage_url)
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
+
logging.error(f"获取API使用情况失败:"+str(e))
|
| 48 |
+
return f"**获取API使用情况失败**"
|
| 49 |
+
usage_rounded = round(usage_data['total_usage'] / 100, 2)
|
| 50 |
+
return f"**本月使用金额** \u3000 ${usage_rounded}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except requests.exceptions.ConnectTimeout:
|
| 52 |
status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
| 53 |
return status_text
|