A Python application that automatically generates Japanese flashcards with AI-generated content and images, then uploads them directly to Anki via AnkiConnect.
- AI-Powered Content Generation: Uses OpenRouter API to generate structured Japanese vocabulary data
- Image Generation: Creates custom illustrations for each flashcard using AI
- Anki Integration: Automatically uploads cards and images to Anki via AnkiConnect
- Structured Data: Generates comprehensive card data including kanji, kana, meanings, and example sentences
- Media Management: Properly handles image uploads to Anki's media folder
- Error Handling: Robust error handling and debugging output
- Anki: Install Anki on your system
- AnkiConnect: Install the AnkiConnect add-on in Anki
- Python 3.10+: Ensure you have Python 3.10 or higher
- OpenRouter API Key: Get an API key from OpenRouter
-
Clone the repository:
git clone <repository-url> cd anki-agent
-
Install dependencies:
uv sync # or pip install -r requirements.txt -
Set up environment variables:
# Create .env file echo "OPENROUTER_API_KEY=your_api_key_here" > .env
-
Configure settings (optional): Edit
config.yamlto customize models and settings.
- Start Anki with AnkiConnect enabled
- Run the application:
python main.py
- Enter a Japanese word when prompted
- Watch the magic happen - the app will:
- Generate structured vocabulary data
- Create a custom illustration
- Upload everything to Anki
anki-agent/
βββ main.py # Main application
βββ mobile_main.py # Main application tailored for android
βββ config.yaml # Configuration settings
βββ pyproject.toml # Python project configuration
βββ .python-version # Python version specification
βββ .gitignore # Git ignore rules
βββ outputs/ # Generated files
β βββ *_card.json # Card data files
β βββ *_image.png # Generated images
βββ README.md # This file
OPENROUTER_API_KEY=your_openrouter_api_key_heretext_model: "openai/gpt-5-nano" # Text generation model
image_model: "openai/gpt-5-image-mini" # Image generation model
output_dir: "outputs" # Output directory- Takes a Japanese word/phrase as input
- Uses OpenRouter API to generate structured data:
- Kanji and kana readings
- English meanings
- Example sentences (Japanese and English)
- Image generation prompts
- Creates custom illustrations using AI
- Generates images based on the vocabulary word
- Saves images to the outputs directory
- Uploads images to Anki's media folder using AnkiConnect
- Creates flashcards with embedded images
- Handles duplicate detection and error management
Each generated card includes:
{
"kanji": "ηͺ",
"kana": "γΎγ©",
"english_meaning": "window",
"example_sentence_jp": "ηͺγιγγ¦ι’¨γε
₯γγγ",
"example_sentence_en": "I opened the window to let in a breeze.",
"anki_format": {
"front": "ηͺ (γΎγ©) β window",
"back": "Meaning: window\nReading: γΎγ©\nExample: ηͺγιγγ¦ι’¨γε
₯γγγ / I opened the window to let in a breeze.",
"tags": ["Japanese", "Vocabulary", "Kanji", "N5"]
},
"image_prompt": "A clean, minimal illustration of a Japanese window..."
}- Text Generation: Uses GPT models for content creation
- Image Generation: Uses image models for illustrations
- Structured Output: Ensures consistent JSON format
- Media Upload: Uses
storeMediaFileaction for images - Card Creation: Uses
addNoteaction for flashcards - Error Handling: Comprehensive error checking and reporting
Edit config.yaml to use different AI models:
text_model: "openai/gpt-4o-mini" # Different text model
image_model: "dall-e-3" # Different image modelModify the card structure in main.py to customize:
- Field names and content
- HTML formatting
- Tag assignments
- Image placement
-
"Cannot connect to AnkiConnect"
- Ensure Anki is running
- Verify AnkiConnect add-on is installed and enabled
- Check that port 8765 is accessible
-
"API key not found"
- Verify your
.envfile containsOPENROUTER_API_KEY - Check that the API key is valid and has credits
- Verify your
-
"Image upload failed"
- Check that the image file exists in outputs/
- Verify AnkiConnect is responding to requests
- Check file permissions
-
"Duplicate card error"
- This is normal - the app prevents duplicate cards
- Change
allowDuplicatetoTruein the code if needed
The application includes extensive debugging output:
- Card data structure
- Image upload progress
- AnkiConnect responses
- Error details
# Process multiple words
words = ["η«", "η¬", "ι³₯"]
for word in words:
# Your processing logic here
passModify the system prompt in generate_flashcard_data() to:
- Change the output format
- Add specific requirements
- Customize the language level
- requests: HTTP requests to APIs
- pyyaml: Configuration file parsing
- python-dotenv: Environment variable management
- pathlib: File path handling
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. Feel free to use, modify, and distribute as needed.
For issues and questions:
- Check the troubleshooting section above
- Review AnkiConnect documentation
- Verify your API keys and configuration
- Open an issue on the project repository
- AnkiConnect for seamless Anki integration
- OpenRouter for AI model access
- Anki for the amazing spaced repetition system
Happy studying! πβ¨