# CALCOEE

Calculadora OEE en Python con ingreso manual e interprete IA para casos en texto libre.

La IA no calcula el OEE. Mistral interpreta el texto, llena campos estructurados y el
motor Python valida y calcula.

## Estructura

```text
app/
  main.py              Rutas web y API
  models.py            Esquemas Pydantic
  oee_calculator.py    Motor deterministico OEE
  ai_interpreter.py    Interprete con Mistral y fallback local
  validators.py        Conversion de formularios y mensajes
  templates/           Vistas HTML
  static/              CSS
deploy/
  apache_calcoee.conf  Reverse proxy Apache2
  calcoee.service      Servicio systemd
tests/
```

## Instalacion local

```bash
cd Python/CALCOEE
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
```

En Windows PowerShell:

```powershell
cd Python\CALCOEE
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
copy .env.example .env
```

Configura `.env`:

```env
MISTRAL_API_KEY=tu_api_key
MISTRAL_MODEL=mistral-small-latest
MISTRAL_TRANSCRIPTION_MODEL=voxtral-mini-latest
ENABLE_LOCAL_FALLBACK=true
```

Ejecucion:

```bash
uvicorn app.main:app --host 0.0.0.0 --port 8000
```

Abrir:

```text
http://localhost:8000
```

## API

Calculo directo:

```bash
curl -X POST http://localhost:8000/api/oee/calculate \
  -H "Content-Type: application/json" \
  -d '{
    "tiempo_turno_min": 480,
    "paradas_planificadas_min": 30,
    "paradas_no_planificadas_min": 45,
    "produccion_total": 920,
    "produccion_buena": 870,
    "ciclo_ideal_seg": 25
  }'
```

Interpretacion IA:

```bash
curl -X POST http://localhost:8000/api/oee/interpret \
  -H "Content-Type: application/json" \
  -d '{
    "text": "La linea trabajo 8 horas, tuvo 30 minutos de refrigerio, 45 minutos de falla, produjo 920 piezas, 870 buenas y el ciclo ideal es 25 segundos."
  }'
```

Transcripcion de audio:

```bash
curl -X POST http://localhost:8000/api/audio/transcribe \
  -F "language=es" \
  -F "audio=@dictado.webm;type=audio/webm"
```

La pantalla `/interpret` usa `MediaRecorder` para grabar audio y envia el archivo al
backend. La transcripcion la realiza Mistral/Voxtral, por lo que no depende del
servicio de voz del navegador. En produccion el microfono requiere HTTPS.

## Despliegue Ubuntu + Apache2

Copiar el proyecto al servidor:

```bash
sudo mkdir -p /opt/calcoee
sudo rsync -av --delete ./ /opt/calcoee/
sudo chown -R www-data:www-data /opt/calcoee
```

Crear entorno:

```bash
cd /opt/calcoee
sudo -u www-data python3 -m venv .venv
sudo -u www-data .venv/bin/pip install -r requirements.txt
sudo -u www-data cp .env.example .env
sudo nano .env
```

Instalar servicio:

```bash
sudo cp deploy/calcoee.service /etc/systemd/system/calcoee.service
sudo systemctl daemon-reload
sudo systemctl enable --now calcoee
sudo systemctl status calcoee
```

Configurar Apache2:

```bash
sudo a2enmod proxy proxy_http headers
sudo cp deploy/apache_calcoee.conf /etc/apache2/sites-available/calcoee.conf
sudo nano /etc/apache2/sites-available/calcoee.conf
sudo a2ensite calcoee
sudo systemctl reload apache2
```

Validar:

```bash
curl http://127.0.0.1:8000/health
curl http://tu-dominio/health
```

## Pruebas

```bash
pytest
```
