# ๐Ÿš€ 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 # 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! ๐ŸŽ‰