EmeraldCreator commited on
Commit
d0569d0
·
verified ·
1 Parent(s): 1685be8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -10
main.py CHANGED
@@ -6,28 +6,34 @@ import shutil
6
 
7
  app = FastAPI()
8
 
9
- # Разрешаем твоему сайту подключаться
10
  app.add_middleware(
11
  CORSMiddleware,
12
  allow_origins=["*"],
 
13
  allow_methods=["*"],
14
  allow_headers=["*"],
15
  )
16
 
17
- # НАСТРОЙКИ (EmeraldCreator — это твой ник)
18
  REPO_ID = "EmeraldCreator/BlueTok-Storage"
19
  TOKEN = os.environ.get("HF_TOKEN")
20
 
21
  api = HfApi()
22
 
 
 
 
 
23
  @app.post("/upload")
24
  async def upload_video(file: UploadFile = File(...)):
25
- temp_path = file.filename
26
- with open(temp_path, "wb") as buffer:
27
- shutil.copyfileobj(file.file, buffer)
28
-
29
  try:
30
- # Загружаем в датасет
 
 
 
 
31
  api.upload_file(
32
  path_or_fileobj=temp_path,
33
  path_in_repo=f"videos/{file.filename}",
@@ -36,17 +42,17 @@ async def upload_video(file: UploadFile = File(...)):
36
  token=TOKEN
37
  )
38
 
39
- # Ссылка на файл в датасете
40
  raw_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/videos/{file.filename}"
41
- os.remove(temp_path)
42
  return {"url": raw_url}
43
  except Exception as e:
44
  return {"error": str(e)}
 
 
 
45
 
46
  @app.get("/videos")
47
  async def get_videos():
48
  try:
49
- # Список всех файлов в датасете
50
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
51
  video_urls = [
52
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f}"
 
6
 
7
  app = FastAPI()
8
 
9
+ # ГЛОБАЛЬНЫЕ НАСТРОЙКИ ДОСТУПА (CORS)
10
  app.add_middleware(
11
  CORSMiddleware,
12
  allow_origins=["*"],
13
+ allow_credentials=True,
14
  allow_methods=["*"],
15
  allow_headers=["*"],
16
  )
17
 
18
+ # НАСТРОЙКИ ХРАНИЛИЩА
19
  REPO_ID = "EmeraldCreator/BlueTok-Storage"
20
  TOKEN = os.environ.get("HF_TOKEN")
21
 
22
  api = HfApi()
23
 
24
+ @app.get("/")
25
+ def read_root():
26
+ return {"status": "BlueTok Server Online"}
27
+
28
  @app.post("/upload")
29
  async def upload_video(file: UploadFile = File(...)):
30
+ temp_path = f"temp_{file.filename}"
 
 
 
31
  try:
32
+ # Временное сохранение
33
+ with open(temp_path, "wb") as buffer:
34
+ shutil.copyfileobj(file.file, buffer)
35
+
36
+ # Загрузка в Dataset
37
  api.upload_file(
38
  path_or_fileobj=temp_path,
39
  path_in_repo=f"videos/{file.filename}",
 
42
  token=TOKEN
43
  )
44
 
 
45
  raw_url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/videos/{file.filename}"
 
46
  return {"url": raw_url}
47
  except Exception as e:
48
  return {"error": str(e)}
49
+ finally:
50
+ if os.path.exists(temp_path):
51
+ os.remove(temp_path)
52
 
53
  @app.get("/videos")
54
  async def get_videos():
55
  try:
 
56
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
57
  video_urls = [
58
  f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f}"