Skip to content

Instantly share code, notes, and snippets.

@SaschaHeyer
Last active December 18, 2023 11:55
Show Gist options
  • Save SaschaHeyer/5fa8df56d722caa01cb13e71518c696e to your computer and use it in GitHub Desktop.
Save SaschaHeyer/5fa8df56d722caa01cb13e71518c696e to your computer and use it in GitHub Desktop.
gen-ai-vs-translation-api
import vertexai
from vertexai.language_models import TextGenerationModel
from google.cloud import translate_v2 as translate
vertexai.init(project="sascha-playground-doit", location="us-central1")
parameters = {
"candidate_count": 1,
"max_output_tokens": 1024,
"temperature": 0.9,
"top_p": 1
}
model = TextGenerationModel.from_pretrained("text-bison")
# Text to be translated
input_text = "Meine Name ist Sascha Heyer ich arbeite bei DoiT International"
# Gen AI API
response = model.predict(
f"""You are a professional book/novel translator and you will be given a language and a body of text in German.
Your job is to translate the text into into American English.
You are only allowed to return the translated text and nothing else. 
IMPORTANT: ONLY RETURN TRANSLATED TEXT AND NOTHING ELSE.
Translate the following text:
{input_text}
\"\"\"""",
**parameters
)
output_text = response.text
input_characters = len(input_text)
output_characters = len(output_text)
# Pricing calculation for Gen AI
gen_ai_input_cost = 0.00025 * (input_characters / 1000)
gen_ai_output_cost = 0.00020 * (output_characters / 1000)
total_gen_ai_cost = gen_ai_input_cost + gen_ai_output_cost
# Google Cloud Translation API
translate_client = translate.Client()
translation = translate_client.translate(input_text, target_language='en')
translated_text = translation['translatedText']
translated_characters = len(translated_text)
# Pricing calculation for Cloud Translation API
cloud_translation_cost = 20 * (translated_characters / 1000000)
# Output the results
print(f"Input Text: {input_text}")
print(f"Translated Text (Gen AI): {output_text}")
print(f"Translated Text (Cloud Translation): {translated_text}")
print(f"Total characters (input + output) for Gen AI: {input_characters + output_characters}")
print(f"Total characters for Cloud Translation: {translated_characters}")
print(f"Total cost for Gen AI: ${total_gen_ai_cost:.6f}")
print(f"Total cost for Cloud Translation: ${cloud_translation_cost:.6f}")
# Calculate the percentage difference
if cloud_translation_cost > 0: # Prevent division by zero
percentage_difference = ((cloud_translation_cost - total_gen_ai_cost) / cloud_translation_cost) * 100
else:
percentage_difference = 0
print(f"Gen AI API is {percentage_difference:.2f}% cheaper than the Cloud Translation API.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment