| | from fastapi import FastAPI, File, UploadFile |
| | from huggingface_hub import HfApi |
| | import os |
| |
|
| | |
| | app = FastAPI() |
| | api = HfApi() |
| |
|
| | |
| | repo_id = os.getenv("HF_REPO_ID") |
| | token = os.getenv("HF_TOKEN") |
| |
|
| | |
| | @app.post("/upload/") |
| | async def upload_file(folder_name: str, file: UploadFile = File(...)): |
| | file_path = f"/tmp/temp_{file.filename}" |
| | with open(file_path, "wb") as f: |
| | f.write(await file.read()) |
| | |
| | |
| | api.upload_file( |
| | path_or_fileobj=file_path, |
| | path_in_repo=f"{folder_name}/{file.filename}", |
| | repo_id=repo_id, |
| | repo_type="model", |
| | token=token |
| | ) |
| | os.remove(file_path) |
| | return {"message": f"File uploaded as {file.filename} to {folder_name}/{file.filename}"} |