video / app.py
seawolf2357's picture
Update app.py
5fd7cca verified
raw
history blame
No virus
2.23 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', 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)