Spaces:
Running
Running
Update bin_public/app/openai_func.py
Browse files- bin_public/app/openai_func.py +24 -14
bin_public/app/openai_func.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import requests
|
2 |
import logging
|
|
|
3 |
from bin_public.config.presets import (
|
4 |
timeout_all,
|
5 |
USAGE_API_URL,
|
@@ -33,32 +34,34 @@ def get_billing_data(openai_api_key, billing_url):
|
|
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 |
-
balance_data=get_billing_data(openai_api_key,
|
41 |
logging.debug(balance_data)
|
42 |
try:
|
43 |
balance = balance_data["total_available"] if balance_data["total_available"] else 0
|
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)
|
46 |
except Exception as e:
|
47 |
-
logging.error(f"API使用情况解析失败:"+str(e))
|
48 |
balance = 0
|
49 |
-
total_used=0
|
50 |
return f"**API使用情况解析失败**"
|
51 |
if balance == 0:
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
55 |
try:
|
56 |
usage_data = get_billing_data(openai_api_key, usage_url)
|
57 |
except Exception as e:
|
58 |
-
logging.error(f"获取API使用情况失败:"+str(e))
|
59 |
return f"**获取API使用情况失败**"
|
60 |
-
|
61 |
-
|
|
|
62 |
# return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
|
63 |
return f"""\
|
64 |
<b>免费额度使用情况</b>
|
@@ -69,7 +72,7 @@ def get_usage(openai_api_key):
|
|
69 |
</div>
|
70 |
<div style="display: flex; justify-content: space-between;"><span>已用 ${total_used}</span><span>可用 ${balance}</span></div>
|
71 |
"""
|
72 |
-
|
73 |
except requests.exceptions.ConnectTimeout:
|
74 |
status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
75 |
return status_text
|
@@ -77,5 +80,12 @@ def get_usage(openai_api_key):
|
|
77 |
status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
|
78 |
return status_text
|
79 |
except Exception as e:
|
80 |
-
logging.error(f"获取API使用情况失败:"+str(e))
|
81 |
-
return standard_error_msg + error_retrieve_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
import logging
|
3 |
+
from bin_public.utils import shared
|
4 |
from bin_public.config.presets import (
|
5 |
timeout_all,
|
6 |
USAGE_API_URL,
|
|
|
34 |
return data
|
35 |
else:
|
36 |
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
|
37 |
+
|
38 |
|
39 |
def get_usage(openai_api_key):
|
40 |
try:
|
41 |
+
balance_data = get_billing_data(openai_api_key, shared.state.balance_api_url)
|
42 |
logging.debug(balance_data)
|
43 |
try:
|
44 |
balance = balance_data["total_available"] if balance_data["total_available"] else 0
|
45 |
total_used = balance_data["total_used"] if balance_data["total_used"] else 0
|
46 |
+
usage_percent = round(total_used / (total_used + balance) * 100, 2) if balance > 0 else 0
|
47 |
except Exception as e:
|
48 |
+
logging.error(f"API使用情况解析失败:" + str(e))
|
49 |
balance = 0
|
50 |
+
total_used = 0
|
51 |
return f"**API使用情况解析失败**"
|
52 |
if balance == 0:
|
53 |
+
curr_time = datetime.datetime.now()
|
54 |
+
last_day_of_month = get_last_day_of_month(curr_time).strftime("%Y-%m-%d")
|
55 |
+
first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d")
|
56 |
+
usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}"
|
57 |
try:
|
58 |
usage_data = get_billing_data(openai_api_key, usage_url)
|
59 |
except Exception as e:
|
60 |
+
logging.error(f"获取API使用情况失败:" + str(e))
|
61 |
return f"**获取API使用情况失败**"
|
62 |
+
usage_rounded = round(usage_data['total_usage'] / 100, 2)
|
63 |
+
return f"**本月使用金额** \u3000 ${usage_rounded}"
|
64 |
+
|
65 |
# return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
|
66 |
return f"""\
|
67 |
<b>免费额度使用情况</b>
|
|
|
72 |
</div>
|
73 |
<div style="display: flex; justify-content: space-between;"><span>已用 ${total_used}</span><span>可用 ${balance}</span></div>
|
74 |
"""
|
75 |
+
|
76 |
except requests.exceptions.ConnectTimeout:
|
77 |
status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
78 |
return status_text
|
|
|
80 |
status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
|
81 |
return status_text
|
82 |
except Exception as e:
|
83 |
+
logging.error(f"获取API使用情况失败:" + str(e))
|
84 |
+
return standard_error_msg + error_retrieve_prompt
|
85 |
+
|
86 |
+
|
87 |
+
def get_last_day_of_month(any_day):
|
88 |
+
# The day 28 exists in every month. 4 days later, it's always next month
|
89 |
+
next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
|
90 |
+
# subtracting the number of the current day brings us back one month
|
91 |
+
return next_month - datetime.timedelta(days=next_month.day)
|