Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ import cv2
|
|
| 7 |
import random
|
| 8 |
from PIL import Image
|
| 9 |
import string
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Initialize the Hugging Face API with the token
|
| 12 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
|
@@ -49,49 +52,50 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
|
|
| 49 |
}
|
| 50 |
|
| 51 |
def update_index_file(new_metadata_path):
|
| 52 |
-
|
| 53 |
-
temp_dir = "
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
repo_id="vericudebuget/ok4231",
|
| 60 |
repo_type="space",
|
| 61 |
-
path_in_repo=index_file_path
|
| 62 |
)
|
| 63 |
-
current_paths = existing_content.decode().strip()
|
| 64 |
-
|
| 65 |
-
# If the file exists but is empty, initialize it
|
| 66 |
-
if not current_paths:
|
| 67 |
-
updated_paths = new_metadata_path
|
| 68 |
-
else:
|
| 69 |
-
# Split existing paths and add the new one
|
| 70 |
-
paths_list = current_paths.split(';')
|
| 71 |
-
if new_metadata_path not in paths_list:
|
| 72 |
-
paths_list.append(new_metadata_path)
|
| 73 |
-
updated_paths = ';'.join(paths_list)
|
| 74 |
-
|
| 75 |
-
except Exception as e:
|
| 76 |
-
# If file doesn't exist or can't be loaded, start with just the new path
|
| 77 |
-
updated_paths = new_metadata_path
|
| 78 |
-
|
| 79 |
-
# Create temporary file for updated index
|
| 80 |
-
os.makedirs(temp_dir, exist_ok=True)
|
| 81 |
-
with open(temp_index_path, "w") as f:
|
| 82 |
-
f.write(updated_paths)
|
| 83 |
-
|
| 84 |
-
# Upload updated index file
|
| 85 |
-
api.upload_file(
|
| 86 |
-
path_or_fileobj=temp_index_path,
|
| 87 |
-
path_in_repo=index_file_path,
|
| 88 |
-
repo_id="vericudebuget/ok4231",
|
| 89 |
-
repo_type="space",
|
| 90 |
-
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
os.
|
|
|
|
| 95 |
|
| 96 |
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
|
| 97 |
# Create temp paths
|
|
@@ -99,71 +103,72 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
|
|
| 99 |
if not os.path.exists(temp_dir):
|
| 100 |
os.makedirs(temp_dir)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
if os.path.exists(file_path):
|
| 164 |
-
os.remove(file_path)
|
| 165 |
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# Streamlit app interface
|
| 169 |
st.title("Upload your video")
|
|
@@ -202,6 +207,5 @@ if uploaded_video:
|
|
| 202 |
if metadata:
|
| 203 |
st.success("Upload completed successfully!")
|
| 204 |
st.json(metadata)
|
| 205 |
-
|
| 206 |
else:
|
| 207 |
st.info("Please upload a video file to begin.")
|
|
|
|
| 7 |
import random
|
| 8 |
from PIL import Image
|
| 9 |
import string
|
| 10 |
+
import subprocess
|
| 11 |
+
import glob
|
| 12 |
+
import shutil
|
| 13 |
|
| 14 |
# Initialize the Hugging Face API with the token
|
| 15 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
def update_index_file(new_metadata_path):
|
| 55 |
+
# Create a temporary directory for cloning
|
| 56 |
+
temp_dir = "temp_repo"
|
| 57 |
+
if os.path.exists(temp_dir):
|
| 58 |
+
shutil.rmtree(temp_dir)
|
| 59 |
|
| 60 |
try:
|
| 61 |
+
# Clone the repository
|
| 62 |
+
subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
|
| 63 |
+
|
| 64 |
+
# Find all JSON files in the metadata directory
|
| 65 |
+
metadata_dir = os.path.join(temp_dir, 'metadata')
|
| 66 |
+
json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
|
| 67 |
+
|
| 68 |
+
# Create the paths string
|
| 69 |
+
base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
|
| 70 |
+
paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
|
| 71 |
+
|
| 72 |
+
# Add the new metadata path if it's not already there
|
| 73 |
+
new_metadata_filename = os.path.basename(new_metadata_path)
|
| 74 |
+
new_full_path = f"{base_url}{new_metadata_filename}"
|
| 75 |
+
if new_full_path not in paths:
|
| 76 |
+
paths.append(new_full_path)
|
| 77 |
+
|
| 78 |
+
# Create the index content
|
| 79 |
+
index_content = "{ " + "; ".join(paths) + "; }"
|
| 80 |
+
|
| 81 |
+
# Write to a temporary file
|
| 82 |
+
index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
|
| 83 |
+
os.makedirs(os.path.dirname(index_path), exist_ok=True)
|
| 84 |
+
with open(index_path, 'w') as f:
|
| 85 |
+
f.write(index_content)
|
| 86 |
+
|
| 87 |
+
# Upload the index file
|
| 88 |
+
api.upload_file(
|
| 89 |
+
path_or_fileobj=index_path,
|
| 90 |
+
path_in_repo="metadata/video-index.json",
|
| 91 |
repo_id="vericudebuget/ok4231",
|
| 92 |
repo_type="space",
|
|
|
|
| 93 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
finally:
|
| 96 |
+
# Clean up
|
| 97 |
+
if os.path.exists(temp_dir):
|
| 98 |
+
shutil.rmtree(temp_dir)
|
| 99 |
|
| 100 |
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
|
| 101 |
# Create temp paths
|
|
|
|
| 103 |
if not os.path.exists(temp_dir):
|
| 104 |
os.makedirs(temp_dir)
|
| 105 |
|
| 106 |
+
try:
|
| 107 |
+
# Generate randomized filenames
|
| 108 |
+
video_name = add_random_to_filename(original_video_name)
|
| 109 |
+
video_path = os.path.join(temp_dir, video_name)
|
| 110 |
+
|
| 111 |
+
base_name = os.path.splitext(video_name)[0]
|
| 112 |
+
thumbnail_name = f"{base_name}_thumb.jpg"
|
| 113 |
+
thumbnail_path = os.path.join(temp_dir, thumbnail_name)
|
| 114 |
+
|
| 115 |
+
json_name = f"{base_name}-index.json"
|
| 116 |
+
json_path = os.path.join(temp_dir, json_name)
|
| 117 |
+
|
| 118 |
+
# Write the video content to a file
|
| 119 |
+
with open(video_path, "wb") as f:
|
| 120 |
+
f.write(video_file.read())
|
| 121 |
+
|
| 122 |
+
# Handle thumbnail
|
| 123 |
+
if custom_thumbnail:
|
| 124 |
+
thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
|
| 125 |
+
else:
|
| 126 |
+
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
|
| 127 |
+
|
| 128 |
+
if not thumbnail_extracted:
|
| 129 |
+
st.error("Failed to process thumbnail")
|
| 130 |
+
return None
|
| 131 |
+
|
| 132 |
+
# Upload the video
|
| 133 |
+
video_location = f"videos/{video_name}"
|
| 134 |
+
api.upload_file(
|
| 135 |
+
path_or_fileobj=video_path,
|
| 136 |
+
path_in_repo=video_location,
|
| 137 |
+
repo_id="vericudebuget/ok4231",
|
| 138 |
+
repo_type="space",
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Upload the thumbnail
|
| 142 |
+
thumbnail_location = f"thumbnails/{thumbnail_name}"
|
| 143 |
+
api.upload_file(
|
| 144 |
+
path_or_fileobj=thumbnail_path,
|
| 145 |
+
path_in_repo=thumbnail_location,
|
| 146 |
+
repo_id="vericudebuget/ok4231",
|
| 147 |
+
repo_type="space",
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Generate and upload metadata JSON
|
| 151 |
+
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
|
| 152 |
+
with open(json_path, "w") as f:
|
| 153 |
+
json.dump(metadata, f, indent=2)
|
| 154 |
+
|
| 155 |
+
metadata_location = f"metadata/{json_name}"
|
| 156 |
+
api.upload_file(
|
| 157 |
+
path_or_fileobj=json_path,
|
| 158 |
+
path_in_repo=metadata_location,
|
| 159 |
+
repo_id="vericudebuget/ok4231",
|
| 160 |
+
repo_type="space",
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Update the index file
|
| 164 |
+
update_index_file(metadata_location)
|
| 165 |
+
|
| 166 |
+
return metadata
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
finally:
|
| 169 |
+
# Cleanup temp files
|
| 170 |
+
if os.path.exists(temp_dir):
|
| 171 |
+
shutil.rmtree(temp_dir)
|
| 172 |
|
| 173 |
# Streamlit app interface
|
| 174 |
st.title("Upload your video")
|
|
|
|
| 207 |
if metadata:
|
| 208 |
st.success("Upload completed successfully!")
|
| 209 |
st.json(metadata)
|
|
|
|
| 210 |
else:
|
| 211 |
st.info("Please upload a video file to begin.")
|