A Python FastAPI service that provides medical AI capabilities using the MedGemma-4B-IT model via Gradio Client.
- FastAPI Framework: Modern, fast web framework for building APIs
- Gradio Client Integration: Direct connection to Hugging Face MedGemma-4B-IT space
- Medical Safety: Built-in medical disclaimers and safety checks
- Authentication: Optional API key authentication
- Error Handling: Graceful error handling with fallback responses
- Health Checks: Built-in health monitoring endpoints
- Docker Support: Ready for containerized deployment
-
Clone and Setup:
cd python_ai_service pip install -r requirements.txt -
Configure Environment:
cp .env.example .env # Edit .env with your configuration -
Run the Service:
python main.py
-
Test the Service:
curl -X POST "http://localhost:8000/query-medgemma" \ -H "Content-Type: application/json" \ -d '{ "message": { "text": "What are the symptoms of diabetes?", "files": [] }, "system_prompt": "You are a helpful medical expert.", "max_tokens": 2048 }'
-
Build the Image:
docker build -t medical-ai-service . -
Run the Container:
docker run -p 8000:8000 \ -e API_KEY=your-secure-key \ -e HF_TOKEN=your-hf-token \ medical-ai-service
Query the MedGemma-4B-IT model with a medical question.
Request Body:
{
"message": {
"text": "What are the symptoms of diabetes?",
"files": []
},
"system_prompt": "You are a helpful medical expert.",
"max_tokens": 2048,
"user_id": "optional-user-id"
}Response:
{
"response": "Diabetes symptoms include frequent urination, excessive thirst...",
"confidence": 0.85,
"source": "medgemma-4b-it-gradio",
"processing_time": 2.34,
"user_id": "optional-user-id"
}Health check endpoint that verifies service status and Gradio client connection.
Basic service information endpoint.
| Variable | Description | Default |
|---|---|---|
API_KEY |
API key for authentication | your-secure-api-key-here |
REQUIRE_API_KEY |
Whether API key is required | false |
HUGGINGFACE_SPACE |
Hugging Face space to use | Abdhack/medgemma-4b-it |
HF_TOKEN |
Hugging Face token (optional) | - |
HOST |
Server host | 0.0.0.0 |
PORT |
Server port | 8000 |
ENVIRONMENT |
Environment (development/production) | production |
- Connect your GitHub repository to Render
- Create a new Web Service
- Set build command:
pip install -r requirements.txt - Set start command:
python main.py - Add environment variables in Render dashboard
# Deploy from source
gcloud run deploy medical-ai-service \
--source . \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars API_KEY=your-key,HF_TOKEN=your-token- Connect your GitHub repository to Railway
- Railway will auto-detect the Python app
- Add environment variables in Railway dashboard
- Deploy automatically on git push
# Create Heroku app
heroku create your-medical-ai-service
# Set environment variables
heroku config:set API_KEY=your-secure-key
heroku config:set HF_TOKEN=your-hf-token
# Deploy
git push heroku mainAfter deploying your Python service, update your Supabase Edge Function environment variables:
- Go to your Supabase project dashboard
- Navigate to Edge Functions
- Set the following environment variables:
PYTHON_SERVICE_URL: Your deployed service URL (e.g.,https://your-service.onrender.com)PYTHON_SERVICE_API_KEY: Your API key (if using authentication)
- API Key Authentication: Enable
REQUIRE_API_KEY=truein production - CORS Configuration: Update CORS origins to match your domain
- Rate Limiting: Consider adding rate limiting for production use
- Input Validation: The service includes input validation via Pydantic models
- Medical Disclaimers: Automatic addition of medical disclaimers to responses
- Health check endpoint at
/health - Structured logging with request/response tracking
- Processing time measurement
- Error tracking and graceful fallbacks
-
Gradio Client Connection Failed:
- Check if the Hugging Face space is accessible
- Verify HF_TOKEN if using a private space
- Check network connectivity
-
Service Unavailable:
- Check if the service is running on the correct port
- Verify environment variables are set correctly
- Check logs for detailed error messages
-
Authentication Errors:
- Verify API_KEY matches between client and server
- Check if REQUIRE_API_KEY is set correctly
The service provides detailed logging for debugging:
- Request/response tracking
- Processing time measurement
- Error details and stack traces
- Gradio client connection status