app.py Reference Documentation
This document provides a comprehensive reference for all methods, variables, and configurations in app.py (485 lines). It is designed for agent systems to understand and interact with the e621tagger Flask application.
File Overview
| Attribute |
Value |
| Location |
app.py |
| Lines |
485 |
| Framework |
Flask |
| Purpose |
Web application for e621 image tagging |
| Dependencies |
flask, flask-limiter, torch, PIL, ua-parser |
File Structure Flow
1. Global Constants
Tag Categories
| Usage |
Map tag category IDs to e621 category names |
Application Configuration
| Variable |
Default |
Description |
APP_VERSION |
test |
Version string (set by Docker build) |
LOG_LEVEL |
DEBUG/test, INFO/prod |
Logging level based on version |
Flask Application
| Config |
Value |
Description |
MAX_CONTENT_LENGTH |
20MB |
Maximum upload file size |
Rate Limiter
| Limit |
Scope |
| 500/hour |
All endpoints |
| 20/minute |
/predict (decorated) |
Model Configuration
| Variable |
Default |
Description |
MODEL_PATH |
models/jtp-3-hydra.safetensors |
Model file location |
TAGS_PATH |
data/jtp-3-hydra-tags.csv |
Tag metadata CSV |
DEVICE |
cuda/cpu |
PyTorch device |
MAX_SEQ_LEN |
1024 |
Max patches for input |
PATCH_SIZE |
16 |
Patch size (pixels) |
Prediction Settings
| Variable |
Value |
Description |
DEFAULT_TOP_K |
200 |
Default tag count |
ALLOWED_TOP_K |
{50,75,100,150,200,300} |
Valid top_k values |
File Handling
| Variable |
Default |
Description |
SAVE_UPLOADS |
false |
Save uploads to disk |
UPLOAD_DIR |
/app/uploads |
Upload save directory |
ALLOWED_EXTENSIONS |
7 formats |
Valid file extensions |
ALLOWED_MIME_TYPES |
7 MIME types |
Valid MIME types |
2. Helper Functions
secure_log(s: str) -> str
| Location |
app.py:67-72 |
| Input |
s: str - Raw string |
| Output |
str - Sanitized string |
| Purpose |
Remove control characters and newlines from log strings |
status_emoji(status_code: int) -> str
| Location |
app.py:74-80 |
| Input |
status_code: int - HTTP status code |
| Output |
str - Emoji (🟢/🟡/🔴) |
| Purpose |
Get status emoji for logging |
| Status Range |
Emoji |
| 200-299 |
🟢 |
| 300-399 |
🟡 |
| 400+ |
🔴 |
get_country_flag(accept_lang: str) -> str
| Location |
app.py:82-90 |
| Input |
accept_lang: str - Accept-Language header |
| Output |
str - Country flag emoji |
| Purpose |
Extract country code from Accept-Language header |
parse_user_agent(ua_str: str) -> tuple[str, str]
| Location |
app.py:92-134 |
| Input |
ua_str: str - User-Agent string |
| Output |
tuple[str, str] - (device_type, ua_string) |
| Purpose |
Parse User-Agent string into device type and short UA |
| Device Type |
Detection |
| bot |
spider, bot, crawler |
| mobile |
smartphone, or "mobile" in UA |
| tablet |
tablet |
| desktop |
default |
3. Request Hooks
@app.before_request
| Location |
app.py:136-138 |
| Purpose |
Start request timing for duration tracking |
| Sets |
g.start_time - Request start timestamp |
@app.after_request
| Location |
app.py:140-189 |
| Purpose |
Add security headers and request logging |
| Headers Added |
HSTS, X-Frame-Options, CSP, etc. |
Security Headers Added:
| Header |
Value |
| Strict-Transport-Security |
max-age=31536000; includeSubDomains |
| X-Frame-Options |
DENY |
| X-Content-Type-Options |
nosniff |
| Referrer-Policy |
strict-origin-when-cross-origin |
| Permissions-Policy |
geolocation=(), microphone=(), camera=() |
| Content-Security-Policy |
default-src 'self'; img-src 'self' data: blob:; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://e621.net; object-src 'none'; base-uri 'self'; |
4. Image Validation Functions
is_valid_image(file) -> bool
| Location |
app.py:217-225 |
| Input |
file - File-like object |
| Output |
bool - True if valid image |
| Purpose |
Verify file is a valid image |
is_allowed_file(filename: str, content_type: str) -> bool
| Location |
app.py:227-229 |
| Input |
filename: str, content_type: str |
| Output |
bool - True if allowed |
| Purpose |
Check file extension and MIME type |
5. Processing Functions
detect_meta_tags_for_image_path(image_path: str) -> set[str]
| Location |
app.py:231-287 |
| Input |
image_path: str - Path to image file |
| Output |
set[str] - Auto-detected meta tags |
| Purpose |
Generate meta tags from image properties |
| Tag |
Condition |
| animated |
GIF with n_frames > 1 |
| thumbnail |
w ≤ 250 and h ≤ 250 |
| low_res |
w ≤ 500 and h ≤ 500 |
| hi_res |
w ≥ 1600 or h ≥ 1200 |
| absurd_res |
w ≥ 3200 or h ≥ 2400 |
| 4k |
Exact 3840x2160/4096x2160 variants |
| superabsurd_res |
w ≥ 10000 and h ≥ 10000 |
| long_image |
ratio ≥ 4:1 or ≤ 1:4 |
| tall_image |
ratio ≤ 1:4 |
| 1:1, 2:1, 16:9, etc. |
Exact aspect ratios |
| widescreen |
16:9 or ~16:10 |
save_upload(file, original_filename: str) -> str | None
| Location |
app.py:289-305 |
| Input |
file, original_filename: str |
| Output |
str - Save path, or None on failure |
| Purpose |
Save uploaded file to disk |
| Requirement |
SAVE_UPLOADS=true |
6. Flask Routes
index() -> str
| Location |
app.py:312-314 |
| Route |
GET / |
| Output |
HTML - Rendered index.html |
| Purpose |
Main UI page |
favicon() -> Response
| Location |
app.py:316-320 |
| Route |
GET /favicon.ico |
| Output |
ICO file with 24h caching |
| Purpose |
Serve favicon |
static_files(filename: str) -> Response
| Location |
app.py:322-326 |
| Route |
GET /static/<path> |
| Input |
filename: str - File path |
| Output |
Static file with 24h caching |
| Purpose |
Serve CSS, JS, icons |
service_worker() -> Response
| Location |
app.py:328-335 |
| Route |
GET /service-worker.js |
| Output |
JS with no-cache headers |
| Purpose |
PWA service worker |
health() -> tuple
| Location |
app.py:337-353 |
| Route |
GET /health |
| Output |
JSON - Health status |
| Purpose |
Health check endpoint |
Healthy Response:
Unhealthy Response (503):
predict() -> tuple
| Location |
app.py:355-456 |
| Route |
POST /predict |
| Rate Limit |
20/minute |
| Input |
image file (multipart/form-data), optional top_k |
| Output |
JSON - Tag predictions |
Request Parameters:
| Parameter |
Type |
Required |
Description |
| image |
file |
Yes |
Image file |
| top_k |
int |
No |
Number of tags (50-300, default 200) |
Success Response:
Error Responses:
| Status |
Error Message |
| 400 |
"No image provided" |
| 400 |
"Empty filename" |
| 400 |
"File type not allowed" |
| 400 |
"Invalid or corrupted image file" |
| 413 |
"File too large. Maximum size is 20MB." |
| 500 |
"Internal server error" |
robots() -> Response
| Location |
app.py:458-468 |
| Route |
GET /robots.txt |
| Output |
Robots.txt content |
| Purpose |
Search engine instructions |
Content:
sitemap() -> Response
| Location |
app.py:470-482 |
| Route |
GET /sitemap.xml |
| Output |
XML sitemap |
| Purpose |
SEO sitemap |
handle_file_too_large(e) -> tuple
| Location |
app.py:307-310 |
| Handler |
413 errors |
| Purpose |
Handle file too large |
7. Runtime Initialization
These run at module load time (lines 191-215):
Global Runtime Objects
| Object |
Type |
Description |
model |
NaFlexVit |
Loaded ML model |
tag_list |
list[str] |
All tag names (~7500) |
ext_info |
dict |
Extension info |
metadata |
dict |
Tag categories and implications |
8. Entry Point
| Setting |
Value |
Description |
| Host |
0.0.0.0 |
Accept all interfaces |
| Port |
5000 |
Default port |
| debug |
False |
Production mode |
Quick Reference for Agents
Calling the API
Environment Variables
| Variable |
Required |
Description |
| APP_VERSION |
No |
Version string |
| MODEL_PATH |
No |
Model file path |
| DEVICE |
No |
cuda or cpu |
| MAX_SEQ_LEN |
No |
Max patches |
Error Handling
The app returns different HTTP status codes:
- 200: Success
- 400: Bad request (missing file, invalid type)
- 413: File too large
- 429: Rate limit exceeded
- 500: Internal error
Logging
All logs include timestamp, level, and emoji prefix:
- 🚀 - Startup/version
- ⚙️ - Loading
- ✅ - Success
- ⚠️ - Warning
- ❌ - Error
- 📥 - Upload
- 📤 - Prediction request
- 👤 - Page view