File size: 6,034 Bytes
e38de99 |
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 |
# DCRM Analysis API - Quick Start Guide
## π Overview
This FastAPI wrapper provides a REST API endpoint for DCRM (Dynamic Contact Resistance Measurement) analysis. It accepts CSV uploads and returns comprehensive JSON analysis reports.
## π Quick Start
### 1. Install Dependencies
```bash
pip install -r requirements_api.txt
```
### 2. Configure Deployment URL
Open `dcrm_api.py` and update line 46:
```python
DEPLOYMENT_URL = "http://localhost:5000" # Change to your deployed URL
```
### 3. Run the API Server
```bash
python dcrm_api.py
```
The API will start on `http://localhost:5000`
## π‘ API Endpoints
### Main Analysis Endpoint
**POST** `/api/circuit-breakers/{breaker_id}/tests/upload`
**Parameters:**
- `breaker_id` (path parameter): Circuit breaker identifier
- `file` (form-data): CSV file with DCRM test data
**Example Request (using curl):**
```bash
curl -X POST \
"http://localhost:5000/api/circuit-breakers/6926e63d4614721a79b7b24e/tests/upload" \
-F "file=@df3_final.csv"
```
**Example Request (using Python requests):**
```python
import requests
url = "http://localhost:5000/api/circuit-breakers/6926e63d4614721a79b7b24e/tests/upload"
files = {'file': open('df3_final.csv', 'rb')}
response = requests.post(url, files=files)
print(response.json())
```
**Example Request (using JavaScript/Fetch):**
```javascript
const formData = new FormData();
formData.append('file', csvFile); // csvFile is a File object
fetch('http://localhost:5000/api/circuit-breakers/6926e63d4614721a79b7b24e/tests/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log(data));
```
### Health Check Endpoints
**GET** `/`
- Simple health check
- Returns API status and version
**GET** `/api/health`
- Detailed health check
- Returns status of all components (LLM, ViT model, etc.)
## π CSV Format Requirements
The uploaded CSV must contain these columns:
- `Time_ms` - Time in milliseconds
- `Resistance` - Resistance values in ¡Ω
- `Current` - Current values
- `Travel` - Travel distance
- `Close_Coil` - Close coil current
- `Trip_Coil_1` - Trip coil 1 current
- `Trip_Coil_2` - Trip coil 2 current
**Minimum rows:** 100 (typically ~400 rows)
## π€ Response Format
The API returns a comprehensive JSON report matching the structure in `data/dcrm-sample-response.txt`, including:
- **aiVerdict**: AI-generated fault analysis and recommendations
- **breakerId**: Circuit breaker ID from the request
- **cbhi**: Composite Breaker Health Index with history
- **kpis**: Array of Key Performance Indicators
- **phaseWiseAnalysis**: Detailed analysis of 5 operational phases
- **waveform**: Time-series data with SHAP values
- **findings**: Summary of detected faults
- **healthScore**: Overall health score (0-100)
## β οΈ Error Handling
The API returns detailed error messages for common issues:
### Invalid File Type (400)
```json
{
"error": "Invalid file type",
"message": "Only CSV files are accepted",
"received": "data.xlsx"
}
```
### Missing Columns (400)
```json
{
"error": "Missing required columns",
"missing": ["Travel", "Current"],
"required": ["Time_ms", "Resistance", "Current", ...],
"found": ["Time_ms", "Resistance", ...]
}
```
### Insufficient Data (400)
```json
{
"error": "Insufficient data",
"message": "CSV must contain at least 100 rows of data",
"received_rows": 50
}
```
### Analysis Failure (500)
```json
{
"error": "Analysis failed",
"message": "An error occurred during DCRM analysis",
"error_type": "ValueError",
"error_details": "..."
}
```
## π§ Configuration Options
### Change Port
Edit the last line in `dcrm_api.py`:
```python
uvicorn.run(app, host="0.0.0.0", port=5000) # Change port here
```
### Enable/Disable CORS
Modify the CORS middleware settings (line 60):
```python
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change to specific domains for security
...
)
```
### Update Google API Key
Change the API key on line 20:
```python
os.environ["GOOGLE_API_KEY"] = "your-api-key-here"
```
## π Deployment
### Local Development
```bash
python dcrm_api.py
```
### Production (using Gunicorn + Uvicorn)
```bash
pip install gunicorn
gunicorn dcrm_api:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:5000
```
### Docker Deployment
Create a `Dockerfile`:
```dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements_api.txt .
RUN pip install -r requirements_api.txt
COPY . .
CMD ["python", "dcrm_api.py"]
```
Build and run:
```bash
docker build -t dcrm-api .
docker run -p 5000:5000 dcrm-api
```
## π API Documentation
Once the server is running, visit:
- **Swagger UI**: http://localhost:5000/docs
- **ReDoc**: http://localhost:5000/redoc
These provide interactive API documentation and testing interfaces.
## π§ͺ Testing
Test with the sample CSV:
```bash
curl -X POST \
"http://localhost:5000/api/circuit-breakers/test-123/tests/upload" \
-F "file=@df3_final (1).csv" \
-o response.json
```
## π Monitoring
The API logs all requests and errors to the console. For production, consider:
- Setting up proper logging (to files or logging services)
- Adding request/response monitoring
- Implementing rate limiting
- Adding authentication if needed
## π‘ Tips
1. **Processing Time**: Analysis typically takes 30-60 seconds due to AI processing
2. **Concurrent Requests**: The API can handle multiple requests, but heavy AI processing may slow down responses
3. **File Size**: Keep CSV files under 5MB for optimal performance
4. **Caching**: Consider implementing caching for repeated analyses of the same data
## π Support
For issues or questions, check:
- API logs in the console
- Error messages in the response
- The `/api/health` endpoint for component status
|