Spaces:
Sleeping
Sleeping
File size: 6,439 Bytes
ff03012 |
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 |
# 🔌 API Usage Guide - Hugging Face Spaces
Sur Hugging Face Spaces, **seul Gradio est exposé publiquement**. L'API FastAPI (port 8000) n'est pas accessible depuis l'extérieur.
**Mais Gradio expose automatiquement une API REST native!** 🎉
## 📡 Accéder à l'API depuis l'extérieur
### Option 1: API Gradio Native (Recommandé)
Gradio expose automatiquement une API REST à l'endpoint `/api/predict`.
#### Python avec `gradio_client`:
```python
from gradio_client import Client
# Remplacez par votre Space URL
client = Client("AI-DrivenTesting/CU1-X")
# Appeler l'API
result = client.predict(
"screenshot.png", # image (filepath or PIL Image)
0.35, # confidence_threshold (float)
2, # thickness (int)
True, # enable_clip (bool)
True, # enable_ocr (bool)
False, # enable_blip (bool)
False, # ocr_only (bool)
"Only image & button", # blip_scope (str)
False, # preprocess (bool)
"RF-DETR Optimized (Recommended)", # preprocess_mode (str)
"standard", # preprocess_preset (str)
api_name="/predict"
)
# Résultat: (annotated_image, summary, detections_json)
annotated_image, summary, detections_json = result
print(detections_json)
```
#### REST API (curl):
```bash
# Pour un Space public
curl -X POST "https://AI-DrivenTesting-CU1-X.hf.space/api/predict" \
-H "Content-Type: application/json" \
-d '{
"data": [
"screenshot.png", # Base64 encoded image or URL
0.35,
2,
true,
true,
false,
false,
"Only image & button",
false,
"RF-DETR Optimized (Recommended)",
"standard"
]
}'
```
**Note:** Pour les images, vous devez soit:
- Utiliser une URL publique vers l'image
- Encoder l'image en base64
- Utiliser `gradio_client` qui gère ça automatiquement
#### REST API avec Python `requests`:
```python
import requests
import base64
from PIL import Image
import io
# Encoder l'image en base64
def image_to_base64(image_path):
with open(image_path, "rb") as f:
return base64.b64encode(f.read()).decode()
# Appeler l'API
url = "https://AI-DrivenTesting-CU1-X.hf.space/api/predict"
image_b64 = image_to_base64("screenshot.png")
response = requests.post(
url,
json={
"data": [
f"data:image/png;base64,{image_b64}",
0.35,
2,
True,
True,
False,
False,
"Only image & button",
False,
"RF-DETR Optimized (Recommended)",
"standard"
]
},
timeout=120
)
result = response.json()
print(result)
```
### Option 2: API FastAPI (Interne uniquement)
L'API FastAPI sur le port 8000 **n'est PAS accessible depuis l'extérieur** du Space HF.
Elle fonctionne uniquement:
- ✅ En local (`python app.py`)
- ✅ Entre les processus internes du Space
- ❌ **PAS depuis l'extérieur du Space**
## 🔑 Authentification
### Spaces Publics
- Aucune authentification requise
- API accessible directement
### Spaces Privés
- Nécessite un token Hugging Face
- Ajoutez le header: `Authorization: Bearer <HF_TOKEN>`
```python
from gradio_client import Client
client = Client(
"AI-DrivenTesting/CU1-X",
hf_token="your_hf_token_here" # Pour les Spaces privés
)
```
## 📊 Paramètres de l'API
| Paramètre | Type | Description | Valeur par défaut |
|-----------|------|-------------|-------------------|
| `image` | file/str | Image à analyser | - |
| `confidence_threshold` | float | Seuil de confiance (0.1-0.9) | 0.35 |
| `thickness` | int | Épaisseur des boîtes (1-6) | 2 |
| `enable_clip` | bool | Activer classification CLIP | False |
| `enable_ocr` | bool | Activer extraction OCR | True |
| `enable_blip` | bool | Activer descriptions BLIP | False |
| `ocr_only` | bool | Mode OCR seul (skip detection) | False |
| `blip_scope` | str | Portée BLIP ("Only image & button" ou "All elements") | "Only image & button" |
| `preprocess` | bool | Activer preprocessing | False |
| `preprocess_mode` | str | Mode preprocessing | "RF-DETR Optimized (Recommended)" |
| `preprocess_preset` | str | Preset preprocessing | "standard" |
## 📝 Format de Réponse
```json
{
"annotated_image": "base64_encoded_image",
"summary": "Markdown summary text",
"detections_json": {
"success": true,
"detections": [...],
"total_detections": 10,
"image_size": {"width": 1080, "height": 1920},
"parameters": {...},
"type_distribution": {...}
}
}
```
## 🚀 Exemples Complets
### Exemple 1: Détection Simple
```python
from gradio_client import Client
client = Client("AI-DrivenTesting/CU1-X")
result = client.predict(
"screenshot.png",
0.35, 2, False, True, False, False, "Only image & button",
False, "RF-DETR Optimized (Recommended)", "standard",
api_name="/predict"
)
annotated_image, summary, detections = result
print(f"Found {detections['total_detections']} elements")
```
### Exemple 2: Détection Complète avec CLIP
```python
result = client.predict(
"screenshot.png",
0.35, 2, True, True, False, False, "Only image & button",
False, "RF-DETR Optimized (Recommended)", "standard",
api_name="/predict"
)
```
### Exemple 3: OCR Seulement
```python
result = client.predict(
"screenshot.png",
0.35, 2, False, True, False, True, "Only image & button",
False, "RF-DETR Optimized (Recommended)", "standard",
api_name="/predict"
)
```
## ⚠️ Limitations HF Spaces
1. **Timeout:** 60 secondes par défaut (peut être augmenté dans Settings)
2. **Mémoire:** Limite selon le hardware choisi
3. **CPU/GPU:** Performance dépend du hardware sélectionné
4. **API FastAPI:** Non accessible depuis l'extérieur
## 🔗 Liens Utiles
- [Gradio Client Docs](https://www.gradio.app/guides/getting-started-with-the-python-client)
- [HF Spaces API Docs](https://huggingface.co/docs/hub/spaces-sdks-gradio#api-tab)
- [HF Authentication](https://huggingface.co/docs/hub/security-tokens)
## 💡 Tips
- Utilisez `gradio_client` pour une meilleure gestion des images
- Pour les gros fichiers, utilisez des URLs publiques
- Activez le preprocessing pour des résultats cohérents sur différents devices
- Mode OCR-only est plus rapide si vous voulez juste le texte
|