Spaces:
Sleeping
Sleeping
mystic_CBK
commited on
Commit
·
7041f6d
1
Parent(s):
79c5498
Implement Direct HF Loading Strategy: Load ECG-FM model directly from wanglab/ecg-fm repository to work within 1GB limit
Browse files- Dockerfile +6 -4
- HF_LOADING_STRATEGY.md +161 -0
- server.py +47 -23
Dockerfile
CHANGED
|
@@ -7,8 +7,10 @@ ENV DEBIAN_FRONTEND=noninteractive
|
|
| 7 |
# Install system dependencies
|
| 8 |
RUN apt-get update && apt-get install -y --no-install-recommends git build-essential && rm -rf /var/lib/apt/lists/*
|
| 9 |
|
| 10 |
-
# Create app user
|
| 11 |
-
RUN useradd --create-home --shell /bin/bash app &&
|
|
|
|
|
|
|
| 12 |
|
| 13 |
WORKDIR /app
|
| 14 |
|
|
@@ -29,8 +31,8 @@ RUN git clone https://github.com/Jwoo5/fairseq-signals.git && \
|
|
| 29 |
pip install --editable ./ --no-build-isolation && \
|
| 30 |
cd ..
|
| 31 |
|
| 32 |
-
# Copy application files (updated 2025-08-25 12:
|
| 33 |
-
# Build trigger attempt #
|
| 34 |
COPY . .
|
| 35 |
|
| 36 |
# Switch to app user
|
|
|
|
| 7 |
# Install system dependencies
|
| 8 |
RUN apt-get update && apt-get install -y --no-install-recommends git build-essential && rm -rf /var/lib/apt/lists/*
|
| 9 |
|
| 10 |
+
# Create app user with optimized cache directories for HF loading strategy
|
| 11 |
+
RUN useradd --create-home --shell /bin/bash app && \
|
| 12 |
+
mkdir -p /app/.cache/huggingface /app/.cache/transformers /app/.config/matplotlib && \
|
| 13 |
+
chown -R app:app /app
|
| 14 |
|
| 15 |
WORKDIR /app
|
| 16 |
|
|
|
|
| 31 |
pip install --editable ./ --no-build-isolation && \
|
| 32 |
cd ..
|
| 33 |
|
| 34 |
+
# Copy application files (updated 2025-08-25 12:45 UTC - Direct HF Loading Strategy)
|
| 35 |
+
# Build trigger attempt #6 - Direct HF model loading implementation
|
| 36 |
COPY . .
|
| 37 |
|
| 38 |
# Switch to app user
|
HF_LOADING_STRATEGY.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🚀 ECG-FM API: Direct HF Loading Strategy
|
| 2 |
+
|
| 3 |
+
## **Overview**
|
| 4 |
+
|
| 5 |
+
This ECG-FM API uses a **Direct HF Loading Strategy** to work within Hugging Face Spaces' 1GB limit while maintaining full model performance.
|
| 6 |
+
|
| 7 |
+
## **🎯 The Problem**
|
| 8 |
+
|
| 9 |
+
- **ECG-FM Model Size**: ~1.09 GB
|
| 10 |
+
- **HF Spaces Free Limit**: 1 GB
|
| 11 |
+
- **Traditional Approach**: Store weights locally ❌ (exceeds limit)
|
| 12 |
+
|
| 13 |
+
## **💡 The Solution**
|
| 14 |
+
|
| 15 |
+
**Load the model directly from the official repository at runtime:**
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
# Instead of storing weights locally
|
| 19 |
+
from huggingface_hub import hf_hub_download
|
| 20 |
+
|
| 21 |
+
# Download directly from official repo
|
| 22 |
+
checkpoint = hf_hub_download(
|
| 23 |
+
repo_id="wanglab/ecg-fm",
|
| 24 |
+
filename="mimic_iv_ecg_physionet_pretrained.pt"
|
| 25 |
+
)
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## **✅ Benefits**
|
| 29 |
+
|
| 30 |
+
1. **No Local Storage**: Works within 1GB limit
|
| 31 |
+
2. **Always Updated**: Uses latest official weights
|
| 32 |
+
3. **Full Performance**: No quantization or compression
|
| 33 |
+
4. **Elegant Solution**: No model modification needed
|
| 34 |
+
5. **Scalable**: Clear upgrade path to Pro tier
|
| 35 |
+
|
| 36 |
+
## **🔧 How It Works**
|
| 37 |
+
|
| 38 |
+
### **Phase 1: Cold Start (First Request)**
|
| 39 |
+
```
|
| 40 |
+
User Request → Download Model (2-5 min) → Cache → Inference
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### **Phase 2: Cached (Subsequent Requests)**
|
| 44 |
+
```
|
| 45 |
+
User Request → Load from Cache → Fast Inference
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
### **Phase 3: Space Sleep (After 15 min idle)**
|
| 49 |
+
```
|
| 50 |
+
Space Sleeps → Model Cleared → Next Request = Cold Start
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
## **📊 Performance Characteristics**
|
| 54 |
+
|
| 55 |
+
| Scenario | Time | Notes |
|
| 56 |
+
|----------|------|-------|
|
| 57 |
+
| **Cold Start** | 2-5 minutes | First request after deployment |
|
| 58 |
+
| **Cached** | 15-30 seconds | Normal inference time |
|
| 59 |
+
| **After Sleep** | 2-5 minutes | Space wakes up from idle |
|
| 60 |
+
|
| 61 |
+
## **🚀 Scaling Path**
|
| 62 |
+
|
| 63 |
+
### **Phase 1: Free Tier (Current)**
|
| 64 |
+
- ✅ **Working API** within 1GB limit
|
| 65 |
+
- ⚠️ **Slow cold start** (2-5 min)
|
| 66 |
+
- ⚠️ **CPU only** (15-30 sec inference)
|
| 67 |
+
- ⚠️ **Sleeps after 15 min** idle
|
| 68 |
+
|
| 69 |
+
### **Phase 2: Pro Tier ($9/month)**
|
| 70 |
+
- ✅ **GPU acceleration** (2-5 sec inference)
|
| 71 |
+
- ✅ **Always-on** (no sleep, no cold start)
|
| 72 |
+
- ✅ **50GB limit** (could store weights locally)
|
| 73 |
+
|
| 74 |
+
### **Phase 3: Production**
|
| 75 |
+
- ✅ **Dedicated endpoints** (always-on)
|
| 76 |
+
- ✅ **Custom infrastructure** (full control)
|
| 77 |
+
- ✅ **Load balancing** (multiple instances)
|
| 78 |
+
|
| 79 |
+
## **💾 Caching Strategy**
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
# Persistent cache directory
|
| 83 |
+
cache_dir="/app/.cache/huggingface"
|
| 84 |
+
|
| 85 |
+
# Model will be cached here
|
| 86 |
+
# Survives container restarts
|
| 87 |
+
# Faster reloads after sleep
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
## **🔍 Technical Implementation**
|
| 91 |
+
|
| 92 |
+
### **Model Loading**
|
| 93 |
+
```python
|
| 94 |
+
def load_model():
|
| 95 |
+
# Download from official repo
|
| 96 |
+
ckpt_path = hf_hub_download(
|
| 97 |
+
repo_id="wanglab/ecg-fm",
|
| 98 |
+
filename="mimic_iv_ecg_physionet_pretrained.pt",
|
| 99 |
+
cache_dir="/app/.cache/huggingface"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Load with fairseq-signals
|
| 103 |
+
model = build_model_from_checkpoint(ckpt_path)
|
| 104 |
+
return model
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### **Error Handling**
|
| 108 |
+
```python
|
| 109 |
+
try:
|
| 110 |
+
model = load_model()
|
| 111 |
+
model_loaded = True
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"Model loading failed: {e}")
|
| 114 |
+
model_loaded = False
|
| 115 |
+
# API runs but inference fails
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## **📋 API Endpoints**
|
| 119 |
+
|
| 120 |
+
- **`/`**: Root with strategy info
|
| 121 |
+
- **`/health`**: Health check with model status
|
| 122 |
+
- **`/info`**: Model information and strategy details
|
| 123 |
+
- **`/predict`**: ECG inference endpoint
|
| 124 |
+
|
| 125 |
+
## **🎯 Use Cases**
|
| 126 |
+
|
| 127 |
+
### **Perfect For:**
|
| 128 |
+
- ✅ **Testing & Development**
|
| 129 |
+
- ✅ **Demo & Prototyping**
|
| 130 |
+
- ✅ **Low-traffic APIs**
|
| 131 |
+
- ✅ **Research & Education**
|
| 132 |
+
|
| 133 |
+
### **Consider Pro Tier For:**
|
| 134 |
+
- ⚠️ **Production APIs**
|
| 135 |
+
- ⚠️ **High-traffic services**
|
| 136 |
+
- ⚠️ **Real-time applications**
|
| 137 |
+
- ⚠️ **Always-on requirements**
|
| 138 |
+
|
| 139 |
+
## **🚨 Limitations & Considerations**
|
| 140 |
+
|
| 141 |
+
1. **Cold Start Delay**: 2-5 minutes for first request
|
| 142 |
+
2. **Sleep Behavior**: Free tier sleeps after 15 min idle
|
| 143 |
+
3. **CPU Performance**: Slower than GPU (15-30 sec vs 2-5 sec)
|
| 144 |
+
4. **Network Dependency**: Requires internet for model download
|
| 145 |
+
|
| 146 |
+
## **🔮 Future Improvements**
|
| 147 |
+
|
| 148 |
+
1. **Model Quantization**: Reduce size for local storage
|
| 149 |
+
2. **Progressive Loading**: Load essential parts first
|
| 150 |
+
3. **Smart Caching**: Pre-load during idle time
|
| 151 |
+
4. **Hybrid Approach**: Cache + direct loading
|
| 152 |
+
|
| 153 |
+
## **📚 References**
|
| 154 |
+
|
| 155 |
+
- [Official ECG-FM Repository](https://huggingface.co/wanglab/ecg-fm)
|
| 156 |
+
- [HF Spaces Documentation](https://huggingface.co/docs/hub/spaces)
|
| 157 |
+
- [fairseq-signals Repository](https://github.com/Jwoo5/fairseq-signals)
|
| 158 |
+
|
| 159 |
+
---
|
| 160 |
+
|
| 161 |
+
**This strategy gives us a working ECG-FM API within HF Spaces constraints while maintaining a clear path to production deployment!** 🎉
|
server.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
ECG-FM API Server with
|
| 4 |
-
|
| 5 |
-
BUILD VERSION: 2025-08-25
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
@@ -78,41 +78,54 @@ except ImportError:
|
|
| 78 |
print(f"❌ Failed to load checkpoint: {e}")
|
| 79 |
raise
|
| 80 |
|
| 81 |
-
# Configuration
|
| 82 |
-
MODEL_REPO =
|
| 83 |
-
CKPT =
|
| 84 |
HF_TOKEN = os.getenv("HF_TOKEN") # optional if repo is public
|
| 85 |
|
| 86 |
class ECGPayload(BaseModel):
|
| 87 |
signal: List[List[float]] # shape: [leads, samples], e.g., [12, 5000]
|
| 88 |
fs: Optional[int] = None # sampling rate (optional)
|
| 89 |
|
| 90 |
-
app = FastAPI(title="ECG-FM API", description="ECG Foundation Model API")
|
| 91 |
|
| 92 |
model = None
|
| 93 |
model_loaded = False
|
| 94 |
|
| 95 |
def load_model():
|
| 96 |
-
|
|
|
|
| 97 |
print(f"📦 fairseq_signals available: {fairseq_available}")
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Use the appropriate model loading method
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
if hasattr(m, 'eval'):
|
| 108 |
m.eval()
|
| 109 |
-
print("✅
|
| 110 |
else:
|
| 111 |
print("⚠️ Model loaded but no eval() method - may be raw checkpoint")
|
| 112 |
|
| 113 |
return m
|
| 114 |
except Exception as e:
|
| 115 |
-
print(f"❌ Error loading model: {e}")
|
| 116 |
print("🔄 Checkpoint format may need adjustment")
|
| 117 |
raise
|
| 118 |
|
|
@@ -128,20 +141,24 @@ def _startup():
|
|
| 128 |
print("🔄 Attempting to continue with fallback mode...")
|
| 129 |
|
| 130 |
try:
|
|
|
|
| 131 |
model = load_model()
|
| 132 |
model_loaded = True
|
| 133 |
-
print("🎉
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
-
print(f"❌ Failed to load model on startup: {e}")
|
| 136 |
print("⚠️ API will run but model inference will fail")
|
| 137 |
model_loaded = False
|
| 138 |
|
| 139 |
@app.get("/")
|
| 140 |
async def root():
|
| 141 |
return {
|
| 142 |
-
"message": "ECG-FM API is running!",
|
| 143 |
"model_loaded": model_loaded,
|
| 144 |
"fairseq_signals_available": fairseq_available,
|
|
|
|
|
|
|
| 145 |
"endpoints": {
|
| 146 |
"health": "/health",
|
| 147 |
"predict": "/predict",
|
|
@@ -154,7 +171,8 @@ async def health_check():
|
|
| 154 |
return {
|
| 155 |
"status": "healthy",
|
| 156 |
"model_loaded": model_loaded,
|
| 157 |
-
"fairseq_signals_available": fairseq_available
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
@app.get("/info")
|
|
@@ -167,7 +185,13 @@ async def model_info():
|
|
| 167 |
"checkpoint": CKPT,
|
| 168 |
"fairseq_signals_available": fairseq_available,
|
| 169 |
"model_type": type(model).__name__,
|
| 170 |
-
"model_has_eval": hasattr(model, 'eval')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
}
|
| 172 |
|
| 173 |
@app.post("/predict")
|
|
@@ -190,7 +214,6 @@ async def predict_ecg(payload: ECGPayload):
|
|
| 190 |
if fairseq_available:
|
| 191 |
# Use fairseq_signals for proper ECG-FM inference
|
| 192 |
print("🚀 Using fairseq_signals for ECG-FM inference")
|
| 193 |
-
# This will use the proper ECG-FM model loading and inference
|
| 194 |
result = model(signal)
|
| 195 |
else:
|
| 196 |
# Fallback to basic PyTorch inference
|
|
@@ -199,18 +222,19 @@ async def predict_ecg(payload: ECGPayload):
|
|
| 199 |
|
| 200 |
# Process results
|
| 201 |
if isinstance(result, dict):
|
| 202 |
-
# Extract relevant information
|
| 203 |
output = {
|
| 204 |
"prediction": result.get('prediction', 'ECG analysis completed'),
|
| 205 |
"confidence": result.get('confidence', 0.8),
|
| 206 |
"features": result.get('features', []),
|
| 207 |
-
"model_type": "ECG-FM (fairseq_signals)" if fairseq_available else "ECG-FM (fallback)"
|
|
|
|
| 208 |
}
|
| 209 |
else:
|
| 210 |
output = {
|
| 211 |
"prediction": "ECG analysis completed",
|
| 212 |
"result_type": str(type(result)),
|
| 213 |
-
"model_type": "ECG-FM (fairseq_signals)" if fairseq_available else "ECG-FM (fallback)"
|
|
|
|
| 214 |
}
|
| 215 |
|
| 216 |
return output
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
ECG-FM API Server with Direct HF Model Loading
|
| 4 |
+
Loads model directly from wanglab/ecg-fm repository
|
| 5 |
+
BUILD VERSION: 2025-08-25 12:45 UTC - Direct HF Loading Strategy
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
|
|
| 78 |
print(f"❌ Failed to load checkpoint: {e}")
|
| 79 |
raise
|
| 80 |
|
| 81 |
+
# Configuration - DIRECT HF LOADING STRATEGY
|
| 82 |
+
MODEL_REPO = "wanglab/ecg-fm" # Official ECG-FM repository
|
| 83 |
+
CKPT = "mimic_iv_ecg_physionet_pretrained.pt" # Official checkpoint
|
| 84 |
HF_TOKEN = os.getenv("HF_TOKEN") # optional if repo is public
|
| 85 |
|
| 86 |
class ECGPayload(BaseModel):
|
| 87 |
signal: List[List[float]] # shape: [leads, samples], e.g., [12, 5000]
|
| 88 |
fs: Optional[int] = None # sampling rate (optional)
|
| 89 |
|
| 90 |
+
app = FastAPI(title="ECG-FM API", description="ECG Foundation Model API - Direct HF Loading")
|
| 91 |
|
| 92 |
model = None
|
| 93 |
model_loaded = False
|
| 94 |
|
| 95 |
def load_model():
|
| 96 |
+
"""Load ECG-FM model directly from official HF repository"""
|
| 97 |
+
print(f"🔄 Loading ECG-FM model directly from {MODEL_REPO}...")
|
| 98 |
print(f"📦 fairseq_signals available: {fairseq_available}")
|
| 99 |
|
| 100 |
try:
|
| 101 |
+
# STRATEGY: Download checkpoint directly from official repo
|
| 102 |
+
# This avoids storing large weights in our HF Space
|
| 103 |
+
print("📥 Downloading checkpoint from official ECG-FM repository...")
|
| 104 |
+
ckpt_path = hf_hub_download(
|
| 105 |
+
repo_id=MODEL_REPO,
|
| 106 |
+
filename=CKPT,
|
| 107 |
+
token=HF_TOKEN,
|
| 108 |
+
cache_dir="/app/.cache/huggingface" # Use persistent cache
|
| 109 |
+
)
|
| 110 |
+
print(f"📁 Checkpoint downloaded to: {ckpt_path}")
|
| 111 |
|
| 112 |
# Use the appropriate model loading method
|
| 113 |
+
if fairseq_available:
|
| 114 |
+
print("🚀 Using fairseq_signals for ECG-FM model loading...")
|
| 115 |
+
m = build_model_from_checkpoint(ckpt_path)
|
| 116 |
+
else:
|
| 117 |
+
print("⚠️ Using fallback PyTorch loading...")
|
| 118 |
+
m = build_model_from_checkpoint(ckpt_path)
|
| 119 |
|
| 120 |
if hasattr(m, 'eval'):
|
| 121 |
m.eval()
|
| 122 |
+
print("✅ ECG-FM model loaded successfully and set to eval mode!")
|
| 123 |
else:
|
| 124 |
print("⚠️ Model loaded but no eval() method - may be raw checkpoint")
|
| 125 |
|
| 126 |
return m
|
| 127 |
except Exception as e:
|
| 128 |
+
print(f"❌ Error loading ECG-FM model: {e}")
|
| 129 |
print("🔄 Checkpoint format may need adjustment")
|
| 130 |
raise
|
| 131 |
|
|
|
|
| 141 |
print("🔄 Attempting to continue with fallback mode...")
|
| 142 |
|
| 143 |
try:
|
| 144 |
+
print("🌐 Starting ECG-FM API with direct HF model loading...")
|
| 145 |
model = load_model()
|
| 146 |
model_loaded = True
|
| 147 |
+
print("🎉 ECG-FM model loaded successfully on startup")
|
| 148 |
+
print("💡 Note: First request may be slow due to model download")
|
| 149 |
except Exception as e:
|
| 150 |
+
print(f"❌ Failed to load ECG-FM model on startup: {e}")
|
| 151 |
print("⚠️ API will run but model inference will fail")
|
| 152 |
model_loaded = False
|
| 153 |
|
| 154 |
@app.get("/")
|
| 155 |
async def root():
|
| 156 |
return {
|
| 157 |
+
"message": "ECG-FM API is running with direct HF model loading!",
|
| 158 |
"model_loaded": model_loaded,
|
| 159 |
"fairseq_signals_available": fairseq_available,
|
| 160 |
+
"model_source": f"{MODEL_REPO}/{CKPT}",
|
| 161 |
+
"strategy": "Direct HF loading - no local weight storage",
|
| 162 |
"endpoints": {
|
| 163 |
"health": "/health",
|
| 164 |
"predict": "/predict",
|
|
|
|
| 171 |
return {
|
| 172 |
"status": "healthy",
|
| 173 |
"model_loaded": model_loaded,
|
| 174 |
+
"fairseq_signals_available": fairseq_available,
|
| 175 |
+
"model_source": f"{MODEL_REPO}/{CKPT}"
|
| 176 |
}
|
| 177 |
|
| 178 |
@app.get("/info")
|
|
|
|
| 185 |
"checkpoint": CKPT,
|
| 186 |
"fairseq_signals_available": fairseq_available,
|
| 187 |
"model_type": type(model).__name__,
|
| 188 |
+
"model_has_eval": hasattr(model, 'eval'),
|
| 189 |
+
"loading_strategy": "Direct HF repository loading",
|
| 190 |
+
"benefits": [
|
| 191 |
+
"No local weight storage",
|
| 192 |
+
"Always uses latest official weights",
|
| 193 |
+
"Works within HF Spaces 1GB limit"
|
| 194 |
+
]
|
| 195 |
}
|
| 196 |
|
| 197 |
@app.post("/predict")
|
|
|
|
| 214 |
if fairseq_available:
|
| 215 |
# Use fairseq_signals for proper ECG-FM inference
|
| 216 |
print("🚀 Using fairseq_signals for ECG-FM inference")
|
|
|
|
| 217 |
result = model(signal)
|
| 218 |
else:
|
| 219 |
# Fallback to basic PyTorch inference
|
|
|
|
| 222 |
|
| 223 |
# Process results
|
| 224 |
if isinstance(result, dict):
|
|
|
|
| 225 |
output = {
|
| 226 |
"prediction": result.get('prediction', 'ECG analysis completed'),
|
| 227 |
"confidence": result.get('confidence', 0.8),
|
| 228 |
"features": result.get('features', []),
|
| 229 |
+
"model_type": "ECG-FM (fairseq_signals)" if fairseq_available else "ECG-FM (fallback)",
|
| 230 |
+
"model_source": f"{MODEL_REPO}/{CKPT}"
|
| 231 |
}
|
| 232 |
else:
|
| 233 |
output = {
|
| 234 |
"prediction": "ECG analysis completed",
|
| 235 |
"result_type": str(type(result)),
|
| 236 |
+
"model_type": "ECG-FM (fairseq_signals)" if fairseq_available else "ECG-FM (fallback)",
|
| 237 |
+
"model_source": f"{MODEL_REPO}/{CKPT}"
|
| 238 |
}
|
| 239 |
|
| 240 |
return output
|