~cytrogen/evi-run

ref: 894e1b53802870053338599159dd3092f1ddd403 evi-run/bot/utils/calculate_tokens.py -rw-r--r-- 928 bytes
894e1b53 — Bendy docs: clarify Memory Management description 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from database.models import User
from database.repositories.user import UserRepository

from config import TYPE_USAGE, CREDITS_INPUT_TEXT, CREDITS_OUTPUT_TEXT, CREDITS_INPUT_IMAGE, CREDITS_OUTPUT_IMAGE


async def calculate_tokens(user_repo: UserRepository, user: User,
                           input_tokens_text: int, output_tokens_text: int,
                           input_tokens_img: int, output_tokens_img: int):
    if TYPE_USAGE != 'private':
        credits_input_text = (input_tokens_text / 1000) * CREDITS_INPUT_TEXT
        credits_output_text = (output_tokens_text / 1000) * CREDITS_OUTPUT_TEXT

        credits_input_img = (input_tokens_img / 1000) * CREDITS_INPUT_IMAGE
        credits_output_img = (output_tokens_img / 1000) * CREDITS_OUTPUT_IMAGE

        credits = credits_input_text + credits_output_text + credits_input_img + credits_output_img
        await user_repo.update(user, balance_credits=credits)