Skip to content

Latest commit

 

History

71 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

X-Ray Analysis Service

Deploy to Heroku

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.

Table of Contents

Architecture

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Salesforce    │◄──▶│   Heroku App     │───▶│   AI Model      │
│   Health Cloud  │    │   (FastAPI)      │    │   (DenseNet)    │
│                 │    │                  │    │                 │
│ • ContentVersion│    │ • Image Processing│    │ • X-ray Analysis│
│ • Patient Data  │    │ • API Endpoints  │    │ • Diagnosis     │
│ • Flows         │    │ • AppLink        │    │ • Confidence    │
└─────────────────┘    └──────────────────┘    └─────────────────┘

Features

  • 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

Technology Stack

  • Framework: FastAPI
  • AI/ML: PyTorch, TorchXrayVision
  • Image Processing: PIL (Pillow), NumPy
  • Deployment: Heroku with Integration Service Mesh
  • Python Version: 3.13+

API Endpoints

GET /xray_diagnosis

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

Installation

  1. Clone the repository:
git clone git@github.com:vivekvj01/xrayanalysis.git
cd xrayanalysis
  1. Install dependencies:
pip install -r requirements.txt
  1. Run the application:
uvicorn app:app --host=0.0.0.0 --port=3000

Dependencies

Key dependencies include:

  • fastapi: Web framework
  • torch: PyTorch for deep learning
  • torchxrayvision: Pre-trained X-ray analysis models
  • Pillow: Image processing
  • heroku-applink: Salesforce integration
  • requests: HTTP client

Model Information

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

Heroku Deployment Steps

Quick Deploy (Recommended)

Click the "Deploy to Heroku" button above to automatically deploy the application to Heroku.

Manual Deployment

  1. Create a Heroku Account: If you don't have one, sign up at Heroku.

  2. Install the Heroku CLI: Follow the Heroku CLI Installation Guide.

  3. Login to Heroku:

    heroku login
  4. Clone the Repository:

    git clone https://github.com/vivekvj01/xrayanalysis.git
    cd xrayanalysis
  5. Create a Heroku App:

    heroku create your-xray-analysis-app
  6. Deploy the App:

    git push heroku main
  7. Scale the App:

    heroku ps:scale web=1
  8. Open the App:

    heroku open

AppLink Configuration for Salesforce Integration

  1. Provision the AppLink Add-on:

    heroku addons:create heroku-applink
  2. Add the Service Mesh Buildpack:

    heroku buildpacks:add --index=1 heroku/heroku-applink-service-mesh
  3. Connect to Your Salesforce Org:

    • Using OAuth:

      heroku salesforce:connect CONNECTION_NAME -a your-app-name

      Replace CONNECTION_NAME with 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, and YOUR_SALESFORCE_USERNAME with your values.

  4. Publish Your App:

    heroku salesforce:publish ./spec.yaml -a your-app-name -c "X-Ray Analysis Client" --connection-name=CONNECTION_NAME

    Replace CONNECTION_NAME with the same name used in step 3.

  5. Verify Publication:

    heroku salesforce:publications -a your-app-name
  6. Set Up Flow for Integration and Test API:

    • Create Flow for Integration:

      • In Salesforce, navigate to SetupProcess AutomationFlows
      • 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.

Deployment Configuration

The service is configured for Heroku deployment with:

  • Procfile: Defines the web process
  • runtime.txt: Specifies Python version
  • app.json: Heroku deployment configuration with AppLink add-on
  • Integration Service Mesh for Salesforce connectivity

Salesforce Health Cloud Integration Setup

Prerequisites

  • Salesforce Health Cloud org with appropriate permissions
  • Heroku account with Salesforce integration capabilities
  • Admin access to both Salesforce and Heroku

AppLink Configuration Steps

  1. Configure Health Cloud Permissions

    • In Salesforce Setup → Permission Sets
    • Create or modify a permission set for the integration
    • Grant the following permissions:
      • ContentVersion: Read
      • ContentDocument: Read
      • ContentDocumentLink: Read
      • Account: Read (for patient context)
      • Contact: Read (for patient context)
      • Case: Read/Write (for diagnostic results)
  2. 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)

Testing the Integration

  1. 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

Troubleshooting

  • 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

Security Considerations

  • 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

Usage Notes

  • 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

Error Handling

The service includes comprehensive error handling for:

  • Invalid image formats
  • Network connectivity issues
  • Model prediction errors
  • Salesforce API errors

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

License

This project is licensed under the MIT License.

Disclaimer

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages