Spaces:
Sleeping
Sleeping
Implement persistent storage for student database on HF Spaces
Browse files- PERSISTENT_STORAGE.md +54 -0
- app.py +12 -1
PERSISTENT_STORAGE.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Persistent Storage Setup for Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
This application now uses HF Spaces persistent storage to save student registration data permanently.
|
| 5 |
+
|
| 6 |
+
## How It Works
|
| 7 |
+
|
| 8 |
+
### Automatic Detection
|
| 9 |
+
The app automatically detects if persistent storage is available:
|
| 10 |
+
- **On HF Spaces with storage enabled:** Uses `/data/students.db`
|
| 11 |
+
- **Locally or without storage:** Falls back to `students.db` in project directory
|
| 12 |
+
|
| 13 |
+
### Code Implementation
|
| 14 |
+
```python
|
| 15 |
+
PERSISTENT_DIR = "/data"
|
| 16 |
+
if os.path.exists(PERSISTENT_DIR) and os.access(PERSISTENT_DIR, os.W_OK):
|
| 17 |
+
DB_PATH = os.path.join(PERSISTENT_DIR, "students.db")
|
| 18 |
+
print(f"✅ Using persistent storage: {DB_PATH}")
|
| 19 |
+
else:
|
| 20 |
+
DB_PATH = "students.db"
|
| 21 |
+
print("⚠️ Persistent storage not available")
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Enabling Persistent Storage on HF Spaces
|
| 25 |
+
|
| 26 |
+
### Steps:
|
| 27 |
+
1. Go to your Space: https://huggingface.co/spaces/gladguy/SimpleProject
|
| 28 |
+
2. Click **Settings** (gear icon)
|
| 29 |
+
3. Scroll to **Storage**
|
| 30 |
+
4. Click **Enable persistent storage**
|
| 31 |
+
5. Choose storage size (recommended: at least 1GB)
|
| 32 |
+
6. Restart the Space
|
| 33 |
+
|
| 34 |
+
### Verification
|
| 35 |
+
Check the container logs after enabling storage. You should see:
|
| 36 |
+
```
|
| 37 |
+
✅ Using persistent storage: /data/students.db
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
## Benefits
|
| 41 |
+
- ✅ **Data Persistence:** Student registrations survive Space restarts
|
| 42 |
+
- ✅ **Sleep Mode Safe:** Data retained when Space goes to sleep
|
| 43 |
+
- ✅ **Automatic Fallback:** Works both locally and on HF Spaces
|
| 44 |
+
- ✅ **No Code Changes:** Same code works in both environments
|
| 45 |
+
|
| 46 |
+
## Database Location
|
| 47 |
+
- **HF Spaces (with storage):** `/data/students.db`
|
| 48 |
+
- **Local development:** `./students.db`
|
| 49 |
+
- **HF Spaces (without storage):** `./students.db` (ephemeral, will be wiped)
|
| 50 |
+
|
| 51 |
+
## Important Notes
|
| 52 |
+
- Persistent storage is a **paid feature** on HF Spaces
|
| 53 |
+
- Free tier Spaces will use ephemeral storage
|
| 54 |
+
- Local development always uses local `students.db`
|
app.py
CHANGED
|
@@ -17,7 +17,18 @@ SERPAPI_KEY = os.getenv("SERPAPI_KEY")
|
|
| 17 |
HYPERBOLIC_API_KEY = os.getenv("HYPERBOLIC_API_KEY")
|
| 18 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 19 |
ADMIN_PASSWORD = "BT54iv!@"
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# --- DATABASE FUNCTIONS ---
|
| 23 |
def init_database():
|
|
|
|
| 17 |
HYPERBOLIC_API_KEY = os.getenv("HYPERBOLIC_API_KEY")
|
| 18 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 19 |
ADMIN_PASSWORD = "BT54iv!@"
|
| 20 |
+
|
| 21 |
+
# --- PERSISTENT STORAGE CONFIGURATION ---
|
| 22 |
+
# HF Spaces provides persistent storage at /data
|
| 23 |
+
# Check if persistent storage is available, otherwise use local directory
|
| 24 |
+
PERSISTENT_DIR = "/data"
|
| 25 |
+
if os.path.exists(PERSISTENT_DIR) and os.access(PERSISTENT_DIR, os.W_OK):
|
| 26 |
+
DB_PATH = os.path.join(PERSISTENT_DIR, "students.db")
|
| 27 |
+
print(f"✅ Using persistent storage: {DB_PATH}")
|
| 28 |
+
else:
|
| 29 |
+
DB_PATH = "students.db"
|
| 30 |
+
print(f"⚠️ Persistent storage not available. Using local: {DB_PATH}")
|
| 31 |
+
print("💡 To enable persistence on HF Spaces, go to Settings > Storage > Enable persistent storage")
|
| 32 |
|
| 33 |
# --- DATABASE FUNCTIONS ---
|
| 34 |
def init_database():
|