vericudebuget commited on
Commit
8fc67f3
·
verified ·
1 Parent(s): bd4ed7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -6,11 +6,19 @@ 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
 
@@ -49,16 +57,21 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
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):
56
  os.makedirs(temp_dir)
57
 
 
 
58
  video_path = os.path.join(temp_dir, video_name)
59
- thumbnail_name = f"{os.path.splitext(video_name)[0]}_thumb.jpg"
 
 
60
  thumbnail_path = os.path.join(temp_dir, thumbnail_name)
61
- json_name = f"{os.path.splitext(video_name)[0]}-index.json"
 
62
  json_path = os.path.join(temp_dir, json_name)
63
 
64
  # Write the video content to a file
 
6
  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"))
13
 
14
+ def generate_random_string(length=4):
15
+ return ''.join(random.choices(string.ascii_lowercase, k=length))
16
+
17
+ def add_random_to_filename(filename):
18
+ name, ext = os.path.splitext(filename)
19
+ random_string = generate_random_string()
20
+ return f"{name}-{random_string}{ext}"
21
+
22
  def extract_thumbnail(video_path, thumbnail_path):
23
  video = cv2.VideoCapture(video_path)
24
 
 
57
  "likes": 0
58
  }
59
 
60
+ def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
61
  # Create temp paths
62
  temp_dir = "temp"
63
  if not os.path.exists(temp_dir):
64
  os.makedirs(temp_dir)
65
 
66
+ # Generate randomized filenames
67
+ video_name = add_random_to_filename(original_video_name)
68
  video_path = os.path.join(temp_dir, video_name)
69
+
70
+ base_name = os.path.splitext(video_name)[0]
71
+ thumbnail_name = f"{base_name}_thumb.jpg"
72
  thumbnail_path = os.path.join(temp_dir, thumbnail_name)
73
+
74
+ json_name = f"{base_name}-index.json"
75
  json_path = os.path.join(temp_dir, json_name)
76
 
77
  # Write the video content to a file