This microservice provides a FastAPI-based REST API for uploading, processing, and managing DICOM files, with integration for any frontend (e.g., Next.js) and PostgreSQL storage.
- Upload images (converted to DICOM, anonymized, encrypted)
- Store upload and metadata in PostgreSQL
- Associate uploads with frontend users
- Retrieve user uploads and upload details
- Get upload statistics
- Background processing of batch uploads
- Comprehensive logging system
- Python 3.11+
- PostgreSQL database
- Redis (for batch processing with Celery)
- See
requirements.txtfor Python dependencies
git clone <repository-url>
cd DICOM# On Windows
python -m venv venv
venv\Scripts\activate
# On macOS/Linux
python -m venv venv
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the project root with the following variables:
# Database Configuration
DATABASE_URL=postgresql://username:password@localhost:5432/dicom_db
# OR individual parameters
POSTGRES_DB=dicom_db
POSTGRES_USER=username
POSTGRES_PASSWORD=password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
# Redis Configuration (for Celery)
REDIS_URL=redis://localhost:6379/0
Make sure PostgreSQL is running, then create the database and tables:
# Create database (if not already created)
createdb dicom_db
# Create tables
python create_tables.pyRedis is used for background processing of batch uploads. See Redis Setup for detailed instructions.
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000python -m uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4Start the Celery worker in a separate terminal:
# On Linux/macOS
celery -A job_queue.celery_app worker --loglevel=info
# On Windows
python celery_worker_windows.pyThe simple processor is automatically used if Celery is not available. No additional setup is required.
- POST
/upload - Headers:
X-File-Name: Original filename (required)X-User-ID: User ID (optional, set by your frontend)Content-Type: image/jpeg (or other image type)
- Body: Raw image file bytes
- Response:
upload_id: Unique upload IDmessage: Status messagediagnosis: ML diagnosis (mocked)confidence: Confidence score (mocked)
- POST
/upload/batch - Headers:
X-User-ID: User ID (optional)Content-Type: application/json
- Body: JSON with files data
{
"files": [
{
"filename": "scan1.jpg",
"content": "base64-encoded-file-content"
},
{
"filename": "dicom1.dcm",
"content": "base64-encoded-file-content"
}
]
}- Response:
batch_id: Unique batch IDmessage: Status messagetotal_files: Number of files queuedstatus: "queued"
- GET
/upload/batch/{batch_id}/status - Response:
batch_id: Batch identifierstatus: "queued", "processing", "completed", "failed"total_files: Total number of filesprocessed_files: Number of processed filesprogress_percentage: Completion percentage
- GET
/user/{user_id}/batches - Response:
user_id: User identifierbatches: List of batch recordscount: Number of batches
- GET
/upload/batch/{batch_id}/files - Response:
batch_id: Batch identifierfiles: List of files in the batchcount: Number of files
- POST
/upload - Headers:
X-File-Name: Original filename (required)X-User-ID: User ID (optional, set by your frontend)Content-Type: image/jpeg (or other image type)
- Body: Raw image file bytes
- Response:
upload_id: Unique upload IDmessage: Status messagediagnosis: ML diagnosis (mocked)confidence: Confidence score (mocked)
- GET
/user/{user_id}/uploads - Response:
user_id: The user IDuploads: List of upload recordscount: Number of uploads
- GET
/upload/{upload_id}/details - Response: Upload record details
- GET
/upload/{upload_id}/preprocessed-dicom - Response: Returns the anonymized DICOM file for the given upload_id as a downloadable file (MIME type: application/dicom)
- POST
/save-upload - Query Parameters:
user_id: User IDupload_id: Upload ID
- Response: Status message
- GET
/stats - Response:
total_uploads: Total number of uploadsunique_users: Number of unique userslatest_upload: Timestamp of latest uploadearliest_upload: Timestamp of earliest upload
- GET
/ - Response: Welcome message
See .env for database configuration.
See init.sql for schema.
A sample dockerfile is provided for containerization.
The application uses Python's built-in logging module to provide comprehensive logging. Logs are written to both the console and a rotating file.
- Logs are stored in the
logsdirectory (created automatically) - The main log file is
logs/dicom_service.log - Log files rotate when they reach 10MB, with a maximum of 5 backup files
The application uses the following log levels:
- DEBUG: Detailed information, typically useful only for diagnosing problems
- INFO: Confirmation that things are working as expected
- WARNING: Indication that something unexpected happened, but the application is still working
- ERROR: Due to a more serious problem, the application has not been able to perform a function
- CRITICAL: A serious error, indicating that the application may be unable to continue running
You can adjust the logging configuration in main.py by modifying the setup_logging() function.
- Ensure PostgreSQL is running and accessible
- Verify that the database credentials in your
.envfile are correct - Check that the database and required tables exist
- If using
DATABASE_URL, ensure the connection string format is correct
- Ensure Redis is running and accessible
- Check the Redis connection URL in your
.envfile - On Windows, use
celery_worker_windows.pyinstead of the standard Celery command - If Celery fails, the application will automatically fall back to the simple processor
- Ensure the
uploadsdirectory exists and is writable - Check that the
X-File-Nameheader is included in upload requests - Verify that the file content is being sent correctly in the request body
- For batch uploads, ensure the JSON payload is correctly formatted
- Ensure the
logsdirectory is writable - Check the log files for detailed error messages
- If logs are not being generated, verify the logging configuration in
main.py
For more details, see the code and comments in main.py and database.py.