File size: 4,793 Bytes
8dd058f
 
 
 
 
 
 
 
 
 
 
 
 
9b4e272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Baby Cry Ai
emoji: 😻
colorFrom: yellow
colorTo: indigo
sdk: gradio
sdk_version: 6.3.0
app_file: app.py
pinned: false
license: mit
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
# πŸ‘Ά Baby Cry AI Service

A Flask microservice that analyzes baby cry audio to identify the reason for crying using an ensemble of Hugging Face models.

## 🎯 Features

- **Ensemble Model Approach**: Combines supervised and zero-shot classification for improved accuracy
- **Single Load Architecture**: Models loaded once at startup for optimal performance
- **Docker Ready**: Production-ready containerized deployment
- **REST API**: Simple POST endpoint for audio analysis

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Baby Cry AI Service                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  app.py (Flask API)                                         β”‚
β”‚    └── POST /analyze-cry                                    β”‚
β”‚         └── inference.py                                    β”‚
β”‚              β”œβ”€β”€ Supervised Model (Wiam/baby-cry-*)         β”‚
β”‚              └── Zero-Shot Model (laion/clap-htsat-unfused) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸš€ Quick Start

### Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Run the service
python app.py
```

### Docker

```bash
# Build image
docker build -t baby-cry-ai .

# Run container
docker run -p 5000:5000 baby-cry-ai
```

## πŸ“‘ API Reference

### Health Check

```http
GET /health
```

**Response:**
```json
{
  "status": "healthy",
  "service": "baby-cry-ai"
}
```

### Analyze Cry

```http
POST /analyze-cry
Content-Type: multipart/form-data
```

**Request:**
- `audio`: Audio file (WAV, MP3, OGG, FLAC, M4A, WebM)

**Response:**
```json
{
  "cry_detected": true,
  "top_reason": "hunger",
  "scores": {
    "hunger": 0.45,
    "belly_pain": 0.20,
    "tired": 0.15,
    "discomfort": 0.12,
    "burping": 0.08
  },
  "disclaimer": "AI-generated suggestion, not a medical diagnosis. Please consult a healthcare professional for medical advice."
}
```

### Example Usage

```bash
# Using curl
curl -X POST http://localhost:5000/analyze-cry \
  -F "audio=@baby_cry.wav"

# Using Python requests
import requests

with open("baby_cry.wav", "rb") as f:
    response = requests.post(
        "http://localhost:5000/analyze-cry",
        files={"audio": f}
    )
    print(response.json())
```

## 🏷️ Cry Categories

| Label | Description |
|-------|-------------|
| `hunger` | Baby is hungry (rhythmic "neh" sound) |
| `belly_pain` | Stomach discomfort (sharp, high-pitched) |
| `tired` | Baby needs sleep (heavy, yawning cry) |
| `discomfort` | General discomfort (fussy, whiny) |
| `burping` | Needs to burp (repetitive sounds) |

## 🧠 Models Used

1. **Supervised Model**: `Wiam/baby-cry-classification-finetuned-babycry-v4`
   - Fine-tuned specifically for baby cry classification
   
2. **Zero-Shot Model**: `laion/clap-htsat-unfused`
   - CLAP model for audio-text matching
   - Provides additional context via natural language prompts

## πŸ“ Project Structure

```
baby-cry-ai-service/
β”œβ”€β”€ app.py              # Flask entry point
β”œβ”€β”€ inference.py        # Model loading & inference logic
β”œβ”€β”€ requirements.txt    # Python dependencies
β”œβ”€β”€ Dockerfile          # Container configuration
β”œβ”€β”€ .env.example        # Environment variables template
└── README.md           # Documentation
```

## βš™οΈ Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `PORT` | `5000` | Server port |
| `FLASK_DEBUG` | `false` | Enable debug mode |

## πŸ”§ Production Deployment

For production, the Docker image uses Gunicorn with:
- Single worker (due to model memory requirements)
- 120s timeout for large audio files
- Health check endpoint

```bash
# Production run with custom port
docker run -p 8080:5000 -e PORT=5000 baby-cry-ai
```

## ⚠️ Disclaimer

This service provides AI-generated suggestions only and should **NOT** be used as a substitute for professional medical advice. Always consult with a healthcare professional for concerns about your baby's health.

## πŸ“„ License

MIT License