Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,33 @@ from huggingface_hub import HfApi
|
|
3 |
import os
|
4 |
import json
|
5 |
from datetime import datetime
|
|
|
|
|
6 |
|
7 |
# Initialize the Hugging Face API with the token
|
8 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
9 |
|
10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
return {
|
12 |
"fileName": video_name,
|
13 |
"title": title,
|
@@ -15,35 +37,53 @@ def generate_metadata(video_name, title, description, uploader, file_location):
|
|
15 |
"uploader": uploader,
|
16 |
"uploadTimestamp": datetime.now().isoformat(),
|
17 |
"fileLocation": file_location,
|
|
|
18 |
"views": 0,
|
19 |
"likes": 0
|
20 |
}
|
21 |
|
22 |
def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
|
32 |
# Write the video content to a file
|
33 |
with open(video_path, "wb") as f:
|
34 |
f.write(video_file.read())
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
api.upload_file(
|
39 |
path_or_fileobj=video_path,
|
40 |
-
path_in_repo=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
repo_id="vericudebuget/ok4231",
|
42 |
repo_type="space",
|
43 |
)
|
44 |
|
45 |
# Generate and upload metadata JSON
|
46 |
-
metadata = generate_metadata(video_name, title, description, uploader,
|
47 |
with open(json_path, "w") as f:
|
48 |
json.dump(metadata, f, indent=2)
|
49 |
|
@@ -54,6 +94,11 @@ def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
|
54 |
repo_type="space",
|
55 |
)
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
return metadata
|
58 |
|
59 |
# Streamlit app interface
|
@@ -78,7 +123,7 @@ if uploaded_video:
|
|
78 |
if not title or not uploader:
|
79 |
st.error("Please fill in the title and uploader name.")
|
80 |
else:
|
81 |
-
with st.spinner("Uploading video and
|
82 |
metadata = upload_video_to_hf(
|
83 |
uploaded_video,
|
84 |
uploaded_video.name,
|
@@ -86,8 +131,9 @@ if uploaded_video:
|
|
86 |
description,
|
87 |
uploader
|
88 |
)
|
89 |
-
|
90 |
-
|
|
|
91 |
|
92 |
else:
|
93 |
st.info("Please upload a video file to begin.")
|
|
|
3 |
import os
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
+
import cv2
|
7 |
+
import random
|
8 |
|
9 |
# Initialize the Hugging Face API with the token
|
10 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
11 |
|
12 |
+
def extract_thumbnail(video_path, thumbnail_path):
|
13 |
+
video = cv2.VideoCapture(video_path)
|
14 |
+
|
15 |
+
# Get total number of frames
|
16 |
+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
17 |
+
|
18 |
+
# Choose a random frame
|
19 |
+
random_frame = random.randint(0, total_frames - 1)
|
20 |
+
|
21 |
+
# Set the frame position
|
22 |
+
video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
|
23 |
+
|
24 |
+
# Read the frame
|
25 |
+
success, frame = video.read()
|
26 |
+
if success:
|
27 |
+
cv2.imwrite(thumbnail_path, frame)
|
28 |
+
|
29 |
+
video.release()
|
30 |
+
return success
|
31 |
+
|
32 |
+
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location):
|
33 |
return {
|
34 |
"fileName": video_name,
|
35 |
"title": title,
|
|
|
37 |
"uploader": uploader,
|
38 |
"uploadTimestamp": datetime.now().isoformat(),
|
39 |
"fileLocation": file_location,
|
40 |
+
"thumbnailLocation": thumbnail_location,
|
41 |
"views": 0,
|
42 |
"likes": 0
|
43 |
}
|
44 |
|
45 |
def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
46 |
+
# Create temp paths
|
47 |
+
temp_dir = "temp"
|
48 |
+
if not os.path.exists(temp_dir):
|
49 |
+
os.makedirs(temp_dir)
|
50 |
|
51 |
+
video_path = os.path.join(temp_dir, video_name)
|
52 |
+
thumbnail_name = f"{os.path.splitext(video_name)[0]}_thumb.jpg"
|
53 |
+
thumbnail_path = os.path.join(temp_dir, thumbnail_name)
|
54 |
+
json_name = f"{os.path.splitext(video_name)[0]}-index.json"
|
55 |
+
json_path = os.path.join(temp_dir, json_name)
|
56 |
|
57 |
# Write the video content to a file
|
58 |
with open(video_path, "wb") as f:
|
59 |
f.write(video_file.read())
|
60 |
|
61 |
+
# Extract and save thumbnail
|
62 |
+
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
|
63 |
+
if not thumbnail_extracted:
|
64 |
+
st.error("Failed to extract thumbnail from video")
|
65 |
+
return None
|
66 |
+
|
67 |
+
# Upload the video
|
68 |
+
video_location = f"videos/{video_name}"
|
69 |
api.upload_file(
|
70 |
path_or_fileobj=video_path,
|
71 |
+
path_in_repo=video_location,
|
72 |
+
repo_id="vericudebuget/ok4231",
|
73 |
+
repo_type="space",
|
74 |
+
)
|
75 |
+
|
76 |
+
# Upload the thumbnail
|
77 |
+
thumbnail_location = f"thumbnails/{thumbnail_name}"
|
78 |
+
api.upload_file(
|
79 |
+
path_or_fileobj=thumbnail_path,
|
80 |
+
path_in_repo=thumbnail_location,
|
81 |
repo_id="vericudebuget/ok4231",
|
82 |
repo_type="space",
|
83 |
)
|
84 |
|
85 |
# Generate and upload metadata JSON
|
86 |
+
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
|
87 |
with open(json_path, "w") as f:
|
88 |
json.dump(metadata, f, indent=2)
|
89 |
|
|
|
94 |
repo_type="space",
|
95 |
)
|
96 |
|
97 |
+
# Cleanup temp files
|
98 |
+
for file_path in [video_path, thumbnail_path, json_path]:
|
99 |
+
if os.path.exists(file_path):
|
100 |
+
os.remove(file_path)
|
101 |
+
|
102 |
return metadata
|
103 |
|
104 |
# Streamlit app interface
|
|
|
123 |
if not title or not uploader:
|
124 |
st.error("Please fill in the title and uploader name.")
|
125 |
else:
|
126 |
+
with st.spinner("Uploading video, generating thumbnail and metadata..."):
|
127 |
metadata = upload_video_to_hf(
|
128 |
uploaded_video,
|
129 |
uploaded_video.name,
|
|
|
131 |
description,
|
132 |
uploader
|
133 |
)
|
134 |
+
if metadata:
|
135 |
+
st.success("Upload completed successfully!")
|
136 |
+
st.json(metadata)
|
137 |
|
138 |
else:
|
139 |
st.info("Please upload a video file to begin.")
|