generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Feb 24, 2024
1 parent
d1c282d
commit d8b321e
Showing
6 changed files
with
1,098 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "swarms-cloud" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
description = "Swarms Cloud - Pytorch" | ||
license = "MIT" | ||
authors = ["Kye Gomez <[email protected]>"] | ||
|
@@ -26,11 +26,9 @@ python = "^3.9" | |
swarms = "*" | ||
fastapi = "*" | ||
skypilot = "*" | ||
supabase = "*" | ||
|
||
|
||
[tool.poetry.dev-dependencies] | ||
# Add development dependencies here | ||
torch = "*" | ||
einops = "*" | ||
pydantic = "*" | ||
|
||
|
||
[tool.poetry.group.lint.dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from transformers import PreTrainedTokenizer | ||
|
||
|
||
# Function to calculate tokens and pricing | ||
def calculate_pricing(texts, tokenizer: PreTrainedTokenizer, rate_per_million=0.01): | ||
""" | ||
Calculates the pricing for a given list of texts based on the number of tokens, sentences, words, characters, and paragraphs. | ||
Args: | ||
texts (list): A list of texts to calculate pricing for. | ||
tokenizer (PreTrainedTokenizer): A pre-trained tokenizer object used to tokenize the texts. | ||
rate_per_million (float, optional): The rate per million tokens used to calculate the cost. Defaults to 0.01. | ||
Returns: | ||
tuple: A tuple containing the total number of tokens, sentences, words, characters, paragraphs, and the calculated cost. | ||
Example usage: | ||
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2") | ||
>>> texts = ["This is the first example text.", "This is the second example text."] | ||
>>> total_tokens, total_sentences, total_words, total_characters, total_paragraphs, cost = calculate_pricing(texts, tokenizer) | ||
>>> print(f"Total tokens processed: {total_tokens}") | ||
>>> print(f"Total cost: ${cost:.5f}") | ||
""" | ||
total_tokens = 0 | ||
total_sentences = 0 | ||
total_words = 0 | ||
total_characters = 0 | ||
total_paragraphs = 0 | ||
|
||
for text in texts: | ||
# Tokenize the text and count tokens | ||
tokens = tokenizer.encode(text, add_special_tokens=True) | ||
total_tokens += len(tokens) | ||
|
||
# Count sentences | ||
sentences = text.count(".") + text.count("!") + text.count("?") | ||
total_sentences += sentences | ||
|
||
# Count words | ||
words = len(text.split()) | ||
total_words += words | ||
|
||
# Count characters | ||
characters = len(text) | ||
total_characters += characters | ||
|
||
# Count paragraphs | ||
paragraphs = text.count("\n\n") + 1 | ||
total_paragraphs += paragraphs | ||
|
||
# Calculate total cost with high precision | ||
cost = (total_tokens / 1_000_000) * rate_per_million | ||
print(f"Total cost: ${float(cost):.10f}") | ||
|
||
return ( | ||
total_tokens, | ||
total_sentences, | ||
total_words, | ||
total_characters, | ||
total_paragraphs, | ||
cost, | ||
) |
Oops, something went wrong.