lit-comp

Literary Companion

Literary Companion is a web application designed to enrich the classic reading experience. As you read a novel, it provides a modern English translation and generates contextually relevant “fun facts” and trivia in a side panel, bringing the story’s world to life.

Built with Python, Flask, and the Google Agent Development Kit (ADK), this project leverages the power of generative AI (Vertex AI) to create a dynamic and interactive companion for your literary journeys.

Features

Architecture Overview

The application operates using two primary workflows, both orchestrated by AI agents built with the Google ADK.

Architecture Diagram

1. Book Preparation (A One-Time Process)

Before a novel can be read in the UI, it must be prepared. This is a batch process that you run once for each book.

2. Fun Fact Generation (The Interactive Experience)

This is the core interactive loop that happens as a user reads the book.

Key Technologies

Getting Started

Follow these steps to get the Literary Companion running on your local machine.

Prerequisites

Installation

  1. Clone the repository:
    git clone <your-repo-url>
    cd lit-comp
    
  2. Set up a virtual environment:
    python -m venv .venv
    source .venv/bin/activate
    
  3. Install dependencies:
    pip install -r requirements.txt
    
  4. Configure Environment Variables: Create a .env file in the project root and add the necessary configuration. This file is loaded by the Flask application for local development.

    # .env
    
    # --- GCP Configuration ---
    GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
    GOOGLE_CLOUD_LOCATION="your-gcp-region" # e.g., us-central1
    
    # --- Application Specific ---
    GCS_BUCKET_NAME="your-gcs-bucket-name"
    GCS_FILE_NAME="name-of-your-book.txt" # The book to load by default
    
    # --- AI Model Configuration ---
    DEFAULT_AGENT_MODEL="gemini-1.5-flash-001"
    GOOGLE_GENAI_USE_VERTEXAI=TRUE
    

Usage

  1. Upload a Book: Upload a plain text (.txt) version of a novel to your GCS bucket.

  2. Prepare the Book: Run the book preparation script, pointing it to the file you just uploaded.
    python scripts/run_book_preparation.py --bucket "your-gcs-bucket-name" --file "name-of-your-book.txt"
    

    This will create a _prepared.json file in your bucket.

  3. Run the Web Application: Start the Flask development server.
    python app.py
    
  4. Open in Browser: Navigate to http://127.0.0.1:5001 to start reading.

Utility Scripts

Filtering a Prepared Book

After preparing a book, you may want to create a smaller version for testing or demos. The scripts/filter_prepared_book.py script allows you to do this.

How to Contribute

We welcome contributions! Here are a few ideas to get you started:

  1. Immersive Audio Experience: Add a “Listen” button that generates and plays short, relevant audio clips for the current scene. This could involve creating a new tool that uses a text-to-audio generation model to create background soundscapes (e.g., a bustling 19th-century London street, the creaking of a ship at sea) or sound effects for key actions.

  2. Multi-Format Book Support: Currently, the application only processes .txt files. A great enhancement would be to extend the book_processor_tool to handle popular ebook formats like .epub and .pdf. This would require adding libraries like ebooklib and PyPDF2 to parse these files and extract their text content before passing it to the translation and analysis pipeline.

  3. Persistent User Sessions with Redis: The current InMemorySessionService loses user history when the app restarts, which is not ideal for a production environment. A valuable contribution would be to replace it with a persistent session store using Redis. This would involve creating a RedisSessionService class that implements the BaseSessionService interface, connecting to a Redis instance, and handling the serialization/deserialization of session data.

License

This project is licensed under the Apache 2.0 License.