mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-22 07:04:59 +01:00
added tts generation support
This commit is contained in:
@@ -56,6 +56,8 @@ class UsageTracker:
|
||||
if os.path.isfile(self.user_file):
|
||||
with open(self.user_file, "r") as file:
|
||||
self.usage = json.load(file)
|
||||
if 'tts_characters' not in self.usage['usage_history']:
|
||||
self.usage['usage_history']['tts_characters'] = {}
|
||||
else:
|
||||
# ensure directory exists
|
||||
pathlib.Path(logs_dir).mkdir(exist_ok=True)
|
||||
@@ -63,7 +65,7 @@ class UsageTracker:
|
||||
self.usage = {
|
||||
"user_name": user_name,
|
||||
"current_cost": {"day": 0.0, "month": 0.0, "all_time": 0.0, "last_update": str(date.today())},
|
||||
"usage_history": {"chat_tokens": {}, "transcription_seconds": {}, "number_images": {}}
|
||||
"usage_history": {"chat_tokens": {}, "transcription_seconds": {}, "number_images": {}, "tts_characters": {}}
|
||||
}
|
||||
|
||||
# token usage functions:
|
||||
@@ -151,6 +153,57 @@ class UsageTracker:
|
||||
usage_month += sum(images)
|
||||
return usage_day, usage_month
|
||||
|
||||
# tts usage functions:
|
||||
|
||||
def add_tts_request(self, text_length, tts_model, tts_prices):
|
||||
tts_models = ['tts-1', 'tts-1-hd']
|
||||
price = tts_prices[tts_models.index(tts_model)]
|
||||
today = date.today()
|
||||
tts_price = round(text_length * price / 1000, 2)
|
||||
self.add_current_costs(tts_price)
|
||||
|
||||
if 'tts_characters' not in self.usage['usage_history']:
|
||||
self.usage['usage_history']['tts_characters'] = {}
|
||||
|
||||
if tts_model not in self.usage['usage_history']['tts_characters']:
|
||||
self.usage['usage_history']['tts_characters'][tts_model] = {}
|
||||
|
||||
# update usage_history
|
||||
if str(today) in self.usage["usage_history"]["tts_characters"][tts_model]:
|
||||
# add requested text length to existing date
|
||||
self.usage["usage_history"]["tts_characters"][tts_model][str(today)] += text_length
|
||||
else:
|
||||
# create new entry for current date
|
||||
self.usage["usage_history"]["tts_characters"][tts_model][str(today)] = text_length
|
||||
|
||||
# write updated token usage to user file
|
||||
with open(self.user_file, "w") as outfile:
|
||||
json.dump(self.usage, outfile)
|
||||
|
||||
def get_current_tts_usage(self):
|
||||
"""Get length of speech generated for today and this month.
|
||||
|
||||
:return: total amount of characters converted to speech per day and per month
|
||||
"""
|
||||
|
||||
tts_models = ['tts-1', 'tts-1-hd']
|
||||
today = date.today()
|
||||
characters_day = 0
|
||||
for tts_model in tts_models:
|
||||
if tts_model in self.usage["usage_history"]["tts_characters"] and \
|
||||
str(today) in self.usage["usage_history"]["tts_characters"][tts_model]:
|
||||
characters_day += self.usage["usage_history"]["tts_characters"][tts_model][str(today)]
|
||||
|
||||
month = str(today)[:7] # year-month as string
|
||||
characters_month = 0
|
||||
for tts_model in tts_models:
|
||||
if tts_model in self.usage["usage_history"]["tts_characters"]:
|
||||
for today, characters in self.usage["usage_history"]["tts_characters"][tts_model].items():
|
||||
if today.startswith(month):
|
||||
characters_month += characters
|
||||
return int(characters_day), int(characters_month)
|
||||
|
||||
|
||||
# transcription usage functions:
|
||||
|
||||
def add_transcription_seconds(self, seconds, minute_price=0.006):
|
||||
@@ -236,13 +289,14 @@ class UsageTracker:
|
||||
cost_all_time = self.usage["current_cost"].get("all_time", self.initialize_all_time_cost())
|
||||
return {"cost_today": cost_day, "cost_month": cost_month, "cost_all_time": cost_all_time}
|
||||
|
||||
def initialize_all_time_cost(self, tokens_price=0.002, image_prices="0.016,0.018,0.02", minute_price=0.006):
|
||||
def initialize_all_time_cost(self, tokens_price=0.002, image_prices="0.016,0.018,0.02", minute_price=0.006, tts_prices='0.015,0.030'):
|
||||
"""Get total USD amount of all requests in history
|
||||
|
||||
:param tokens_price: price per 1000 tokens, defaults to 0.002
|
||||
:param image_prices: prices for images of sizes ["256x256", "512x512", "1024x1024"],
|
||||
defaults to [0.016, 0.018, 0.02]
|
||||
:param minute_price: price per minute transcription, defaults to 0.006
|
||||
:param character_price: price per character tts per model ['tts-1', 'tts-1-hd'], defaults to [0.015, 0.030]
|
||||
:return: total cost of all requests
|
||||
"""
|
||||
total_tokens = sum(self.usage['usage_history']['chat_tokens'].values())
|
||||
@@ -255,5 +309,9 @@ class UsageTracker:
|
||||
total_transcription_seconds = sum(self.usage['usage_history']['transcription_seconds'].values())
|
||||
transcription_cost = round(total_transcription_seconds * minute_price / 60, 2)
|
||||
|
||||
all_time_cost = token_cost + transcription_cost + image_cost
|
||||
total_characters = [sum(tts_model.values()) for tts_model in self.usage['usage_history']['tts_characters'].values()]
|
||||
tts_prices_list = [float(x) for x in tts_prices.split(',')]
|
||||
tts_cost = round(sum([count * price / 1000 for count, price in zip(total_characters, tts_prices_list)]), 2)
|
||||
|
||||
all_time_cost = token_cost + transcription_cost + image_cost + tts_cost
|
||||
return all_time_cost
|
||||
|
||||
Reference in New Issue
Block a user