workflow / app.py
triflix's picture
Update app.py
5664a58 verified
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)}