Friday, July 18, 2025

Using AI to retrieve genre and synopsis from book titles

I have several thousand epub files. They are named in such a way that there is usually the author and title. I can use the epub-metadata library in python to get the title. I’d like to be able to use AI and google to get the genre and synopsys. I have the Google One Pro edition which gives me the Gemini Pro but you could also use it’s free version. You need to go to the Google AIStudio to get your API key. Create an environment variable GOOGLE_API_KEY with your key and the genai library will automatically find it. Otherwise you can pass the API key directly when you create the instance of the client. See Using Gemini API keys for examples. I then created a tiny python script as a proof of concept.

Title: the emperors new mind

('Genre: Science, Physics, Mathematics, Computer Science, Philosophy of Mind, ' 'Artificial Intelligence\n' 'Synopsys: Roger Penrose, a renowned physicist and mathematician, presents a ' 'controversial argument that human consciousness cannot be fully explained by ' 'classical computation or algorithms, directly challenging the foundations of ' "'strong AI.' Drawing upon concepts from quantum mechanics, general " "relativity, and Gödel's incompleteness theorems, he proposes that " 'non-computable processes are essential to understanding the mind, suggesting ' 'a deeper connection between consciousness and the fundamental laws of ' 'physics.')

"""
Google Books API Integration Script

This script uses Google's Gemini AI API to retrieve genre and synopsis information
for books based on their titles. It constructs a query using the book title and
the Google Books URL format, then uses the Gemini AI model to generate a response
with genre and synopsis information.

Requirements:
- Google Gemini API key set as environment variable 'GEMINI_API_KEY'
- google-generativeai package installed

Usage:
    python test_google_book_api.py
    (The script will prompt for a book title)
"""

import pprint  # For pretty-printing the API response
from google import genai  # Google's Generative AI library

# The client gets the API key from the environment variable `GEMINI_API_KEY`.
# Google Books URL format: https://www.google.ca/books/edition/title


def aibook(title):
    """
    Query the Gemini AI model for book genre and synopsis information.
    
    This function creates a Gemini AI client, constructs a prompt with the book title
    and desired output format, then returns the generated text response.
    
    Args:
        title (str): The title of the book to query
        
    Returns:
        str: The formatted response text containing genre and synopsis information
        
    Example output format:
        Genre: Fiction, Science Fiction, Dystopian
        Synopsis: A story about...
    """
    # Initialize the Gemini AI client
    client = genai.Client()
    
    # Generate content using the Gemini model with a formatted prompt
    response = client.models.generate_content(
        model="gemini-2.5-flash",  # Using the Gemini 2.5 Flash model for fast responses
        contents=f"""
        book url https://www.google.ca/books/edition/{title}/
        provide the genre and synopsis of the book
        use the format 
        line 1 Genre: genre1, genre2, etc
        line 2 Synopsys: the synopsys     
        """
    )
    
    # Return the text portion of the response
    return response.text


# Execute only if run as a script, not when imported as a module
if __name__ == "__main__":
    # Prompt the user for a book title, pass it to the aibook function,
    # and pretty-print the results
    pprint.pp(aibook(input('Title: ')))

No comments:

Post a Comment