Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,40 @@
|
|
1 |
-
!pip install moviepy
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
import streamlit as st
|
6 |
from huggingface_hub import HfApi
|
7 |
import os
|
8 |
import json
|
9 |
from datetime import datetime
|
10 |
-
|
|
|
11 |
from PIL import Image
|
12 |
import io
|
13 |
-
import random
|
14 |
|
15 |
# Initialize the Hugging Face API with the token
|
16 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
17 |
|
18 |
def extract_thumbnail(video_path, thumbnail_path):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
return False
|
43 |
|
44 |
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location):
|
45 |
return {
|
@@ -54,7 +49,7 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
|
|
54 |
"likes": 0
|
55 |
}
|
56 |
|
57 |
-
def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
58 |
# Create temp paths
|
59 |
temp_dir = "temp"
|
60 |
if not os.path.exists(temp_dir):
|
@@ -70,53 +65,52 @@ def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
|
70 |
with open(video_path, "wb") as f:
|
71 |
f.write(video_file.read())
|
72 |
|
73 |
-
#
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
if not thumbnail_extracted:
|
|
|
76 |
return None
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
)
|
87 |
-
|
88 |
-
# Upload the thumbnail
|
89 |
-
thumbnail_location = f"thumbnails/{thumbnail_name}"
|
90 |
-
api.upload_file(
|
91 |
-
path_or_fileobj=thumbnail_path,
|
92 |
-
path_in_repo=thumbnail_location,
|
93 |
-
repo_id="vericudebuget/ok4231",
|
94 |
-
repo_type="space",
|
95 |
-
)
|
96 |
-
|
97 |
-
# Generate and upload metadata JSON
|
98 |
-
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
|
99 |
-
with open(json_path, "w") as f:
|
100 |
-
json.dump(metadata, f, indent=2)
|
101 |
-
|
102 |
-
api.upload_file(
|
103 |
-
path_or_fileobj=json_path,
|
104 |
-
path_in_repo=f"metadata/{json_name}",
|
105 |
-
repo_id="vericudebuget/ok4231",
|
106 |
-
repo_type="space",
|
107 |
-
)
|
108 |
-
|
109 |
-
return metadata
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# Streamlit app interface
|
122 |
st.title("Upload your video")
|
@@ -133,6 +127,9 @@ if uploaded_video:
|
|
133 |
description = st.text_area("Description", placeholder="Enter video description")
|
134 |
uploader = st.text_input("Uploader Name", placeholder="Enter your name")
|
135 |
|
|
|
|
|
|
|
136 |
# Upload button within the form
|
137 |
submit_button = st.form_submit_button("Upload Video")
|
138 |
|
@@ -146,7 +143,8 @@ if uploaded_video:
|
|
146 |
uploaded_video.name,
|
147 |
title,
|
148 |
description,
|
149 |
-
uploader
|
|
|
150 |
)
|
151 |
if metadata:
|
152 |
st.success("Upload completed successfully!")
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from huggingface_hub import HfApi
|
3 |
import os
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
+
import cv2
|
7 |
+
import random
|
8 |
from PIL import Image
|
9 |
import io
|
|
|
10 |
|
11 |
# Initialize the Hugging Face API with the token
|
12 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
13 |
|
14 |
def extract_thumbnail(video_path, thumbnail_path):
|
15 |
+
video = cv2.VideoCapture(video_path)
|
16 |
+
|
17 |
+
# Get total number of frames
|
18 |
+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
19 |
+
|
20 |
+
# Choose a random frame
|
21 |
+
random_frame = random.randint(0, total_frames - 1)
|
22 |
+
|
23 |
+
# Set the frame position
|
24 |
+
video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
|
25 |
+
|
26 |
+
# Read the frame
|
27 |
+
success, frame = video.read()
|
28 |
+
if success:
|
29 |
+
cv2.imwrite(thumbnail_path, frame)
|
30 |
+
|
31 |
+
video.release()
|
32 |
+
return success
|
33 |
+
|
34 |
+
def save_custom_thumbnail(thumbnail_file, thumbnail_path):
|
35 |
+
img = Image.open(thumbnail_file)
|
36 |
+
img.save(thumbnail_path)
|
37 |
+
return True
|
|
|
38 |
|
39 |
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location):
|
40 |
return {
|
|
|
49 |
"likes": 0
|
50 |
}
|
51 |
|
52 |
+
def upload_video_to_hf(video_file, video_name, title, description, uploader, custom_thumbnail=None):
|
53 |
# Create temp paths
|
54 |
temp_dir = "temp"
|
55 |
if not os.path.exists(temp_dir):
|
|
|
65 |
with open(video_path, "wb") as f:
|
66 |
f.write(video_file.read())
|
67 |
|
68 |
+
# Handle thumbnail
|
69 |
+
if custom_thumbnail:
|
70 |
+
thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
|
71 |
+
else:
|
72 |
+
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
|
73 |
+
|
74 |
if not thumbnail_extracted:
|
75 |
+
st.error("Failed to process thumbnail")
|
76 |
return None
|
77 |
|
78 |
+
# Upload the video
|
79 |
+
video_location = f"videos/{video_name}"
|
80 |
+
api.upload_file(
|
81 |
+
path_or_fileobj=video_path,
|
82 |
+
path_in_repo=video_location,
|
83 |
+
repo_id="vericudebuget/ok4231",
|
84 |
+
repo_type="space",
|
85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
# Upload the thumbnail
|
88 |
+
thumbnail_location = f"thumbnails/{thumbnail_name}"
|
89 |
+
api.upload_file(
|
90 |
+
path_or_fileobj=thumbnail_path,
|
91 |
+
path_in_repo=thumbnail_location,
|
92 |
+
repo_id="vericudebuget/ok4231",
|
93 |
+
repo_type="space",
|
94 |
+
)
|
95 |
+
|
96 |
+
# Generate and upload metadata JSON
|
97 |
+
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
|
98 |
+
with open(json_path, "w") as f:
|
99 |
+
json.dump(metadata, f, indent=2)
|
100 |
|
101 |
+
api.upload_file(
|
102 |
+
path_or_fileobj=json_path,
|
103 |
+
path_in_repo=f"metadata/{json_name}",
|
104 |
+
repo_id="vericudebuget/ok4231",
|
105 |
+
repo_type="space",
|
106 |
+
)
|
107 |
+
|
108 |
+
# Cleanup temp files
|
109 |
+
for file_path in [video_path, thumbnail_path, json_path]:
|
110 |
+
if os.path.exists(file_path):
|
111 |
+
os.remove(file_path)
|
112 |
+
|
113 |
+
return metadata
|
114 |
|
115 |
# Streamlit app interface
|
116 |
st.title("Upload your video")
|
|
|
127 |
description = st.text_area("Description", placeholder="Enter video description")
|
128 |
uploader = st.text_input("Uploader Name", placeholder="Enter your name")
|
129 |
|
130 |
+
# Optional custom thumbnail uploader
|
131 |
+
custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
|
132 |
+
|
133 |
# Upload button within the form
|
134 |
submit_button = st.form_submit_button("Upload Video")
|
135 |
|
|
|
143 |
uploaded_video.name,
|
144 |
title,
|
145 |
description,
|
146 |
+
uploader,
|
147 |
+
custom_thumbnail
|
148 |
)
|
149 |
if metadata:
|
150 |
st.success("Upload completed successfully!")
|