Spaces:
Sleeping
Sleeping
File size: 2,229 Bytes
612cb39 743e080 0660b1b 2348476 f2d1942 c892817 0660b1b 9f1a5de f2d1942 0660b1b 9e25e78 e2ad603 0660b1b 743e080 0660b1b e2ad603 743e080 5fd7cca 377906c 5fd7cca f2d1942 c892817 743e080 0660b1b 1cd16bc 0660b1b b8bab42 f2d1942 |
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 |
import os
import tempfile
import gradio as gr
from huggingface_hub import HfApi
import logging
# ๋ก๊น
์ค์
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# ํ๊ฒฝ ๋ณ์์์ Hugging Face ํ ํฐ ์ฝ๊ธฐ
hf_token = os.getenv("TOKEN")
if not hf_token:
logging.error("Hugging Face token is not set. Please set the HF_TOKEN environment variable.")
exit()
# Hugging Face API ์ด๊ธฐํ
api = HfApi()
def upload_file_to_hf_space(uploaded_file):
user_id = "seawolf2357"
space_name = "video"
repo_id = f"{user_id}/{space_name}"
# ์์ ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋๋ ํ์ผ ๋ฐ์ดํฐ ์ฐ๊ธฐ
with tempfile.NamedTemporaryFile(delete=True, suffix='.mp4', mode='wb') as tmp_file:
# ์
๋ก๋๋ ํ์ผ์ ๋ฐ์ดํธ ๋ฐ์ดํฐ๋ฅผ ์์ ํ์ผ์ ์ฐ๊ธฐ
if isinstance(uploaded_file, bytes):
tmp_file.write(uploaded_file)
else:
# Gradio๋ก๋ถํฐ ๋ฐ์ ํ์ผ ๋ฐ์ดํฐ๊ฐ bytes๊ฐ ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
data = uploaded_file.read() # ํ์ผ ๊ฐ์ฒด์์ ๋ฐ์ดํธ ๋ฐ์ดํฐ ์ฝ๊ธฐ
tmp_file.write(data)
tmp_file.flush()
file_path = tmp_file.name
logging.info(f"Uploading {file_path} to Hugging Face Spaces")
try:
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=os.path.basename(file_path),
repo_id=repo_id,
token=hf_token,
)
uploaded_file_url = f"https://huggingface.co/spaces/{repo_id}/blob/main/{os.path.basename(file_path)}"
logging.info(f"File uploaded successfully: {uploaded_file_url}")
return uploaded_file_url
except Exception as e:
logging.error(f"Failed to upload file: {e}")
return "Failed to upload file."
# Gradio ์ธํฐํ์ด์ค ์ค์ ๋ฐ ์คํ
iface = gr.Interface(
fn=upload_file_to_hf_space,
inputs=gr.File(label="Upload your MP4 file"),
outputs="text",
title="MP4 File Upload to Hugging Face Spaces",
description="Upload an MP4 file and get its URL in Hugging Face Spaces. Please ensure the file is an MP4 format."
)
iface.launch(debug=True)
|