Spaces:
Sleeping
Sleeping
QnxprU69yCNg8XJ commited on
Commit Β·
0ea8c58
1
Parent(s): 7c4eb7e
Add fallback to librosa features when HeAR model not available
Browse files- Dockerfile +4 -0
- README.md +79 -1
- inference_service.py +27 -16
- requirements.txt +2 -3
Dockerfile
CHANGED
|
@@ -13,6 +13,10 @@ COPY app.py .
|
|
| 13 |
COPY inference_service.py .
|
| 14 |
COPY pneumonia_classifier.joblib .
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
EXPOSE 5000
|
| 17 |
|
| 18 |
CMD ["python", "app.py"]
|
|
|
|
| 13 |
COPY inference_service.py .
|
| 14 |
COPY pneumonia_classifier.joblib .
|
| 15 |
|
| 16 |
+
# Set environment variable for Hugging Face token
|
| 17 |
+
# Pass this at runtime: docker run -e HF_TOKEN="your_token" ...
|
| 18 |
+
ENV HF_TOKEN=""
|
| 19 |
+
|
| 20 |
EXPOSE 5000
|
| 21 |
|
| 22 |
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Pneumonia Space
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
@@ -10,4 +10,82 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: Pneumonia Space
|
| 3 |
+
emoji: π«
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π« Pneumonia Risk Assessment API
|
| 14 |
+
|
| 15 |
+
AI-powered API for assessing pneumonia risk from respiratory audio recordings.
|
| 16 |
+
|
| 17 |
+
## π Features
|
| 18 |
+
|
| 19 |
+
- **HeAR Model Integration**: Uses Google's Health Acoustic Representations model
|
| 20 |
+
- **Risk Scoring**: Provides probability-based risk assessment (not diagnostic)
|
| 21 |
+
- **Fallback System**: Uses librosa-based features if HeAR model unavailable
|
| 22 |
+
- **REST API**: Simple Flask endpoint for audio file uploads
|
| 23 |
+
|
| 24 |
+
## π Setup
|
| 25 |
+
|
| 26 |
+
### Hugging Face Authentication
|
| 27 |
+
|
| 28 |
+
The HeAR model requires Hugging Face authentication. Set your token as an environment variable:
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
export HF_TOKEN="your_huggingface_token_here"
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
Or login using the CLI:
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
huggingface-cli login
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
Get your token from: https://huggingface.co/settings/tokens
|
| 41 |
+
|
| 42 |
+
### Running Locally
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
# Install dependencies
|
| 46 |
+
pip install -r requirements.txt
|
| 47 |
+
|
| 48 |
+
# Run the application
|
| 49 |
+
python app.py
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Using Docker
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
# Build the image
|
| 56 |
+
docker build -t pneumonia-api .
|
| 57 |
+
|
| 58 |
+
# Run with HF token
|
| 59 |
+
docker run -p 5000:5000 -e HF_TOKEN="your_token" pneumonia-api
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## π‘ API Usage
|
| 63 |
+
|
| 64 |
+
**Endpoint**: `POST /predict_pneumonia`
|
| 65 |
+
|
| 66 |
+
**Request**: Multipart form data with `audio_file`
|
| 67 |
+
|
| 68 |
+
**Response**:
|
| 69 |
+
```json
|
| 70 |
+
{
|
| 71 |
+
"filename": "recording.wav",
|
| 72 |
+
"pneumonia_risk_score": 0.7234,
|
| 73 |
+
"risk_level": "High",
|
| 74 |
+
"note": "This is an AI assessment, not a medical diagnosis. Consult a healthcare professional."
|
| 75 |
+
}
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
**Example with curl**:
|
| 79 |
+
```bash
|
| 80 |
+
curl -X POST -F "audio_file=@recording.wav" http://localhost:5000/predict_pneumonia
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## β οΈ Disclaimer
|
| 84 |
+
|
| 85 |
+
This tool provides risk assessment scores, not medical diagnoses. Always consult healthcare professionals for medical decisions.
|
| 86 |
+
|
| 87 |
+
## π License
|
| 88 |
+
|
| 89 |
+
MIT License
|
| 90 |
+
|
| 91 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
inference_service.py
CHANGED
|
@@ -9,14 +9,14 @@ import warnings
|
|
| 9 |
warnings.filterwarnings("ignore", category=UserWarning, module="soundfile")
|
| 10 |
warnings.filterwarnings("ignore", module="librosa")
|
| 11 |
|
| 12 |
-
# Try to import
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
except ImportError:
|
| 18 |
-
|
| 19 |
-
print("
|
| 20 |
|
| 21 |
# HeAR Parameters
|
| 22 |
SAMPLE_RATE = 16000
|
|
@@ -36,23 +36,34 @@ def create_dummy_audio(filename='dummy_audio.wav', duration=5, sr=SAMPLE_RATE):
|
|
| 36 |
|
| 37 |
def load_hear_model():
|
| 38 |
"""
|
| 39 |
-
Try to load HeAR model
|
|
|
|
| 40 |
"""
|
| 41 |
-
if not
|
| 42 |
-
print("
|
| 43 |
return None
|
| 44 |
|
| 45 |
-
print("
|
| 46 |
try:
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
infer_fn = loaded_model.signatures["serving_default"]
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return infer_fn
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"
|
| 56 |
print("Falling back to librosa-based feature extraction.")
|
| 57 |
return None
|
| 58 |
|
|
|
|
| 9 |
warnings.filterwarnings("ignore", category=UserWarning, module="soundfile")
|
| 10 |
warnings.filterwarnings("ignore", module="librosa")
|
| 11 |
|
| 12 |
+
# Try to import huggingface_hub for HeAR model
|
| 13 |
try:
|
| 14 |
+
from huggingface_hub import from_pretrained_keras
|
| 15 |
+
from huggingface_hub.utils import HfFolder
|
| 16 |
+
HF_AVAILABLE = True
|
| 17 |
except ImportError:
|
| 18 |
+
HF_AVAILABLE = False
|
| 19 |
+
print("Hugging Face Hub not available. Will use librosa-based features.")
|
| 20 |
|
| 21 |
# HeAR Parameters
|
| 22 |
SAMPLE_RATE = 16000
|
|
|
|
| 36 |
|
| 37 |
def load_hear_model():
|
| 38 |
"""
|
| 39 |
+
Try to load HeAR model from Hugging Face Hub.
|
| 40 |
+
If not available or authentication fails, return None and use fallback feature extraction.
|
| 41 |
"""
|
| 42 |
+
if not HF_AVAILABLE:
|
| 43 |
+
print("Hugging Face Hub not available. Using librosa-based feature extraction.")
|
| 44 |
return None
|
| 45 |
|
| 46 |
+
print("Loading HeAR model from Hugging Face Hub...")
|
| 47 |
try:
|
| 48 |
+
# Check if HF token is available (from env var or HF cache)
|
| 49 |
+
token = os.environ.get("HF_TOKEN") or HfFolder.get_token()
|
| 50 |
+
if token is None:
|
| 51 |
+
print("Warning: No Hugging Face token found.")
|
| 52 |
+
print("Set HF_TOKEN environment variable or login with 'huggingface-cli login'")
|
| 53 |
+
print("Falling back to librosa-based feature extraction.")
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
# Load the model directly from Hugging Face Hub
|
| 57 |
+
loaded_model = from_pretrained_keras("google/hear", use_auth_token=token)
|
| 58 |
infer_fn = loaded_model.signatures["serving_default"]
|
| 59 |
+
|
| 60 |
+
print(f"HeAR Model loaded successfully!")
|
| 61 |
+
print(f"Sample Rate: {SAMPLE_RATE} Hz")
|
| 62 |
+
print(f"Clip Duration: {CLIP_DURATION} seconds")
|
| 63 |
+
print(f"Clip Length: {CLIP_LENGTH} samples")
|
| 64 |
return infer_fn
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"Error loading HeAR model: {e}")
|
| 67 |
print("Falling back to librosa-based feature extraction.")
|
| 68 |
return None
|
| 69 |
|
requirements.txt
CHANGED
|
@@ -4,7 +4,6 @@ librosa
|
|
| 4 |
joblib
|
| 5 |
soundfile
|
| 6 |
scikit-learn
|
| 7 |
-
#
|
|
|
|
| 8 |
tensorflow>=2.12.0
|
| 9 |
-
tensorflow_hub
|
| 10 |
-
kagglehub
|
|
|
|
| 4 |
joblib
|
| 5 |
soundfile
|
| 6 |
scikit-learn
|
| 7 |
+
# For HeAR model support
|
| 8 |
+
huggingface_hub>=0.20.0
|
| 9 |
tensorflow>=2.12.0
|
|
|
|
|
|