I have the following code which uses the google gemini api to process a file called “pdf_texts_79_batch_#.txt.” The “#” in the file name stands for a number that starts at one and works its way up. I need to make it so that I can run the code, then I can change the number in the file name to the onqecutive number, then run the code again, all autonomously. For example, my code would look like this:
import google.generativeai as genai
import os
gemini_key = “ABCDEFG123456789”
genai.configure(api_key=gemini_key)
with open(‘pdf_texts_79_batch_1.txt’, ‘r’, encoding=’utf-8′) as file:
input_text = file.read()
model = genai.GenerativeModel(model_name=”gemini-1.5-pro”)
response = model.generate_content([
“””
Get rid of all instances of the letter “t”.
“”” + input_text
])
with open(‘extract-sections.txt’, ‘w’, encoding=’utf-8′) as output_file:
output_file.write(response.text)
print(“Output has been saved to extract-sections.txt”)
After I run that code once, I would then change ‘pdf_texts_79_batch_1.txt’ to ‘pdf_texts_79_batch_2.txt’ and run the code again. I cannot combine my txt files because of the token limits that the gemini api has.
I have tried combining my txt files but then I run into character limit issues. The only way to get passed the character limits is to pay, but that gets really expensive really fast.
Shriyan Yamali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.