A photo sharing social networking app built with Python and Flask, enhanced with machine learning capabilities for automatic alternative text generation and object-based image search. The example application for the book Python Web Development with Flask (2nd edition) (《Flask Web 开发实战(第 2 版)》).
Demo: http://moments.helloflask.com
This enhanced version includes:
-
Automatic Alternative Text Generation: Uses BLIP (Bootstrapping Language-Image Pre-training) model to automatically generate descriptive alternative text for uploaded images, improving accessibility for screen readers and search engines.
-
Object-Based Image Search: Uses YOLOS (You Only Look at One Sequence) model to detect objects in images, enabling users to search for photos by the objects they contain (e.g., "dog", "car", "person").
Clone the repo:
$ git clone [https://github.com/greyli/moments](https://github.com/omsali/Albumy)
$ cd moments
Install dependencies with pip:
$ pip install -r requirements.txtInstall dependencies with PDM:
$ pdm install
Tip
If you don't have PDM installed, you can create a virtual environment with venv and install dependencies with pip install -r requirements.txt.
To initialize the app, run the following commands:
# Initialize the database
$ python -c "from moments import create_app; app = create_app('development'); app.app_context().push(); from moments.core.extensions import db; db.create_all(); print('Database initialized')"
# Run database migration (adds ML fields)
$ python migrate_db.pyIf you just want to try it out, generate fake data with flask lorem command then run the app:
$ flask lorem
It will create a test account:
- email:
admin@helloflask.com - password:
moments
Now you can run the app:
$ flask run
* Running on http://127.0.0.1:5000/
The ML features require additional packages that are included in the requirements.txt:
torch: PyTorch for deep learning modelstransformers: Hugging Face transformers library for pre-trained modelspillow: Image processingrequests: HTTP requests for model downloads
Note: On first run, the models will be downloaded automatically. This may take several minutes depending on your internet connection. The models are cached locally for subsequent runs.
When you upload a photo, the system automatically:
- Analyzes the image using the BLIP model
- Generates descriptive alternative text
- Stores the text in the database
- Includes it in the HTML
altattribute for accessibility - Auto-populates the photo description if you don't provide one
Smart Description Logic:
- If you provide a description during upload → Your description is used
- If you leave the description empty → AI-generated description is automatically added
- This ensures every photo has a meaningful description for better user experience
You can search for photos by objects they contain:
- Go to the search page
- Select "Objects (ML)" tab
- Enter keywords like "dog", "car", "person", etc.
- The system will find photos containing those objects
- Caption Model: Salesforce/blip-image-captioning-base
- Object Detection Model: hustvl/yolos-tiny
- Database: SQLite with additional fields for ML data
- Search: Enhanced search functionality using both text and object detection
- Added ML-powered alternative text generation for uploaded images
- Added object detection to enable object-based image search
- Persisted ML outputs on
Photoasalt_textanddetected_objects - Updated templates to include meaningful
altattributes - Implemented smart description fallback: if no user description, use generated alt text
- Added a migration helper to add the two new columns
moments/ml_services.py: NewMLImageAnalyzerservice that loads BLIP and YOLOS models and exposes:generate_alt_text(image_path)detect_objects(image_path)
moments/models.py(Photo): Added fieldsalt_text: String(500)detected_objects: Text(JSON string)- Helper methods for parsing and keyword extraction
- Registered with Whooshee to index
description,alt_text,detected_objects
moments/blueprints/main.py:- Upload flow: after saving images, run ML analysis, store
alt_textanddetected_objects - If
descriptionis empty, set it to generatedalt_text - New route
/search/objectsto search by ML-derived fields
- Upload flow: after saving images, run ML analysis, store
- Templates:
templates/main/photo.html,templates/main/index.html,templates/macros.html: ensure<img alt="...">usesphoto.alt_textor a fallbacktemplates/main/search.html: add "Objects (ML)" tab and render results
migrate_db.py: Addsalt_textanddetected_objectscolumns if missingrequirements.txt: Added ML deps:torch,transformers,pillow,requests
- User uploads an image
- App writes original and resized images to
MOMENTS_UPLOAD_PATH - A
Photorow is created - ML service:
- Generates a descriptive caption (stored as
alt_text) - Detects objects and confidences (stored as JSON in
detected_objects)
- Generates a descriptive caption (stored as
- If the user did not provide a description, the generated
alt_textbecomes thedescription - Templates render accessible
<img alt>and search can leverage ML outputs
- If uploads work but you see blank alt text on the very first request, wait until the model finishes its initial download and retry the upload
This project is licensed under the MIT License (see the LICENSE file for details).