video / app.py
seawolf2357's picture
Update app.py
377906c verified
raw
history blame
1.93 kB
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') as tmp_file:
try:
tmp_file.write(uploaded_file)
tmp_file.flush()
file_path = tmp_file.name
logging.info(f"Uploading {file_path} to Hugging Face Spaces")
# Hugging Face Spaces에 파일 μ—…λ‘œλ“œ
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)