Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,261 +1,272 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
- **
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
###
|
| 82 |
-
|
| 83 |
-
- **
|
| 84 |
-
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
```
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
```
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
```
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
```
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
###
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: GP-Tea Lab Analysis
|
| 3 |
+
emoji: π§ͺ
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# GP-Tea Lab Analysis Service
|
| 13 |
+
|
| 14 |
+
A FastAPI-based web service for analyzing lab report images using AI. This service accepts lab report images and provides structured medical analysis with key findings, interpretations, and health insights.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- πΌοΈ Image upload support (JPG, PNG, BMP, TIFF, WEBP)
|
| 19 |
+
- π AI-powered lab report analysis
|
| 20 |
+
- π Structured response with summary, key findings, and interpretation
|
| 21 |
+
- π RESTful API with automatic documentation
|
| 22 |
+
- π§ͺ Built-in test client and web interface
|
| 23 |
+
- β‘ Async processing for better performance
|
| 24 |
+
|
| 25 |
+
## Project Structure
|
| 26 |
+
|
| 27 |
+
```
|
| 28 |
+
Lab_analysis/
|
| 29 |
+
βββ main.py # FastAPI application
|
| 30 |
+
βββ lab_analyzer.py # Core analysis logic
|
| 31 |
+
βββ models.py # Pydantic models
|
| 32 |
+
βββ test_client.py # API test client
|
| 33 |
+
βββ index.html # Web interface
|
| 34 |
+
βββ requirements.txt # Dependencies
|
| 35 |
+
βββ Lab_report_analysis.py # Original script
|
| 36 |
+
βββ README.md # This file
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
## Installation
|
| 40 |
+
|
| 41 |
+
1. **Clone or navigate to the project directory:**
|
| 42 |
+
|
| 43 |
+
```bash
|
| 44 |
+
cd "e:\E-JUST Assignments\Projects\HealthCare\Lab_analysis"
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
2. **Create a virtual environment (recommended):**
|
| 48 |
+
|
| 49 |
+
```bash
|
| 50 |
+
python -m venv venv
|
| 51 |
+
venv\Scripts\activate # On Windows
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
3. **Install dependencies:**
|
| 55 |
+
```bash
|
| 56 |
+
pip install -r requirements.txt
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## Running the API
|
| 60 |
+
|
| 61 |
+
### Method 1: Using Python directly
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
python main.py
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
### Method 2: Using Uvicorn
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
The API will be available at:
|
| 74 |
+
|
| 75 |
+
- **API Endpoints**: http://localhost:8000
|
| 76 |
+
- **Interactive Docs**: http://localhost:8000/docs
|
| 77 |
+
- **ReDoc**: http://localhost:8000/redoc
|
| 78 |
+
|
| 79 |
+
## API Endpoints
|
| 80 |
+
|
| 81 |
+
### Health Check
|
| 82 |
+
|
| 83 |
+
- **GET** `/health`
|
| 84 |
+
- Returns service status
|
| 85 |
+
|
| 86 |
+
### Analyze Lab Report (File Upload)
|
| 87 |
+
|
| 88 |
+
- **POST** `/analyze`
|
| 89 |
+
- Upload an image file for analysis
|
| 90 |
+
- Accepts: `multipart/form-data` with `file` field
|
| 91 |
+
|
| 92 |
+
### Analyze Lab Report (Base64)
|
| 93 |
+
|
| 94 |
+
- **POST** `/analyze-base64`
|
| 95 |
+
- Send base64 encoded image for analysis
|
| 96 |
+
- Accepts: JSON with `image` field containing base64 string
|
| 97 |
+
|
| 98 |
+
## Usage Examples
|
| 99 |
+
|
| 100 |
+
### Using cURL
|
| 101 |
+
|
| 102 |
+
1. **Health check:**
|
| 103 |
+
|
| 104 |
+
```bash
|
| 105 |
+
curl http://localhost:8000/health
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
2. **Analyze image file:**
|
| 109 |
+
|
| 110 |
+
```bash
|
| 111 |
+
curl -X POST "http://localhost:8000/analyze" \
|
| 112 |
+
-H "accept: application/json" \
|
| 113 |
+
-H "Content-Type: multipart/form-data" \
|
| 114 |
+
-F "file=@your_lab_report.jpg"
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
3. **Analyze base64 image:**
|
| 118 |
+
```bash
|
| 119 |
+
curl -X POST "http://localhost:8000/analyze-base64" \
|
| 120 |
+
-H "Content-Type: application/json" \
|
| 121 |
+
-d '{"image": "your_base64_encoded_image_here"}'
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
### Using Python Test Client
|
| 125 |
+
|
| 126 |
+
```python
|
| 127 |
+
from test_client import LabReportAPIClient
|
| 128 |
+
|
| 129 |
+
client = LabReportAPIClient()
|
| 130 |
+
|
| 131 |
+
# Health check
|
| 132 |
+
health = client.health_check()
|
| 133 |
+
print(health)
|
| 134 |
+
|
| 135 |
+
# Analyze image
|
| 136 |
+
result = client.analyze_image_file("path/to/your/lab_report.jpg")
|
| 137 |
+
print(result)
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Using the Web Interface
|
| 141 |
+
|
| 142 |
+
1. Start the API server
|
| 143 |
+
2. Open `index.html` in your web browser
|
| 144 |
+
3. Drag and drop or select a lab report image
|
| 145 |
+
4. Click "Analyze Report" to get results
|
| 146 |
+
|
| 147 |
+
## Response Format
|
| 148 |
+
|
| 149 |
+
Successful analysis returns:
|
| 150 |
+
|
| 151 |
+
```json
|
| 152 |
+
{
|
| 153 |
+
"success": true,
|
| 154 |
+
"filename": "lab_report.jpg",
|
| 155 |
+
"analysis": {
|
| 156 |
+
"error": false,
|
| 157 |
+
"summary": "Brief summary of the lab report",
|
| 158 |
+
"key_findings": ["Finding 1", "Finding 2", "Finding 3"],
|
| 159 |
+
"interpretation": "Medical interpretation",
|
| 160 |
+
"note": "Disclaimer about medical advice",
|
| 161 |
+
"raw_response": "Complete AI response"
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
## Configuration
|
| 167 |
+
|
| 168 |
+
### Environment Variables
|
| 169 |
+
|
| 170 |
+
You can set these environment variables to customize the behavior:
|
| 171 |
+
|
| 172 |
+
- `API_HOST`: Host to bind to (default: "0.0.0.0")
|
| 173 |
+
- `API_PORT`: Port to bind to (default: 8000)
|
| 174 |
+
- `HF_API_KEY`: Hugging Face API key (currently hardcoded in `lab_analyzer.py`)
|
| 175 |
+
|
| 176 |
+
### Updating API Key
|
| 177 |
+
|
| 178 |
+
To use your own Hugging Face API key, modify the `lab_analyzer.py` file:
|
| 179 |
+
|
| 180 |
+
```python
|
| 181 |
+
self.client = InferenceClient(
|
| 182 |
+
provider="nebius",
|
| 183 |
+
api_key="your_api_key_here", # Replace with your API key
|
| 184 |
+
)
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
## Development
|
| 188 |
+
|
| 189 |
+
### Running in Development Mode
|
| 190 |
+
|
| 191 |
+
```bash
|
| 192 |
+
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
### Testing
|
| 196 |
+
|
| 197 |
+
Run the test client:
|
| 198 |
+
|
| 199 |
+
```bash
|
| 200 |
+
python test_client.py
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
### Adding New Features
|
| 204 |
+
|
| 205 |
+
1. Add new endpoints to `main.py`
|
| 206 |
+
2. Update models in `models.py` if needed
|
| 207 |
+
3. Extend the analyzer in `lab_analyzer.py`
|
| 208 |
+
4. Update documentation
|
| 209 |
+
|
| 210 |
+
## Deployment
|
| 211 |
+
|
| 212 |
+
### Docker (Optional)
|
| 213 |
+
|
| 214 |
+
Create a `Dockerfile`:
|
| 215 |
+
|
| 216 |
+
```dockerfile
|
| 217 |
+
FROM python:3.9-slim
|
| 218 |
+
|
| 219 |
+
WORKDIR /app
|
| 220 |
+
COPY requirements.txt .
|
| 221 |
+
RUN pip install -r requirements.txt
|
| 222 |
+
|
| 223 |
+
COPY . .
|
| 224 |
+
|
| 225 |
+
EXPOSE 8000
|
| 226 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
Build and run:
|
| 230 |
+
|
| 231 |
+
```bash
|
| 232 |
+
docker build -t lab-analysis-api .
|
| 233 |
+
docker run -p 8000:8000 lab-analysis-api
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
### Production Considerations
|
| 237 |
+
|
| 238 |
+
- Use environment variables for API keys
|
| 239 |
+
- Set up proper CORS origins
|
| 240 |
+
- Add rate limiting
|
| 241 |
+
- Use HTTPS
|
| 242 |
+
- Add authentication if needed
|
| 243 |
+
- Set up logging and monitoring
|
| 244 |
+
|
| 245 |
+
## Troubleshooting
|
| 246 |
+
|
| 247 |
+
### Common Issues
|
| 248 |
+
|
| 249 |
+
1. **Import errors**: Make sure all dependencies are installed
|
| 250 |
+
2. **Port conflicts**: Change the port in the uvicorn command
|
| 251 |
+
3. **API key issues**: Verify your Hugging Face API key is valid
|
| 252 |
+
4. **Image format errors**: Ensure images are in supported formats
|
| 253 |
+
|
| 254 |
+
### Logs
|
| 255 |
+
|
| 256 |
+
The application logs important events. Check console output for debugging information.
|
| 257 |
+
|
| 258 |
+
## License
|
| 259 |
+
|
| 260 |
+
This project is for educational purposes. Please ensure you have proper licenses for any AI models used.
|
| 261 |
+
|
| 262 |
+
## Contributing
|
| 263 |
+
|
| 264 |
+
1. Fork the repository
|
| 265 |
+
2. Create a feature branch
|
| 266 |
+
3. Make your changes
|
| 267 |
+
4. Test thoroughly
|
| 268 |
+
5. Submit a pull request
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
**Note**: This analysis is for educational purposes only and should not replace professional medical advice.
|