Spaces:
Runtime error
Runtime error
File size: 1,227 Bytes
3a15456 b7de95c 58deaee b320cb6 b7de95c b320cb6 3a15456 b7de95c b320cb6 58deaee b320cb6 d42c55e b320cb6 3a15456 b320cb6 3a15456 b320cb6 |
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 |
import os, json, io
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from huggingface_hub import HfApi
# --- Configuration ---
REPO_ID = "vericudebuget/lotofdata"
FILE_NAME = "messages.json"
HF_TOKEN = os.getenv("HF_TOKEN")
api = HfApi(token=HF_TOKEN)
# --- App setup ---
app = FastAPI()
app.mount("/", StaticFiles(directory="website", html=True), name="static")
@app.post("/api/messages")
def upload_messages(payload: list):
"""Accept a list of messages and upload them to the Hub."""
if not isinstance(payload, list):
raise HTTPException(400, detail="Expected a list of messages.")
try:
# Serialize to JSON, then upload as in-memory file
data_bytes = json.dumps(payload, ensure_ascii=False, indent=2).encode("utf-8")
api.upload_file(
path_or_fileobj = io.BytesIO(data_bytes),
path_in_repo = FILE_NAME,
repo_id = REPO_ID,
repo_type = "dataset",
commit_message = "Upload new messages from Space"
)
return {"ok": True, "count": len(payload)}
except Exception as e:
raise HTTPException(500, detail=f"Failed to upload: {e}")
|