vericudebuget commited on
Commit
5d2e489
·
verified ·
1 Parent(s): 1e9933b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -62
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 cv2
 
 
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
- video = cv2.VideoCapture(video_path)
22
-
23
- # Get total number of frames
24
- total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
25
-
26
- # Choose a random frame
27
- random_frame = random.randint(0, total_frames - 1)
28
-
29
- # Set the frame position
30
- video.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
31
-
32
- # Read the frame
33
- success, frame = video.read()
34
- if success:
35
- cv2.imwrite(thumbnail_path, frame)
36
-
37
- video.release()
38
- return success
 
 
 
 
 
 
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
- # 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
- # Cleanup temp files
106
- for file_path in [video_path, thumbnail_path, json_path]:
107
- if os.path.exists(file_path):
108
- os.remove(file_path)
109
 
110
- return metadata
 
 
 
 
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")