DavidHospinal
commited on
Commit
·
618d646
1
Parent(s):
832010a
Deploy complete PERI BERT model with Git LFS
Browse files- .gitattributes +2 -0
- README.md +247 -10
- model/model.safetensors +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model/*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
model/*.bin filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,12 +1,249 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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BERT Classifier - HuggingFace Space
|
| 2 |
+
|
| 3 |
+
API REST para clasificación de arquetipos éticos en reflexiones sobre IA usando BERT fine-tuneado.
|
| 4 |
+
|
| 5 |
+
## 📋 Descripción
|
| 6 |
+
|
| 7 |
+
Este espacio proporciona una API FastAPI para clasificar reflexiones éticas sobre IA en 5 arquetipos PERI:
|
| 8 |
+
|
| 9 |
+
1. **Tecnócrata Optimizador** - Confía en la eficiencia de sistemas automatizados
|
| 10 |
+
2. **Humanista Crítico** - Prioriza el bienestar humano y cuestiona sesgos
|
| 11 |
+
3. **Pragmático Equilibrado** - Busca balance entre innovación y humanidad
|
| 12 |
+
4. **Visionario Adaptativo** - Abraza la transformación tecnológica
|
| 13 |
+
5. **Escéptico Conservador** - Postura cautelosa hacia la IA
|
| 14 |
+
|
| 15 |
+
## 🚀 Características
|
| 16 |
+
|
| 17 |
+
- **MC Dropout**: Uncertainty quantification para cada predicción
|
| 18 |
+
- **Batch Processing**: Clasificación de múltiples textos simultáneamente
|
| 19 |
+
- **FastAPI Docs**: Documentación interactiva automática
|
| 20 |
+
- **CORS Enabled**: Listo para integración desde cualquier frontend
|
| 21 |
+
- **Métricas detalladas**: Confidence, uncertainty, top-3 predictions
|
| 22 |
+
|
| 23 |
+
## 🔧 Endpoints
|
| 24 |
+
|
| 25 |
+
### `GET /`
|
| 26 |
+
Documentación interactiva Swagger UI
|
| 27 |
+
|
| 28 |
+
### `GET /health`
|
| 29 |
+
Health check del servicio
|
| 30 |
+
```json
|
| 31 |
+
{
|
| 32 |
+
"status": "healthy",
|
| 33 |
+
"model_loaded": true,
|
| 34 |
+
"device": "cuda",
|
| 35 |
+
"timestamp": 1234567890.123
|
| 36 |
+
}
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
### `GET /info`
|
| 40 |
+
Información del modelo
|
| 41 |
+
```json
|
| 42 |
+
{
|
| 43 |
+
"model_name": "bert-base-multilingual-cased (fine-tuned)",
|
| 44 |
+
"num_classes": 5,
|
| 45 |
+
"max_length": 512,
|
| 46 |
+
"device": "cuda",
|
| 47 |
+
"mc_dropout_samples": 10,
|
| 48 |
+
"archetypes": [...]
|
| 49 |
+
}
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### `POST /predict`
|
| 53 |
+
Clasificar una reflexión individual
|
| 54 |
+
|
| 55 |
+
**Request:**
|
| 56 |
+
```json
|
| 57 |
+
{
|
| 58 |
+
"text": "La automatización mediante IA representa...",
|
| 59 |
+
"use_mc_dropout": true
|
| 60 |
+
}
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
**Response:**
|
| 64 |
+
```json
|
| 65 |
+
{
|
| 66 |
+
"archetype": {
|
| 67 |
+
"id": "TECNOCRATA_OPTIMIZADOR",
|
| 68 |
+
"name": "Tecnócrata Optimizador",
|
| 69 |
+
"description": "Confía en la eficiencia y objetividad..."
|
| 70 |
+
},
|
| 71 |
+
"confidence": 0.87,
|
| 72 |
+
"uncertainty": 0.23,
|
| 73 |
+
"top3_predictions": [
|
| 74 |
+
{
|
| 75 |
+
"archetype_id": "TECNOCRATA_OPTIMIZADOR",
|
| 76 |
+
"archetype_name": "Tecnócrata Optimizador",
|
| 77 |
+
"probability": 0.87
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"archetype_id": "PRAGMATICO_EQUILIBRADO",
|
| 81 |
+
"archetype_name": "Pragmático Equilibrado",
|
| 82 |
+
"probability": 0.08
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"archetype_id": "VISIONARIO_ADAPTATIVO",
|
| 86 |
+
"archetype_name": "Visionario Adaptativo",
|
| 87 |
+
"probability": 0.03
|
| 88 |
+
}
|
| 89 |
+
],
|
| 90 |
+
"inference_time_ms": 245.6,
|
| 91 |
+
"method": "bert-mc-dropout"
|
| 92 |
+
}
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
### `POST /predict-batch`
|
| 96 |
+
Clasificar múltiples reflexiones (máx 50)
|
| 97 |
+
|
| 98 |
+
**Request:**
|
| 99 |
+
```json
|
| 100 |
+
{
|
| 101 |
+
"texts": ["Reflexión 1...", "Reflexión 2...", "..."],
|
| 102 |
+
"use_mc_dropout": true
|
| 103 |
+
}
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
**Response:**
|
| 107 |
+
```json
|
| 108 |
+
{
|
| 109 |
+
"predictions": [...],
|
| 110 |
+
"total_inference_time_ms": 1234.5
|
| 111 |
+
}
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
## 🛠️ Uso desde JavaScript/TypeScript
|
| 115 |
+
|
| 116 |
+
```typescript
|
| 117 |
+
// Clasificar una reflexión
|
| 118 |
+
async function classifyReflection(text: string) {
|
| 119 |
+
const response = await fetch('https://your-space.hf.space/predict', {
|
| 120 |
+
method: 'POST',
|
| 121 |
+
headers: {
|
| 122 |
+
'Content-Type': 'application/json',
|
| 123 |
+
},
|
| 124 |
+
body: JSON.stringify({
|
| 125 |
+
text: text,
|
| 126 |
+
use_mc_dropout: true
|
| 127 |
+
})
|
| 128 |
+
});
|
| 129 |
+
|
| 130 |
+
const result = await response.json();
|
| 131 |
+
console.log('Arquetipo:', result.archetype.name);
|
| 132 |
+
console.log('Confianza:', result.confidence);
|
| 133 |
+
console.log('Incertidumbre:', result.uncertainty);
|
| 134 |
+
return result;
|
| 135 |
+
}
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
## 📦 Deployment en HuggingFace Space
|
| 139 |
+
|
| 140 |
+
### Paso 1: Crear Space
|
| 141 |
+
|
| 142 |
+
1. Ir a https://huggingface.co/spaces
|
| 143 |
+
2. Click "Create new Space"
|
| 144 |
+
3. Configurar:
|
| 145 |
+
- **Name**: `peri-bert-classifier`
|
| 146 |
+
- **License**: MIT
|
| 147 |
+
- **Space SDK**: Docker
|
| 148 |
+
- **Hardware**: CPU basic (o T4 small para GPU)
|
| 149 |
+
|
| 150 |
+
### Paso 2: Subir archivos
|
| 151 |
+
|
| 152 |
+
Estructura del repositorio:
|
| 153 |
+
```
|
| 154 |
+
peri-bert-classifier/
|
| 155 |
+
├── app.py # API FastAPI
|
| 156 |
+
├── requirements.txt # Dependencias Python
|
| 157 |
+
├── Dockerfile # Configuración Docker
|
| 158 |
+
├── README.md # Esta documentación
|
| 159 |
+
└── model/ # Modelo BERT fine-tuneado
|
| 160 |
+
├── config.json
|
| 161 |
+
├── pytorch_model.bin
|
| 162 |
+
├── tokenizer_config.json
|
| 163 |
+
└── vocab.txt
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
### Paso 3: Dockerfile
|
| 167 |
+
|
| 168 |
+
```dockerfile
|
| 169 |
+
FROM python:3.10-slim
|
| 170 |
+
|
| 171 |
+
WORKDIR /app
|
| 172 |
+
|
| 173 |
+
# Copiar archivos
|
| 174 |
+
COPY requirements.txt .
|
| 175 |
+
COPY app.py .
|
| 176 |
+
COPY model/ ./model/
|
| 177 |
+
|
| 178 |
+
# Instalar dependencias
|
| 179 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 180 |
+
|
| 181 |
+
# Exponer puerto
|
| 182 |
+
EXPOSE 7860
|
| 183 |
+
|
| 184 |
+
# Comando de inicio
|
| 185 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
### Paso 4: Git push
|
| 189 |
+
|
| 190 |
+
```bash
|
| 191 |
+
# Clonar el space
|
| 192 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/peri-bert-classifier
|
| 193 |
+
cd peri-bert-classifier
|
| 194 |
+
|
| 195 |
+
# Copiar archivos
|
| 196 |
+
cp app.py requirements.txt Dockerfile README.md .
|
| 197 |
+
cp -r ../../../models/peri-bert/best_model ./model
|
| 198 |
+
|
| 199 |
+
# Push
|
| 200 |
+
git add .
|
| 201 |
+
git commit -m "Initial deployment"
|
| 202 |
+
git push
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
## 🧪 Testing Local
|
| 206 |
+
|
| 207 |
+
```bash
|
| 208 |
+
# Instalar dependencias
|
| 209 |
+
pip install -r requirements.txt
|
| 210 |
+
|
| 211 |
+
# Ejecutar servidor
|
| 212 |
+
python app.py
|
| 213 |
+
|
| 214 |
+
# Acceder a:
|
| 215 |
+
# - Docs: http://localhost:7860
|
| 216 |
+
# - Health: http://localhost:7860/health
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
## 📊 Performance
|
| 220 |
+
|
| 221 |
+
- **Latencia (sin GPU)**: ~200-300ms por reflexión
|
| 222 |
+
- **Latencia (con GPU T4)**: ~50-100ms por reflexión
|
| 223 |
+
- **Throughput batch**: ~10 reflexiones/segundo (GPU)
|
| 224 |
+
- **Memoria**: ~2GB RAM + ~1GB VRAM (GPU)
|
| 225 |
+
|
| 226 |
+
## 🔒 Seguridad
|
| 227 |
+
|
| 228 |
+
- Input validation con Pydantic
|
| 229 |
+
- CORS configurado (ajustar origins en producción)
|
| 230 |
+
- Rate limiting recomendado para producción
|
| 231 |
+
- Longitud máxima de texto: 5000 caracteres
|
| 232 |
+
|
| 233 |
+
## 📈 Métricas del Modelo
|
| 234 |
+
|
| 235 |
+
- **Test Accuracy**: ~84%
|
| 236 |
+
- **MC Dropout Accuracy**: ~86%
|
| 237 |
+
- **Mean Uncertainty**: 0.32
|
| 238 |
+
- **F1-Score (macro avg)**: 0.84
|
| 239 |
+
|
| 240 |
+
## 📞 Soporte
|
| 241 |
+
|
| 242 |
+
Para issues o preguntas sobre el modelo:
|
| 243 |
+
- GitHub: [PERI Project](https://github.com/...)
|
| 244 |
+
- Paper: [Ver documentación científica]
|
| 245 |
+
|
| 246 |
---
|
| 247 |
|
| 248 |
+
**Generado con ❤️ para el proyecto PERI**
|
| 249 |
+
**Deep Learning Conference 2025**
|
model/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3fa322af7e353a942ef53d90c6ccd40c1d795777cf31bf9e4b41dd799c0b8382
|
| 3 |
+
size 711452684
|