xingfanxia commited on
Commit
48c6f75
·
unverified ·
1 Parent(s): a9516c8

fix: fix division by zero in usage stats and round monthly usage (#496)

Browse files
Files changed (1) hide show
  1. modules/openai_func.py +3 -2
modules/openai_func.py CHANGED
@@ -42,7 +42,7 @@ def get_usage(openai_api_key):
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
@@ -57,7 +57,8 @@ def get_usage(openai_api_key):
57
  except Exception as e:
58
  logging.error(f"获取API使用情况失败:"+str(e))
59
  return f"**获取API使用情况失败**"
60
- return f"**本月使用金额** \u3000 ${usage_data['total_usage'] / 100}"
 
61
 
62
  # return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
63
  return f"""\
 
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) if balance > 0 else 0
46
  except Exception as e:
47
  logging.error(f"API使用情况解析失败:"+str(e))
48
  balance = 0
 
57
  except Exception as e:
58
  logging.error(f"获取API使用情况失败:"+str(e))
59
  return f"**获取API使用情况失败**"
60
+ usage_rounded = round(usage_data['total_usage'] / 100, 2)
61
+ return f"**本月使用金额** \u3000 ${usage_rounded}"
62
 
63
  # return f"**免费额度**(已用/余额)\u3000${total_used} / ${balance}"
64
  return f"""\