This is a Flask-based REST API for converting TensorFlow.js models to TensorFlow Lite format and generating C/C++ byte arrays for microcontroller deployment.
- Install dependencies:
pip install -r requirements.txt- Run the development server:
python app.pyThe API will be available at http://localhost:3000
Converts a TensorFlow.js model to TFLite format and returns formatted C/C++ code.
Request Body:
{
"modelData": "base64-encoded model.json content",
"weightsData": ["base64-encoded weight file 1", "..."],
"modelMetadata": {
"inputShape": [1, 224, 224, 3],
"outputShape": [1, 10],
"classes": ["class1", "class2", ...]
},
"options": {
"quantize": true,
"quantizationType": "int8",
"arrayName": "g_person_detect_model_data",
"includeMetadata": true
}
}Quantization Types:
"int8"(recommended): Full integer quantization - best for microcontrollers (smallest size, fastest inference)"float16": Float16 weight quantization - moderate compression"dynamic": Dynamic range quantization - weights to int8, activations stay float
Success Response (200):
{
"success": true,
"data": {
"cppCode": "alignas(8) const unsigned char g_person_detect_model_data[] = {...}",
"modelSize": 18432,
"arrayName": "g_person_detect_model_data",
"timestamp": "2024-01-15T10:30:00Z"
}
}Error Response (4xx/5xx):
{
"success": false,
"error": {
"message": "Error message",
"details": "Detailed error information",
"type": "ERROR_TYPE",
"suggestions": ["Suggestion 1", "Suggestion 2"],
"retryable": true
}
}Health check endpoint to verify the API is running.
Response (200):
{
"status": "healthy",
"service": "TFLite Conversion API",
"timestamp": "2024-01-15T10:30:00Z"
}Returns the pre-compiled camera capture firmware binary for flashing to a device.
Request:
- No request body required.
Response (200):
- Content-Type: application/octet-stream
- File download:
camera_capture.bin
Error (404):
{
"success": false,
"error": {
"message": "Binary not found",
"details": "camera_capture.bin does not exist in templates/",
"type": "FILE_NOT_FOUND",
"retryable": false
}
}Returns the pre-compiled acceleration capture firmware binary for flashing to a device.
Request:
- No request body required.
Response (200):
- Content-Type: application/octet-stream
- File download:
acceleration_capture.bin
Error (404):
{
"success": false,
"error": {
"message": "Binary not found",
"details": "acceleration_capture.bin does not exist in templates/",
"type": "FILE_NOT_FOUND",
"retryable": false
}
}Returns the pre-compiled gesture capture firmware binary for flashing to a device.
Request:
- No request body required.
Response (200):
- Content-Type: application/octet-stream
- File download:
gesture_capture.bin
Error (404):
{
"success": false,
"error": {
"message": "Binary not found",
"details": "gesture_capture.bin does not exist in templates/",
"type": "FILE_NOT_FOUND",
"retryable": false
}
}VALIDATION_ERROR: Invalid request data (not retryable)CONVERSION_ERROR: Model conversion failed (retryable)UNSUPPORTED_OPS: Model contains unsupported operations (not retryable)SIZE_LIMIT: Model exceeds size limits (not retryable)NETWORK: Network-related errors (retryable)UNKNOWN: Unexpected errors (retryable)
- Max Upload Size: 10MB (configurable in
app.py) - Port: 3000 (default)
- CORS: Enabled for
localhost:5173andlocalhost:3000
Run the test suite:
pytest tests/test_api.py -vRun with coverage:
pytest tests/test_api.py --cov=app --cov-report=htmlFor production, use a WSGI server like Gunicorn:
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:3000 app:appOr use uWSGI:
pip install uwsgi
uwsgi --http :3000 --wsgi-file app.py --callable app --processes 4- The API automatically cleans up temporary files after each conversion
- Conversion timeout is set to 60 seconds
- Large models (>10MB) will be rejected
- Quantization is enabled by default to reduce model size