From e96f23ddc84a98985ebfe329b7baeb877c319fe8 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Mon, 11 Sep 2023 21:04:51 +0300 Subject: [PATCH] revised README.md --- README.md | 107 +++++++++++++++++++++++++++++------------------------- server.py | 4 +- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index f4a2f25..fdeea44 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,76 @@ -# AnkiAI +# AnkiAI - Automated Anki Deck Creator -AnkiAI is a robust system that converts images containing text into structured Anki cards using Optical Character Recognition (OCR) and OpenAI's GPT-4 language model. Users can quickly generate decks of flashcards from their images for effective study. +AnkiAI is a tool that leverages OCR (Optical Character Recognition) and GPT-3's powerful natural language processing capabilities to automatically generate Anki decks from images containing text. -## Features -- Converts image content to textual content using OCR. -- Uses OpenAI's GPT-4 model to structure the content into Anki decks and cards. -- Outputs the structured content as an Anki package. +### Overview -## Dependencies -- genanki: Used for creating Anki decks and cards. -- Pillow: Image processing library. -- openai: API library for OpenAI's GPT-4 model. -- flask: Web server to host the service. +- AnkiAI is designed to streamline the process of creating Anki decks from images. +- The core idea is to use OCR to extract text from images and then use GPT-3 to transform this text into a structured Anki deck format. +- Users can make a POST request to a Flask server endpoint with their images to receive the Anki deck (.apkg file). -## Setup and Installation +### Directory Structure + +- `.vscode/`: Contains configuration for VSCode debugger for Flask applications. +- `ankiai.py`: The main script that drives the creation of Anki decks from images. +- `constants.py`: Contains constant variables used across the project. +- `deck_creation.py`: Contains logic for communicating with OpenAI's API and deck creation using genanki. +- `image_processing.py`: Processes images, converting them for OCR and then performing OCR to extract text. +- `logging_config.py`: Logging configuration for the entire project. +- `server.py`: Flask server that provides an API endpoint to upload images and get back an Anki deck. + +### Requirements + +To run AnkiAI, you'll need to have the following dependencies installed: + +``` +genanki==0.8.0 +Pillow +openai +flask +``` + +You can install these via `pip` using the `requirements.txt` file: + +```bash +pip install -r requirements.txt +``` + +### How to Run + +1. **Environment Variables**: Make sure to set the `OPENAI_API_KEY` environment variable to your OpenAI API key. + +2. **Run the Flask server**: -1. Clone this repository: ```bash - git clone https://git.rudefox.io/bj/anki-json2ankicards.git - cd json2ankicards + python server.py ``` -2. Set up a virtual environment and activate it: + This will start the Flask server. You can then make a POST request to `http://localhost:5000/deck-from-images` with your images to get an Anki deck. + +3. **Run Directly**: + + If you prefer not to use the Flask server, you can also run `ankiai.py` directly: + ```bash - python3 -m venv venv - source venv/bin/activate + python ankiai.py ``` -3. Install the required packages: - ```bash - pip install -r requirements.txt - ``` +### How to Debug (VSCode Users) -4. Set up the OpenAI API key: - ```bash - export OPENAI_API_KEY=your_openai_api_key - ``` +- Open the project in VSCode. +- Set up your breakpoints. +- Use the VSCode debugger and select "Python: Flask" to start debugging the Flask server. -5. Run the server: - ```bash - python server.py - ``` +### Important Notes -## Usage +- **API Key**: For the project to work, it is essential to have the `OPENAI_API_KEY` environment variable set. +- **Image Types**: Currently, the image processing module supports PNG, JPG, and JPEG formats. +- **Output**: The output `.apkg` file (Anki package file) will be named `out.apkg`. -1. Start the server as mentioned above. +### Acknowledgements -2. Use a tool like [Postman](https://www.postman.com/) or `curl` to send images to `http://localhost:5000/deck-from-images` as a multi-part POST request. +This project heavily relies on the `openai` library for processing and the `genanki` library for deck generation. -3. The server will respond with a downloadable Anki package. Import this into your Anki app and start studying! +### Contributions -## Modules - -1. **ankiai.py**: The main module that orchestrates the flow. -2. **images2text.py**: Converts image content into text using OCR. -3. **json2deck.py**: Converts structured JSON data into an Anki package. -4. **prompt4cards.py**: Uses OpenAI to structure the content into Anki decks and cards. -5. **server.py**: Flask server to host the service. - -## Contributing - -Contributions are welcome! Please submit a pull request or open an issue to discuss changes or fixes. - -## License - -[MIT License](LICENSE) +Contributions are always welcome. Please create a new issue or a pull request for any bug fixes or feature requests. diff --git a/server.py b/server.py index 8fd3dcc..af87767 100644 --- a/server.py +++ b/server.py @@ -3,16 +3,16 @@ import tempfile import shutil import logging -from logging_config import setup_logging from flask import Flask, request, send_from_directory, jsonify from werkzeug.utils import secure_filename from ankiai import images_to_package from constants import IMAGE_KEY, APKG_FILE, NO_IMAGE_PART_ERROR, NO_SELECTED_FILE_ERROR, INVALID_FILENAME_ERROR + +from logging_config import setup_logging setup_logging() -from logging_config import setup_logging app = Flask(__name__) def save_uploaded_images(images, directory):