Spaces:
Running
Running
Update bin_public/app/openai_func.py
Browse files
bin_public/app/openai_func.py
CHANGED
@@ -50,7 +50,7 @@ def get_usage(openai_api_key):
|
|
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}"
|
@@ -60,7 +60,9 @@ def get_usage(openai_api_key):
|
|
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"""\
|
@@ -81,6 +83,8 @@ def get_usage(openai_api_key):
|
|
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 |
|
@@ -88,4 +92,17 @@ 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}"
|
|
|
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 |
+
logging.info("余额为0,获取本月使用情况")
|
65 |
+
return get_monthly_usage(openai_api_key)
|
66 |
|
67 |
# return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
|
68 |
return f"""\
|
|
|
83 |
return status_text
|
84 |
except Exception as e:
|
85 |
logging.error(f"获取API使用情况失败:" + str(e))
|
86 |
+
if "Your request to GET /dashboard/billing/credit_grants must be made with a session key" in str(e):
|
87 |
+
return get_monthly_usage(openai_api_key)
|
88 |
return standard_error_msg + error_retrieve_prompt
|
89 |
|
90 |
|
|
|
92 |
# The day 28 exists in every month. 4 days later, it's always next month
|
93 |
next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
|
94 |
# subtracting the number of the current day brings us back one month
|
95 |
+
return next_month - datetime.timedelta(days=next_month.day)
|
96 |
+
|
97 |
+
def get_monthly_usage(openai_api_key):
|
98 |
+
curr_time = datetime.datetime.now()
|
99 |
+
last_day_of_month = get_last_day_of_month(curr_time).strftime("%Y-%m-%d")
|
100 |
+
first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d")
|
101 |
+
usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}"
|
102 |
+
try:
|
103 |
+
usage_data = get_billing_data(openai_api_key, usage_url)
|
104 |
+
except Exception as e:
|
105 |
+
logging.error(f"获取API使用情况失败:"+str(e))
|
106 |
+
return f"**获取API使用情况失败**"
|
107 |
+
usage_rounded = round(usage_data['total_usage'] / 100, 2)
|
108 |
+
return f"**本月使用金额** \u3000 ${usage_rounded}"
|