File size: 4,068 Bytes
8896972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# πŸ€— Hugging Face Dataset Setup Guide

## Overview

This server now uses **Hugging Face Dataset** for persistent storage of device tokens instead of local files. This ensures that device data is preserved across Space restarts and provides better data management.

## πŸš€ Quick Setup

### 1. Generate Hugging Face Token

1. Go to [Hugging Face Settings](https://huggingface.co/settings/tokens)
2. Click "Create new token"
3. Name: `houzou-notification-server`
4. Type: **Write** (required for dataset operations)
5. Copy the token

### 2. Configure Space Secrets

1. Go to your Space Settings β†’ **Secrets and variables**
2. Add these secrets:

```
HF_TOKEN = hf_your_token_here
HF_DATASET_ID = Detomo/houzou-devices  (optional, defaults to this)
```

### 3. Required Secrets Summary

Your Space needs these secrets:

```
FIREBASE_SERVICE_ACCOUNT = {your firebase config JSON}
HF_TOKEN = hf_your_write_token_here
HF_DATASET_ID = Detomo/houzou-devices (optional)
```

## πŸ“Š Dataset Structure

The dataset will be automatically created with:

```
datasets/Detomo/houzou-devices/
β”œβ”€β”€ README.md           # Dataset documentation
└── devices.json        # Device tokens and metadata
```

### devices.json Format:
```json
[
  [
    "device-id-1",
    {
      "token": "fcm_token_here",
      "lastUpdated": "2024-01-01T00:00:00.000Z",
      "platform": "iOS",
      "appVersion": "1.0.0",
      "registered": true
    }
  ]
]
```

## πŸ”§ Features

### βœ… Automatic Dataset Management
- Creates dataset if it doesn't exist
- Handles permissions automatically
- Provides fallback to in-memory storage

### βœ… Persistent Storage
- Device tokens survive Space restarts
- Automatic backup to HF dataset
- Version control for device data

### βœ… Error Handling
- Graceful fallback if HF API is unavailable
- Detailed logging for troubleshooting
- Continues operation even if dataset access fails

## πŸ” Verification

### Check Dataset Status
```bash
curl https://your-space-url.hf.space/
```

Response includes:
```json
{
  "message": "Houzou Medical Notification Server",
  "status": "running",
  "hfDataset": {
    "enabled": true,
    "datasetId": "Detomo/houzou-devices",
    "hasToken": true
  },
  "deviceCount": 5
}
```

### View Dataset
1. Go to: https://huggingface.co/datasets/Detomo/houzou-devices
2. Check `devices.json` for your device data

## πŸ› Troubleshooting

### Dataset Not Created
- Verify `HF_TOKEN` has **Write** permissions
- Check Space logs for error messages
- Ensure token is valid and not expired

### Data Not Saving
- Check if HF API is responding
- Verify dataset permissions
- Server will continue with in-memory storage as fallback

### Common Errors

**403 Forbidden**
- Token doesn't have write permissions
- Token is expired or invalid

**404 Not Found**
- Dataset doesn't exist (will be auto-created)
- Wrong dataset ID

**Rate Limiting**
- HF API has rate limits
- Server will retry automatically

## πŸ”„ Migration from File Storage

If you're migrating from file-based storage:

1. **Backup existing data**: Download your current `devices.json`
2. **Set up HF dataset**: Follow steps above
3. **Deploy updated server**: The server will automatically use HF dataset
4. **Verify**: Check that devices are loaded correctly

## πŸ“‹ Environment Variables

```bash
# Required
HF_TOKEN=hf_your_write_token_here
FIREBASE_SERVICE_ACCOUNT={"type":"service_account",...}

# Optional
HF_DATASET_ID=Detomo/houzou-devices
PORT=7860
NODE_ENV=production
```

## 🎯 Benefits

- **βœ… Persistent**: Data survives Space restarts
- **βœ… Reliable**: Automatic fallback mechanisms
- **βœ… Scalable**: No file permission issues
- **βœ… Traceable**: Version history in dataset
- **βœ… Secure**: Tokens stored in HF infrastructure

## πŸ”— Links

- [Hugging Face Hub API](https://huggingface.co/docs/hub/api)
- [Dataset Management](https://huggingface.co/docs/datasets/)
- [Access Tokens](https://huggingface.co/settings/tokens)

---

**Ready to deploy!** Your notification server will now use HF Dataset for persistent storage. πŸš€