- Run the evaluator in a Python environment as a function to evaluate one or many texts at once.
- Determining the text grade level traditionally
- Using Gemini for automated determination of grade level
Requirements
- Gemini 2.5 Pro is the recommended model. Other models have not been tested and may not be as accurate for determining the grade level.
Using the Gemini API to evaluate one text
You can automate the entire process in a Python script or notebook to analyze texts quickly and consistently. This section will guide you through setting up the environment and writing the code to perform the analysis.Step 1: Setting up the environment
- First, follow the instructions in Getting set up to create a Python virtual environment.
- Sign up and receive a Gemini API Key from https://aistudio.google.com/.
- Set up the key as an environment variable GOOGLE_API_KEY for security.
- Install the necessary Python libraries:
- Open a new Jupyter notebook and add an empty code cell.
Step 2: Running your evaluation
- In the code cell, paste the following code snippet, replacing the placeholder lines.
// Add System and User prompts you used earlier, here:
You are the expert…
with the system and user prompts from the previous sections of the Tutorial.
- Now, run the cell by pressing
Ctrl-Enter
. It may take a while for the results to appear. They will look like this:
Step 3: Evaluating multiple texts at once
If you have several texts to analyze, you can save them as rows of a CSV file, then run the evaluator for each row automatically.- Combine your texts into a CSV file, one text in a row, with the escaped quotes if necessary.
- Add the column label text in the first row. You can use the following script to add all the text files in the current directory to a CSV file:
- Save the script above as the file named
texts_to_csv.py
. - Run the above script as:
python3 texts_to_csv.py
in the directory containing the texts you want to evaluate, saved as plain text files (*.txt). - Place your CSV file
texts.csv
in the same directory as your notebook from the step 2 above. - Add another empty code cell to the bottom of the notebook, then add the following code block:
- Run your combined text evaluator by hitting
Ctrl-Enter
. Evaluating a moderately long text can take up to one minute. The total waiting time, in minutes, will approximate the number of texts in the CSV file.