Spaces:
Runtime error
Runtime error
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") | |
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}") | |