File size: 1,598 Bytes
99be6a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5664a58
99be6a0
5664a58
 
 
 
 
99be6a0
 
 
 
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
import os, uuid, asyncio
from pathlib import Path
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.requests import Request
from huggingface_hub import CommitScheduler

UPLOAD_DIR = Path("/tmp/uploadedfiles")
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)

# Direct token and repo usage
HF_TOKEN   = os.environ.get("HF_TOKEN")
BACKUP_REPO = "triflix/database"

# Scheduler for realtime commit (will trigger manually per upload)
scheduler = CommitScheduler(
    repo_id      = BACKUP_REPO,
    repo_type    = "dataset",
    folder_path  = UPLOAD_DIR,
    path_in_repo = "data",
    every        = 60*24*365,  # effectively disables timed auto push
    token        = HF_TOKEN,
)

app = FastAPI(title="Triflix Realtime Backup")
templates = Jinja2Templates(directory="templates")

@app.on_event("shutdown")
def _stop_scheduler():
    scheduler.stop()

@app.get("/", response_class=HTMLResponse)
def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

@app.post("/upload")
async def upload(file: UploadFile = File(...)):
    try:
        dest = UPLOAD_DIR / f"{uuid.uuid4()}_{file.filename}"
        content = await file.read()
        with dest.open("wb") as out:
            out.write(content)

        # push in separate thread
        await asyncio.to_thread(scheduler.trigger)

    except Exception as e:
        raise HTTPException(500, f"Failed to store or push file: {e}")

    return {"status": "stored & pushed", "path": str(dest)}