Spaces:
Sleeping
Sleeping
File size: 1,810 Bytes
dadbf13 20ba4ba dadbf13 20ba4ba dadbf13 d0569d0 dadbf13 d0569d0 dadbf13 d0569d0 20ba4ba dadbf13 d0569d0 dadbf13 d0569d0 20ba4ba d0569d0 20ba4ba d0569d0 dadbf13 20ba4ba dadbf13 | 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from huggingface_hub import HfApi
import os
import shutil
app = FastAPI()
# ГЛОБАЛЬНЫЕ НАСТРОЙКИ ДОСТУПА (CORS)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# НАСТРОЙКИ ХРАНИЛИЩА
REPO_ID = "EmeraldCreator/BlueTok-Storage"
TOKEN = os.environ.get("HF_TOKEN")
api = HfApi()
@app.get("/")
def read_root():
return {"status": "BlueTok Server Online"}
@app.post("/upload")
async def upload_video(file: UploadFile = File(...)):
temp_path = f"temp_{file.filename}"
try:
# Временное сохранение
with open(temp_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Загрузка в Dataset
api.upload_file(
path_or_fileobj=temp_path,
path_in_repo=f"videos/{file.filename}",
repo_id=REPO_ID,
repo_type="dataset",
token=TOKEN
)
raw_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/videos/{file.filename}"
return {"url": raw_url}
except Exception as e:
return {"error": str(e)}
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
@app.get("/videos")
async def get_videos():
try:
files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
video_urls = [
f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f}"
for f in files if f.startswith("videos/") and f.endswith(('.mp4', '.mov', '.avi'))
]
return video_urls
except:
return []
|