A FastAPI-based medical imaging service that analyzes X-ray images using deep learning to provide diagnostic insights. The service integrates with Salesforce to retrieve X-ray images from ContentVersion records and returns AI-powered analysis results.
- Architecture
- Features
- Technology Stack
- API Endpoints
- Installation
- Dependencies
- Model Information
- Heroku Deployment Steps
- Deployment Configuration
- Salesforce Health Cloud Integration Setup
- Usage Notes
- Error Handling
- Contributing
- License
- Disclaimer
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Salesforce │◄──▶│ Heroku App │───▶│ AI Model │
│ Health Cloud │ │ (FastAPI) │ │ (DenseNet) │
│ │ │ │ │ │
│ • ContentVersion│ │ • Image Processing│ │ • X-ray Analysis│
│ • Patient Data │ │ • API Endpoints │ │ • Diagnosis │
│ • Flows │ │ • AppLink │ │ • Confidence │
└─────────────────┘ └──────────────────┘ └─────────────────┘
- AI-Powered X-Ray Analysis: Uses a pre-trained DenseNet model to analyze chest X-ray images
- Multiple Condition Detection: Identifies various medical conditions including:
- Normal findings
- Pneumonia
- Tuberculosis
- COVID-19
- Pleural Effusion
- Other abnormalities
- Salesforce Integration: Seamlessly retrieves X-ray images from Salesforce ContentVersion records
- Confidence Scoring: Provides confidence levels for each diagnosis
- RESTful API: Clean, documented API endpoints for easy integration
- Framework: FastAPI
- AI/ML: PyTorch, TorchXrayVision
- Image Processing: PIL (Pillow), NumPy
- Deployment: Heroku with Integration Service Mesh
- Python Version: 3.13+
Analyzes an X-ray image from Salesforce ContentVersion and returns diagnostic results.
Parameters:
content_version_id(string, required): Salesforce ContentVersion ID of the X-ray image
Response:
{
"data": "Diagnosis: [Condition]\nConfidence: [0.00-1.00]"
}Example:
GET /xray_diagnosis?content_version_id=068000000000000
- Clone the repository:
git clone git@github.com:vivekvj01/xrayanalysis.git
cd xrayanalysis- Install dependencies:
pip install -r requirements.txt- Run the application:
uvicorn app:app --host=0.0.0.0 --port=3000Key dependencies include:
fastapi: Web frameworktorch: PyTorch for deep learningtorchxrayvision: Pre-trained X-ray analysis modelsPillow: Image processingheroku-applink: Salesforce integrationrequests: HTTP client
The service uses a pre-trained DenseNet-121 model specifically trained on chest X-ray datasets. The model:
- Accepts 224x224 grayscale images
- Outputs probabilities for various pathologies
- Uses a confidence threshold of 0.5 for positive findings
- Supports multiple medical conditions simultaneously
Click the "Deploy to Heroku" button above to automatically deploy the application to Heroku.
-
Create a Heroku Account: If you don't have one, sign up at Heroku.
-
Install the Heroku CLI: Follow the Heroku CLI Installation Guide.
-
Login to Heroku:
heroku login
-
Clone the Repository:
git clone https://github.com/vivekvj01/xrayanalysis.git cd xrayanalysis -
Create a Heroku App:
heroku create your-xray-analysis-app
-
Deploy the App:
git push heroku main
-
Scale the App:
heroku ps:scale web=1
-
Open the App:
heroku open
-
Provision the AppLink Add-on:
heroku addons:create heroku-applink
-
Add the Service Mesh Buildpack:
heroku buildpacks:add --index=1 heroku/heroku-applink-service-mesh
-
Connect to Your Salesforce Org:
-
Using OAuth:
heroku salesforce:connect CONNECTION_NAME -a your-app-name
Replace
CONNECTION_NAMEwith a unique name for your connection (3-30 characters, alphanumeric and underscores only). -
Using JWT:
heroku salesforce:connect:jwt CONNECTION_NAME -a your-app-name --client-id=YOUR_CLIENT_ID --jwt-key-file=PATH_TO_PRIVATE_KEY --username=YOUR_SALESFORCE_USERNAME
Replace
CONNECTION_NAME,YOUR_CLIENT_ID,PATH_TO_PRIVATE_KEY, andYOUR_SALESFORCE_USERNAMEwith your values.
-
-
Publish Your App:
heroku salesforce:publish ./spec.yaml -a your-app-name -c "X-Ray Analysis Client" --connection-name=CONNECTION_NAMEReplace
CONNECTION_NAMEwith the same name used in step 3. -
Verify Publication:
heroku salesforce:publications -a your-app-name
-
Set Up Flow for Integration and Test API:
-
Create Flow for Integration:
- In Salesforce, navigate to Setup → Process Automation → Flows
- Create a new Flow
- Add "Get Records" element to retrieve ContentVersion
- Add "Apex Action" element to call the X-Ray Analysis API
- Add "Create Records" element to save results
- Activate the Flow
-
Test API Endpoint:
curl -X GET "https://your-heroku-app.herokuapp.com/xray_diagnosis?content_version_id=YOUR_CONTENT_VERSION_ID" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
-
Access Your Published App in Salesforce:
- In Salesforce, navigate to Setup > External Services.
- Your published app should appear in the list of available external services.
- Click on your app to view its configuration and available operations.
- The app is now ready to be used in Salesforce Flows, Process Builder, or Apex code.
-
For more detailed information, refer to the Heroku AppLink CLI Plugin.
The service is configured for Heroku deployment with:
Procfile: Defines the web processruntime.txt: Specifies Python versionapp.json: Heroku deployment configuration with AppLink add-on- Integration Service Mesh for Salesforce connectivity
- Salesforce Health Cloud org with appropriate permissions
- Heroku account with Salesforce integration capabilities
- Admin access to both Salesforce and Heroku
-
Configure Health Cloud Permissions
- In Salesforce Setup → Permission Sets
- Create or modify a permission set for the integration
- Grant the following permissions:
ContentVersion: ReadContentDocument: ReadContentDocumentLink: ReadAccount: Read(for patient context)Contact: Read(for patient context)Case: Read/Write(for diagnostic results)
-
Create Custom Objects (Optional)
- Setup → Object Manager → Create → Custom Object
- Create "X-Ray Analysis Result" object with fields:
Patient__c(Lookup to Account)Content_Version_Id__c(Text)Diagnosis__c(Text)Confidence_Score__c(Number)Analysis_Date__c(DateTime)Image_URL__c(URL)
- Verify in Salesforce
- Upload an X-ray image to ContentVersion
- Note the ContentVersion ID
- Test the API endpoint with the ID
- Verify results are returned correctly
- Authentication Issues: Verify Connected App settings and OAuth scopes
- Permission Errors: Check permission sets and object-level security
- API Timeouts: Ensure Heroku app has sufficient dyno resources
- Image Processing Errors: Verify image format and size requirements
- Use HTTPS for all communications
- Implement proper error handling and logging
- Regularly rotate OAuth tokens
- Monitor API usage and implement rate limiting
- Ensure HIPAA compliance for patient data handling
- The service processes images in grayscale format
- Images are automatically resized to 224x224 pixels
- Results include both the primary diagnosis and confidence score
- The model is optimized for CPU inference
- All predictions are made with gradient computation disabled for efficiency
The service includes comprehensive error handling for:
- Invalid image formats
- Network connectivity issues
- Model prediction errors
- Salesforce API errors
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is licensed under the MIT License.
This service is for research and educational purposes only. It should not be used as a substitute for professional medical diagnosis or treatment. Always consult with qualified healthcare professionals for medical decisions.