uploads

Log | Files | Refs

commit 9f475c68189b69b8f2f6fbee25b853ec6fd8013f
parent 574dfaea3cd128e0539ba2600466846b67884e8c
Author: Amit Dutta <mail@amit.is-a.dev>
Date:   Mon, 20 Jul 2026 20:07:53 +0530

Create index.py
Diffstat:
Aindex.py | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+), 0 deletions(-)

diff --git a/index.py b/index.py @@ -0,0 +1,56 @@ +import os +import random +import string +from datetime import datetime +from fastapi import FastAPI, File, UploadFile, Form, HTTPException +from huggingface_hub import HfApi + +# Create FastAPI app for Vercel +app = FastAPI(docs_url=None, redoc_url=None) + +# Settings +DATASET_REPO = "notamitgamer/uploads" +HF_TOKEN = os.getenv("HF_TOKEN") + +@app.post("/api/upload") +async def upload_file(file: UploadFile = File(...), custom_id: str = Form(None)): + if not HF_TOKEN: + raise HTTPException(status_code=500, detail="HF_TOKEN environment variable is not set") + + try: + ext = os.path.splitext(file.filename)[1] + + # Generate unique code if not provided + if not custom_id: + timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") + random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=6)) + item_id = f"{timestamp}-{random_str}{ext}" + else: + item_id = custom_id + if ext and not item_id.endswith(ext): + item_id += ext + + content = await file.read() + + # Vercel allows writing temporary files to /tmp/ + temp_path = f"/tmp/{item_id}" + with open(temp_path, "wb") as f: + f.write(content) + + # Push to Hugging Face Dataset + api = HfApi() + api.upload_file( + path_or_fileobj=temp_path, + path_in_repo=item_id, + repo_id=DATASET_REPO, + repo_type="dataset", + token=HF_TOKEN + ) + + # Cleanup tmp file + os.remove(temp_path) + + return {"item_id": item_id} + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e))
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror