File size: 5,854 Bytes
a9bc8bd 618d646 6c2f9ff 618d646 | 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 | ---
title: PERI BERT Classifier
emoji: 🤖
colorFrom: blue
colorTo: indigo
sdk: docker
app_file: app.py
pinned: false
license: apache-2.0
---
# PERI BERT Classifier
API REST para clasificación de arquetipos éticos en reflexiones sobre IA usando BERT fine-tuneado.
## 📋 Descripción
Este espacio proporciona una API FastAPI para clasificar reflexiones éticas sobre IA en 5 arquetipos PERI:
1. **Tecnócrata Optimizador** - Confía en la eficiencia de sistemas automatizados
2. **Humanista Crítico** - Prioriza el bienestar humano y cuestiona sesgos
3. **Pragmático Equilibrado** - Busca balance entre innovación y humanidad
4. **Visionario Adaptativo** - Abraza la transformación tecnológica
5. **Escéptico Conservador** - Postura cautelosa hacia la IA
## 🚀 Características
- **MC Dropout**: Uncertainty quantification para cada predicción
- **Batch Processing**: Clasificación de múltiples textos simultáneamente
- **FastAPI Docs**: Documentación interactiva automática
- **CORS Enabled**: Listo para integración desde cualquier frontend
- **Métricas detalladas**: Confidence, uncertainty, top-3 predictions
## 🔧 Endpoints
### `GET /`
Documentación interactiva Swagger UI
### `GET /health`
Health check del servicio
```json
{
"status": "healthy",
"model_loaded": true,
"device": "cuda",
"timestamp": 1234567890.123
}
```
### `GET /info`
Información del modelo
```json
{
"model_name": "bert-base-multilingual-cased (fine-tuned)",
"num_classes": 5,
"max_length": 512,
"device": "cuda",
"mc_dropout_samples": 10,
"archetypes": [...]
}
```
### `POST /predict`
Clasificar una reflexión individual
**Request:**
```json
{
"text": "La automatización mediante IA representa...",
"use_mc_dropout": true
}
```
**Response:**
```json
{
"archetype": {
"id": "TECNOCRATA_OPTIMIZADOR",
"name": "Tecnócrata Optimizador",
"description": "Confía en la eficiencia y objetividad..."
},
"confidence": 0.87,
"uncertainty": 0.23,
"top3_predictions": [
{
"archetype_id": "TECNOCRATA_OPTIMIZADOR",
"archetype_name": "Tecnócrata Optimizador",
"probability": 0.87
},
{
"archetype_id": "PRAGMATICO_EQUILIBRADO",
"archetype_name": "Pragmático Equilibrado",
"probability": 0.08
},
{
"archetype_id": "VISIONARIO_ADAPTATIVO",
"archetype_name": "Visionario Adaptativo",
"probability": 0.03
}
],
"inference_time_ms": 245.6,
"method": "bert-mc-dropout"
}
```
### `POST /predict-batch`
Clasificar múltiples reflexiones (máx 50)
**Request:**
```json
{
"texts": ["Reflexión 1...", "Reflexión 2...", "..."],
"use_mc_dropout": true
}
```
**Response:**
```json
{
"predictions": [...],
"total_inference_time_ms": 1234.5
}
```
## 🛠️ Uso desde JavaScript/TypeScript
```typescript
// Clasificar una reflexión
async function classifyReflection(text: string) {
const response = await fetch('https://your-space.hf.space/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: text,
use_mc_dropout: true
})
});
const result = await response.json();
console.log('Arquetipo:', result.archetype.name);
console.log('Confianza:', result.confidence);
console.log('Incertidumbre:', result.uncertainty);
return result;
}
```
## 📦 Deployment en HuggingFace Space
### Paso 1: Crear Space
1. Ir a https://huggingface.co/spaces
2. Click "Create new Space"
3. Configurar:
- **Name**: `peri-bert-classifier`
- **License**: MIT
- **Space SDK**: Docker
- **Hardware**: CPU basic (o T4 small para GPU)
### Paso 2: Subir archivos
Estructura del repositorio:
```
peri-bert-classifier/
├── app.py # API FastAPI
├── requirements.txt # Dependencias Python
├── Dockerfile # Configuración Docker
├── README.md # Esta documentación
└── model/ # Modelo BERT fine-tuneado
├── config.json
├── pytorch_model.bin
├── tokenizer_config.json
└── vocab.txt
```
### Paso 3: Dockerfile
```dockerfile
FROM python:3.10-slim
WORKDIR /app
# Copiar archivos
COPY requirements.txt .
COPY app.py .
COPY model/ ./model/
# Instalar dependencias
RUN pip install --no-cache-dir -r requirements.txt
# Exponer puerto
EXPOSE 7860
# Comando de inicio
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
```
### Paso 4: Git push
```bash
# Clonar el space
git clone https://huggingface.co/spaces/YOUR_USERNAME/peri-bert-classifier
cd peri-bert-classifier
# Copiar archivos
cp app.py requirements.txt Dockerfile README.md .
cp -r ../../../models/peri-bert/best_model ./model
# Push
git add .
git commit -m "Initial deployment"
git push
```
## 🧪 Testing Local
```bash
# Instalar dependencias
pip install -r requirements.txt
# Ejecutar servidor
python app.py
# Acceder a:
# - Docs: http://localhost:7860
# - Health: http://localhost:7860/health
```
## 📊 Performance
- **Latencia (sin GPU)**: ~200-300ms por reflexión
- **Latencia (con GPU T4)**: ~50-100ms por reflexión
- **Throughput batch**: ~10 reflexiones/segundo (GPU)
- **Memoria**: ~2GB RAM + ~1GB VRAM (GPU)
## 🔒 Seguridad
- Input validation con Pydantic
- CORS configurado (ajustar origins en producción)
- Rate limiting recomendado para producción
- Longitud máxima de texto: 5000 caracteres
## 📈 Métricas del Modelo
- **Test Accuracy**: ~84%
- **MC Dropout Accuracy**: ~86%
- **Mean Uncertainty**: 0.32
- **F1-Score (macro avg)**: 0.84
## 📞 Soporte
Para issues o preguntas sobre el modelo:
- GitHub: [PERI Project](https://github.com/...)
- Paper: [Ver documentación científica]
---
**Generado con ❤️ para el proyecto PERI**
**Deep Learning Conference 2025**
|