Spaces:
Sleeping
Sleeping
File size: 7,339 Bytes
77da9e2 |
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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# π Quick Start Guide
## Unified Architecture API
The project now uses a **unified architecture** where every interface goes through the REST API.
```
βββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Gradio UI (app.py / app_ui.py) β
β β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
β
β HTTP/REST
β
ββββββββββββββββββββΌβββββββββββββββββββββββββββ
β β
β FastAPI Server (app_api.py) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Detection Service β
β ββ RF-DETR (detection) β
β ββ CLIP (classification) β
β ββ OCR (text extraction) β
β ββ BLIP (visual description) β
βββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## π― 3 Ways to Launch
### Option 1: Automatic Launch (Recommended for tests)
**One command starts everything:**
```bash
python app.py
```
**What happens:**
1. β
Starts the API in the background (port 8000)
2. β
Waits until the API is ready
3. β
Launches the Gradio interface (port 7860)
4. β
Handles clean shutdown with Ctrl+C
**Access:**
- Gradio Interface: http://localhost:7860
- API Docs: http://localhost:8000/docs
---
### Option 2: Manual Launch (2 terminals)
**For more control and debugging:**
**Terminal 1 - API Server:**
```bash
python app_api.py
```
**Terminal 2 - Gradio UI:**
```bash
python app_ui.py
```
**Access:**
- Gradio Interface: http://localhost:7860
- API Docs: http://localhost:8000/docs
---
### Option 3: API Only
**To use only the API (integration, scripts, etc.):**
```bash
python app_api.py
```
**Test the API:**
```bash
# Health check
curl http://localhost:8000/health
# Detect elements
curl -X POST "http://localhost:8000/detect" \
-F "image=@screenshot.png" \
-F "confidence_threshold=0.35" \
-F "enable_clip=true" \
-F "enable_ocr=true"
```
**Interactive documentation:**
- OpenAPI Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
---
## π§ Configuration
### Environment Variables
**API Server:**
```bash
export UVICORN_HOST="0.0.0.0" # Default: 0.0.0.0
export UVICORN_PORT="8000" # Default: 8000
```
**Gradio UI:**
```bash
export GRADIO_SERVER_NAME="0.0.0.0" # Default: 0.0.0.0
export GRADIO_SERVER_PORT="7860" # Default: 7860
export CU1_API_URL="http://localhost:8000" # API URL
```
**Example with custom ports:**
```bash
# API on port 9000, UI on port 9001
export UVICORN_PORT="9000"
export GRADIO_SERVER_PORT="9001"
export CU1_API_URL="http://localhost:9000"
python app.py
```
---
## π§ͺ Quick Tests
### Test 1: Make sure the API works
```bash
# In one terminal
python app_api.py
# In another terminal
curl http://localhost:8000/health
```
**Expected result:**
```json
{
"status": "healthy",
"cuda_available": false,
"device": "cpu"
}
```
---
### Test 2: Test detection via the interface
```bash
python app.py
```
1. Open http://localhost:7860
2. Upload an image
3. Click "π Detect Elements"
4. Check the results
---
### Test 3: Test detection through the API
```bash
# Start the API
python app_api.py
# In another terminal, test with curl
curl -X POST "http://localhost:8000/detect" \
-F "image=@votre_image.png" \
-F "confidence_threshold=0.35" \
-F "enable_ocr=true" \
| jq .
```
---
## π Troubleshooting
### Issue: "Connection Error - Cannot connect to API"
**Solution:**
1. Make sure the API is running: `curl http://localhost:8000/health`
2. Check the ports: no conflict with other apps
3. Check the API logs for errors
### Issue: "Port already in use"
**Solution:**
```bash
# Find the process that uses the port
lsof -i :8000 # or :7860
# Kill the process
kill -9 <PID>
# Or use a different port
export UVICORN_PORT="9000"
export GRADIO_SERVER_PORT="9001"
```
### Issue: "Module not found"
**Solution:**
```bash
# Reinstall dependencies
pip install -r requirements.txt
```
### Issue: Models slow to load
**Reason:** The first startup downloads the models
**Solution:** Be patient, the models are cached after the first download
- RF-DETR model (~few MB)
- CLIP model (~600 MB)
- BLIP model (~1 GB)
- EasyOCR models (~100 MB)
---
## π Monitoring
### API logs
The logs appear in the terminal where you launched `app_api.py`
### UI logs
The logs appear in the terminal where you launched `app.py` or `app_ui.py`
### Metrics
Visit http://localhost:8000/docs to view the API statistics
---
## β
Benefits of the Unified Architecture
1. **Single code path** β Easier to maintain
2. **Consistent behavior** β Same results everywhere
3. **Easy to test** β Only one API to test
4. **Scalable** β Can separate API and UI on different servers
5. **Simplified debugging** β Logs centralized in the API
---
## π― For Developers
### Code Architecture
```
.
βββ app.py # β¨ Unified launcher (API + UI)
βββ app_api.py # FastAPI server
βββ app_ui.py # Gradio UI client (manual)
β
βββ api/
β βββ endpoints.py # FastAPI endpoints
β
βββ detection/
β βββ service.py # Detection service
β βββ service_factory.py # Singleton pattern
β βββ image_utils.py # Image utilities
β βββ ocr_handler.py # OCR-only processing
β βββ response_builder.py # Response formatting
β
βββ ui/
βββ detection_wrapper.py # Detection wrappers
βββ gradio_interface.py # Gradio interface (API client)
βββ shared_interface.py # Shared UI components
```
### Request Flow
```
1. User uploads image in Gradio
β
2. `detect_with_api()` sends an HTTP POST to `/detect`
β
3. API endpoint validates the request
β
4. `DetectionService.analyze()` processes the image
β
5. Response formatted with `response_builder`
β
6. JSON returned to Gradio UI
β
7. UI displays annotated image + results
```
---
## π Notes
- **Thread Safety:** The service uses a singleton but passes parameters directly to `analyze()` to avoid race conditions
- **Performance:** The first call is slow (model loading), then fast
- **Memory:** Models use ~2-3 GB of RAM
- **GPU:** Automatic CUDA/MPS detection if available
---
## π Next Steps
1. **Test locally:** `python app.py`
2. **Explore the API:** http://localhost:8000/docs
3. **Customize:** Adjust parameters in the interface
4. **Deploy:** See `DEPLOYMENT.md` for production
Happy testing! π
|