Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,39 +3,39 @@ from huggingface_hub import HfApi
|
|
3 |
import os
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
-
import
|
|
|
|
|
7 |
import random
|
8 |
-
import subprocess
|
9 |
-
import sys
|
10 |
-
|
11 |
-
try:
|
12 |
-
import cv2
|
13 |
-
except ImportError:
|
14 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "opencv-python"])
|
15 |
-
import cv2
|
16 |
|
17 |
# Initialize the Hugging Face API with the token
|
18 |
api = HfApi(token=os.getenv("HF_API_TOKEN"))
|
19 |
|
20 |
def extract_thumbnail(video_path, thumbnail_path):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location):
|
41 |
return {
|
@@ -69,45 +69,50 @@ def upload_video_to_hf(video_file, video_name, title, description, uploader):
|
|
69 |
# Extract and save thumbnail
|
70 |
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
|
71 |
if not thumbnail_extracted:
|
72 |
-
st.error("Failed to extract thumbnail from video")
|
73 |
return None
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
os.remove(file_path)
|
109 |
|
110 |
-
|
|
|
|
|
|
|
|
|
111 |
|
112 |
# Streamlit app interface
|
113 |
st.title("Upload your video")
|
|
|
3 |
import os
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
+
from moviepy.editor import VideoFileClip
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
try:
|
16 |
+
# Load the video
|
17 |
+
video = VideoFileClip(video_path)
|
18 |
+
|
19 |
+
# Get a random time point
|
20 |
+
duration = video.duration
|
21 |
+
random_time = random.uniform(0, duration)
|
22 |
+
|
23 |
+
# Extract the frame
|
24 |
+
frame = video.get_frame(random_time)
|
25 |
+
|
26 |
+
# Convert the frame to a PIL Image
|
27 |
+
image = Image.fromarray(frame)
|
28 |
+
|
29 |
+
# Save the thumbnail
|
30 |
+
image.save(thumbnail_path, "JPEG")
|
31 |
+
|
32 |
+
# Close the video to free up resources
|
33 |
+
video.close()
|
34 |
+
|
35 |
+
return True
|
36 |
+
except Exception as e:
|
37 |
+
st.error(f"Failed to extract thumbnail: {str(e)}")
|
38 |
+
return False
|
39 |
|
40 |
def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location):
|
41 |
return {
|
|
|
69 |
# Extract and save thumbnail
|
70 |
thumbnail_extracted = extract_thumbnail(video_path, thumbnail_path)
|
71 |
if not thumbnail_extracted:
|
|
|
72 |
return None
|
73 |
|
74 |
+
try:
|
75 |
+
# Upload the video
|
76 |
+
video_location = f"videos/{video_name}"
|
77 |
+
api.upload_file(
|
78 |
+
path_or_fileobj=video_path,
|
79 |
+
path_in_repo=video_location,
|
80 |
+
repo_id="vericudebuget/ok4231",
|
81 |
+
repo_type="space",
|
82 |
+
)
|
83 |
+
|
84 |
+
# Upload the thumbnail
|
85 |
+
thumbnail_location = f"thumbnails/{thumbnail_name}"
|
86 |
+
api.upload_file(
|
87 |
+
path_or_fileobj=thumbnail_path,
|
88 |
+
path_in_repo=thumbnail_location,
|
89 |
+
repo_id="vericudebuget/ok4231",
|
90 |
+
repo_type="space",
|
91 |
+
)
|
92 |
+
|
93 |
+
# Generate and upload metadata JSON
|
94 |
+
metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location)
|
95 |
+
with open(json_path, "w") as f:
|
96 |
+
json.dump(metadata, f, indent=2)
|
97 |
+
|
98 |
+
api.upload_file(
|
99 |
+
path_or_fileobj=json_path,
|
100 |
+
path_in_repo=f"metadata/{json_name}",
|
101 |
+
repo_id="vericudebuget/ok4231",
|
102 |
+
repo_type="space",
|
103 |
+
)
|
104 |
+
|
105 |
+
return metadata
|
106 |
|
107 |
+
except Exception as e:
|
108 |
+
st.error(f"Failed to upload: {str(e)}")
|
109 |
+
return None
|
|
|
110 |
|
111 |
+
finally:
|
112 |
+
# Cleanup temp files
|
113 |
+
for file_path in [video_path, thumbnail_path, json_path]:
|
114 |
+
if os.path.exists(file_path):
|
115 |
+
os.remove(file_path)
|
116 |
|
117 |
# Streamlit app interface
|
118 |
st.title("Upload your video")
|