import os import gradio as gr from huggingface_hub import HfApi # 환경 변수에서 Hugging Face 토큰 읽기 hf_token = os.getenv("HF_TOKEN") # Hugging Face API 초기화 api = HfApi() def upload_file_to_hf_space(uploaded_file): # 사용자 ID와 스페이스 이름 설정 user_id = "seawolf2357" space_name = "video" # 스페이스 이름을 여기에 입력하세요. repo_id = f"{user_id}/{space_name}" # 파일명 설정 및 저장 file_path = uploaded_file.name with open(file_path, "wb") as f: f.write(uploaded_file.read()) # 파일 유형 검사 (MP4 파일인지 확인) if not file_path.endswith('.mp4'): return "Please upload an MP4 file." # Hugging Face Spaces에 파일 업로드 response = api.upload_file( path_or_fileobj=file_path, path_in_repo=os.path.basename(file_path), repo_id=repo_id, token=hf_token, ) # 업로드된 파일의 URL 반환 uploaded_file_url = f"https://huggingface.co/spaces/{repo_id}/blob/main/{os.path.basename(file_path)}" return uploaded_file_url # 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()