Keldos commited on
Commit
ec10bf7
1 Parent(s): c8fd9d2

feat: 将赠金usage显示为进度条

Browse files

- 同时为 usage_display 添加了和上下框架一样的 block 样式

Files changed (2) hide show
  1. assets/custom.css +35 -5
  2. modules/openai_func.py +12 -2
assets/custom.css CHANGED
@@ -37,15 +37,45 @@ footer {
37
 
38
  /* usage_display */
39
  #usage_display {
40
- height: 2em;
 
 
 
 
 
 
 
 
 
41
  }
42
-
43
- #usage_display p{
44
- padding: 0 1em;
45
  font-size: .85em;
46
- font-family: monospace;
47
  color: var(--body-text-color-subdued);
48
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  /* list */
50
  ol:not(.options), ul:not(.options) {
51
  padding-inline-start: 2em !important;
 
37
 
38
  /* usage_display */
39
  #usage_display {
40
+ position: relative;
41
+ margin: 0;
42
+ box-shadow: var(--block-shadow);
43
+ border-width: var(--block-border-width);
44
+ border-color: var(--block-border-color);
45
+ border-radius: var(--block-radius);
46
+ background: var(--block-background-fill);
47
+ width: 100%;
48
+ line-height: var(--line-sm);
49
+ min-height: 2em;
50
  }
51
+ #usage_display p, #usage_display span {
52
+ margin: 0;
53
+ padding: .5em 1em;
54
  font-size: .85em;
 
55
  color: var(--body-text-color-subdued);
56
  }
57
+ .progress-bar {
58
+ background-color: var(--input-background-fill);;
59
+ margin: 0 1em;
60
+ height: 20px;
61
+ border-radius: 10px;
62
+ overflow: hidden;
63
+ }
64
+ .progress {
65
+ background-color: var(--block-title-background-fill);;
66
+ height: 100%;
67
+ border-radius: 10px;
68
+ text-align: right;
69
+ transition: width 0.5s ease-in-out;
70
+ }
71
+ .progress-text {
72
+ /* color: white; */
73
+ color: var(--color-accent) !important;
74
+ font-size: 1em !important;
75
+ font-weight: bold;
76
+ padding-right: 10px;
77
+ line-height: 20px;
78
+ }
79
  /* list */
80
  ol:not(.options), ul:not(.options) {
81
  padding-inline-start: 2em !important;
modules/openai_func.py CHANGED
@@ -43,12 +43,12 @@ def get_usage(openai_api_key):
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
  except Exception as e:
47
  logging.error(f"API使用情况解析失败:"+str(e))
48
  balance = 0
49
  total_used=0
50
  return f"**API使用情况解析失败**"
51
-
52
  if balance == 0:
53
  last_day_of_month = datetime.datetime.now().strftime("%Y-%m-%d")
54
  first_day_of_month = datetime.datetime.now().replace(day=1).strftime("%Y-%m-%d")
@@ -60,7 +60,17 @@ def get_usage(openai_api_key):
60
  return f"**获取API使用情况失败**"
61
  return f"**本月使用金额** \u3000 ${usage_data['total_usage'] / 100}"
62
 
63
- return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
 
 
 
 
 
 
 
 
 
 
64
  except requests.exceptions.ConnectTimeout:
65
  status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
66
  return status_text
 
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)
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
  last_day_of_month = datetime.datetime.now().strftime("%Y-%m-%d")
54
  first_day_of_month = datetime.datetime.now().replace(day=1).strftime("%Y-%m-%d")
 
60
  return f"**获取API使用情况失败**"
61
  return f"**本月使用金额** \u3000 ${usage_data['total_usage'] / 100}"
62
 
63
+ # return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
64
+ return f"""\
65
+ <b>免费额度使用情况</b>
66
+ <div class="progress-bar">
67
+ <div class="progress" style="width: {usage_percent}%;">
68
+ <span class="progress-text">{usage_percent}%</span>
69
+ </div>
70
+ </div>
71
+ <div style="display: flex; justify-content: space-between;"><span>已用 ${total_used}</span><span>可用 ${balance}</span></div>
72
+ """
73
+
74
  except requests.exceptions.ConnectTimeout:
75
  status_text = standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
76
  return status_text