Gemini translates only a part of the text

Hi, I try to translate a text using gemini-1.5-flash, and from a 99k input file I get only 25k translated. Here is the script:

import os
import google.generativeai as genai

api_key = “”
genai.configure(api_key=api_key)

with open(r’c:\audio\kniha.txt’, ‘r’, encoding=‘utf-8’) as file:
text_to_translate = file.read()

prompt = "translate from english to slovak: " + text_to_translate

model = genai.GenerativeModel(“gemini-1.5-flash”)

response = model.generate_content(prompt)

translated_text = response.text

with open(r’c:\audio\preklad.txt’, ‘w’, encoding=‘utf-8’) as file:
file.write(translated_text)

print(“Saved to ‘preklad.txt’.”)

Thank you for advice.

Welcome to the forum. The API output token limit for Gemini-1.5-flash is (Modele Gemini  |  Gemini API  |  Google AI for Developers) 8192 tokens. Translate that into characters by multiplying by about 3 to 4 and you get the output file size you posted.

To translate a longer text, you would need to first partition it to sizes such that the translated output doesn’t exceed the output token limit. Then you would have to concatenate the partial translations. Since this process requires multiple API calls and (unless you take further steps to add the previously translated parts into the prompt to ensure semantic consistency) potentially worse translation output, consider using a translation API instead of a generative LLM for this task.

Hope that helps.

2 Likes

Thank you for the clarification.