vericudebuget commited on
Commit
7a2d05f
·
verified ·
1 Parent(s): e971c05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -40
app.py CHANGED
@@ -10,9 +10,13 @@ import string
10
  import subprocess
11
  import glob
12
  import shutil
 
 
 
13
 
14
- # Initialize the Hugging Face API with the token
15
- api = HfApi(token=os.getenv("HF_API_TOKEN"))
 
16
 
17
  def generate_random_string(length=4):
18
  return ''.join(random.choices(string.ascii_lowercase, k=length))
@@ -40,13 +44,13 @@ def save_custom_thumbnail(thumbnail_file, thumbnail_path):
40
 
41
  def get_video_length(video_path):
42
  video = cv2.VideoCapture(video_path)
43
- fps = video.get(cv2.CAP_PROP_FPS) # Frames per second
44
- total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) # Total frames in the video
45
- duration = int(total_frames / fps) if fps > 0 else 0 # Duration in seconds, as an integer
46
  video.release()
47
  return duration
48
 
49
- def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location, duration):
50
  return {
51
  "fileName": video_name,
52
  "title": title,
@@ -55,46 +59,38 @@ def generate_metadata(video_name, title, description, uploader, file_location, t
55
  "uploadTimestamp": datetime.now().isoformat(),
56
  "fileLocation": file_location,
57
  "thumbnailLocation": thumbnail_location,
58
- "duration": duration, # Add duration here
 
59
  "views": 0,
60
  "likes": 0
61
  }
62
 
63
  def update_index_file(new_metadata_path):
64
- # Create a temporary directory for cloning
65
  temp_dir = "temp_repo"
66
  if os.path.exists(temp_dir):
67
  shutil.rmtree(temp_dir)
68
 
69
  try:
70
- # Clone the repository
71
  subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
72
-
73
- # Find all JSON files in the metadata directory
74
  metadata_dir = os.path.join(temp_dir, 'metadata')
75
  json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
76
 
77
- # Create the paths string
78
  base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
79
  paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
80
 
81
- # Add the new metadata path if it's not already there
82
  new_metadata_filename = os.path.basename(new_metadata_path)
83
  new_full_path = f"{base_url}{new_metadata_filename}"
84
  if new_full_path not in paths:
85
  paths.append(new_full_path)
86
 
87
- # Create the index content
88
  index_content = "{ " + "; ".join(paths) + "; }"
89
 
90
- # Write to a temporary file
91
  index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
92
  os.makedirs(os.path.dirname(index_path), exist_ok=True)
93
  with open(index_path, 'w') as f:
94
  f.write(index_content)
95
 
96
- # Upload the index file
97
- api.upload_file(
98
  path_or_fileobj=index_path,
99
  path_in_repo="metadata/video-index.json",
100
  repo_id="vericudebuget/ok4231",
@@ -102,18 +98,45 @@ def update_index_file(new_metadata_path):
102
  )
103
 
104
  finally:
105
- # Clean up
106
  if os.path.exists(temp_dir):
107
  shutil.rmtree(temp_dir)
108
 
109
- def upload_video_to_hf(video_file, original_video_name, title, description, uploader, custom_thumbnail=None):
110
- # Create temp paths
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  temp_dir = "temp"
112
  if not os.path.exists(temp_dir):
113
  os.makedirs(temp_dir)
114
 
115
  try:
116
- # Generate randomized filenames
117
  video_name = add_random_to_filename(original_video_name)
118
  video_path = os.path.join(temp_dir, video_name)
119
 
@@ -124,11 +147,9 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
124
  json_name = f"{base_name}-index.json"
125
  json_path = os.path.join(temp_dir, json_name)
126
 
127
- # Write the video content to a file
128
  with open(video_path, "wb") as f:
129
  f.write(video_file.read())
130
 
131
- # Handle thumbnail
132
  if custom_thumbnail:
133
  thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
134
  else:
@@ -138,47 +159,61 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
138
  st.error("Failed to process thumbnail")
139
  return None
140
 
141
- # Get video length
142
  video_length = get_video_length(video_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
- # Upload the video
145
  video_location = f"videos/{video_name}"
146
- api.upload_file(
147
  path_or_fileobj=video_path,
148
  path_in_repo=video_location,
149
  repo_id="vericudebuget/ok4231",
150
  repo_type="space",
151
  )
152
 
153
- # Upload the thumbnail
154
  thumbnail_location = f"thumbnails/{thumbnail_name}"
155
- api.upload_file(
156
  path_or_fileobj=thumbnail_path,
157
  path_in_repo=thumbnail_location,
158
  repo_id="vericudebuget/ok4231",
159
  repo_type="space",
160
  )
161
 
162
- # Generate and upload metadata JSON
163
- metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location, video_length)
164
  with open(json_path, "w") as f:
165
  json.dump(metadata, f, indent=2)
166
 
167
  metadata_location = f"metadata/{json_name}"
168
- api.upload_file(
169
  path_or_fileobj=json_path,
170
  path_in_repo=metadata_location,
171
  repo_id="vericudebuget/ok4231",
172
  repo_type="space",
173
  )
174
 
175
- # Update the index file
176
  update_index_file(metadata_location)
177
 
178
  return metadata
179
 
180
  finally:
181
- # Cleanup temp files
182
  if os.path.exists(temp_dir):
183
  shutil.rmtree(temp_dir)
184
 
@@ -186,38 +221,46 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
186
  st.title("Upload your video")
187
  st.markdown("---")
188
 
189
- # File uploader for video input
190
- uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov"])
191
 
192
  if uploaded_video:
193
- # Show the video details form
194
  with st.form("video_details"):
195
  st.write("Video Details")
196
  title = st.text_input("Title", placeholder="Enter video title")
197
  description = st.text_area("Description", placeholder="Enter video description")
198
  uploader = st.text_input("Uploader Name", placeholder="Enter your name")
199
 
200
- # Optional custom thumbnail uploader
 
 
 
 
 
 
 
 
 
 
201
  custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
202
 
203
- # Upload button within the form
204
  submit_button = st.form_submit_button("Upload Video")
205
 
206
  if submit_button:
207
  if not title or not uploader:
208
  st.error("Please fill in the title and uploader name.")
209
  else:
210
- with st.spinner("Uploading video, generating thumbnail and metadata..."):
211
  metadata = upload_video_to_hf(
212
  uploaded_video,
213
  uploaded_video.name,
214
  title,
215
  description,
216
  uploader,
 
217
  custom_thumbnail
218
  )
219
  if metadata:
220
  st.success("Upload completed successfully!")
221
  st.json(metadata)
222
  else:
223
- st.info("Please upload a video file to begin.")
 
10
  import subprocess
11
  import glob
12
  import shutil
13
+ from groq import Groq
14
+ import tempfile
15
+ from pydub import AudioSegment
16
 
17
+ # Initialize the Hugging Face and Groq APIs
18
+ hf_api = HfApi(token=os.getenv("HF_API_TOKEN"))
19
+ groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
20
 
21
  def generate_random_string(length=4):
22
  return ''.join(random.choices(string.ascii_lowercase, k=length))
 
44
 
45
  def get_video_length(video_path):
46
  video = cv2.VideoCapture(video_path)
47
+ fps = video.get(cv2.CAP_PROP_FPS)
48
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
49
+ duration = int(total_frames / fps) if fps > 0 else 0
50
  video.release()
51
  return duration
52
 
53
+ def generate_metadata(video_name, title, description, uploader, file_location, thumbnail_location, subtitle_location, duration):
54
  return {
55
  "fileName": video_name,
56
  "title": title,
 
59
  "uploadTimestamp": datetime.now().isoformat(),
60
  "fileLocation": file_location,
61
  "thumbnailLocation": thumbnail_location,
62
+ "subtitleLocation": subtitle_location,
63
+ "duration": duration,
64
  "views": 0,
65
  "likes": 0
66
  }
67
 
68
  def update_index_file(new_metadata_path):
 
69
  temp_dir = "temp_repo"
70
  if os.path.exists(temp_dir):
71
  shutil.rmtree(temp_dir)
72
 
73
  try:
 
74
  subprocess.run(['git', 'clone', 'https://huggingface.co/spaces/vericudebuget/ok4231', temp_dir], check=True)
 
 
75
  metadata_dir = os.path.join(temp_dir, 'metadata')
76
  json_files = glob.glob(os.path.join(metadata_dir, '*-index.json'))
77
 
 
78
  base_url = "huggingface.co/spaces/vericudebuget/ok4231/raw/main/metadata/"
79
  paths = [f"{base_url}{os.path.basename(f)}" for f in json_files]
80
 
 
81
  new_metadata_filename = os.path.basename(new_metadata_path)
82
  new_full_path = f"{base_url}{new_metadata_filename}"
83
  if new_full_path not in paths:
84
  paths.append(new_full_path)
85
 
 
86
  index_content = "{ " + "; ".join(paths) + "; }"
87
 
 
88
  index_path = os.path.join(temp_dir, 'metadata', 'video-index.json')
89
  os.makedirs(os.path.dirname(index_path), exist_ok=True)
90
  with open(index_path, 'w') as f:
91
  f.write(index_content)
92
 
93
+ hf_api.upload_file(
 
94
  path_or_fileobj=index_path,
95
  path_in_repo="metadata/video-index.json",
96
  repo_id="vericudebuget/ok4231",
 
98
  )
99
 
100
  finally:
 
101
  if os.path.exists(temp_dir):
102
  shutil.rmtree(temp_dir)
103
 
104
+ def generate_subtitles(video_path):
105
+ with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_audio:
106
+ # Convert video to mono 128kbps MP3
107
+ audio = AudioSegment.from_file(video_path)
108
+ audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
109
+ audio.export(temp_audio.name, format='mp3', bitrate='128k')
110
+
111
+ # Generate subtitles using Groq
112
+ with open(temp_audio.name, 'rb') as audio_file:
113
+ translation = groq_client.audio.translations.create(
114
+ file=(temp_audio.name, audio_file.read()),
115
+ model="whisper-large-v3",
116
+ response_format="verbose_json",
117
+ temperature=0.0
118
+ )
119
+
120
+ # Generate VTT content
121
+ vtt_content = "WEBVTT\n\n"
122
+ for segment in translation.segments:
123
+ start_time = segment['start']
124
+ end_time = segment['end']
125
+ text = segment['text'].strip()
126
+
127
+ start_time_vtt = f"{int(start_time // 3600):02}:{int((start_time % 3600) // 60):02}:{start_time % 60:06.3f}"
128
+ end_time_vtt = f"{int(end_time // 3600):02}:{int((end_time % 3600) // 60):02}:{end_time % 60:06.3f}"
129
+
130
+ vtt_content += f"{start_time_vtt} --> {end_time_vtt}\n{text}\n\n"
131
+
132
+ return vtt_content
133
+
134
+ def upload_video_to_hf(video_file, original_video_name, title, description, uploader, generate_subs=False, custom_thumbnail=None):
135
  temp_dir = "temp"
136
  if not os.path.exists(temp_dir):
137
  os.makedirs(temp_dir)
138
 
139
  try:
 
140
  video_name = add_random_to_filename(original_video_name)
141
  video_path = os.path.join(temp_dir, video_name)
142
 
 
147
  json_name = f"{base_name}-index.json"
148
  json_path = os.path.join(temp_dir, json_name)
149
 
 
150
  with open(video_path, "wb") as f:
151
  f.write(video_file.read())
152
 
 
153
  if custom_thumbnail:
154
  thumbnail_extracted = save_custom_thumbnail(custom_thumbnail, thumbnail_path)
155
  else:
 
159
  st.error("Failed to process thumbnail")
160
  return None
161
 
 
162
  video_length = get_video_length(video_path)
163
+
164
+ # Generate and upload subtitles if requested and video is not too long
165
+ subtitle_location = ""
166
+ if generate_subs and video_length <= 3600: # 1 hour in seconds
167
+ vtt_content = generate_subtitles(video_path)
168
+ subtitle_name = f"{base_name}.vtt"
169
+ subtitle_path = os.path.join(temp_dir, subtitle_name)
170
+
171
+ with open(subtitle_path, 'w') as f:
172
+ f.write(vtt_content)
173
+
174
+ subtitle_location = f"subtitles/{subtitle_name}"
175
+ hf_api.upload_file(
176
+ path_or_fileobj=subtitle_path,
177
+ path_in_repo=subtitle_location,
178
+ repo_id="vericudebuget/ok4231",
179
+ repo_type="space",
180
+ )
181
 
182
+ # Upload video and thumbnail
183
  video_location = f"videos/{video_name}"
184
+ hf_api.upload_file(
185
  path_or_fileobj=video_path,
186
  path_in_repo=video_location,
187
  repo_id="vericudebuget/ok4231",
188
  repo_type="space",
189
  )
190
 
 
191
  thumbnail_location = f"thumbnails/{thumbnail_name}"
192
+ hf_api.upload_file(
193
  path_or_fileobj=thumbnail_path,
194
  path_in_repo=thumbnail_location,
195
  repo_id="vericudebuget/ok4231",
196
  repo_type="space",
197
  )
198
 
199
+ # Generate and upload metadata
200
+ metadata = generate_metadata(video_name, title, description, uploader, video_location, thumbnail_location, subtitle_location, video_length)
201
  with open(json_path, "w") as f:
202
  json.dump(metadata, f, indent=2)
203
 
204
  metadata_location = f"metadata/{json_name}"
205
+ hf_api.upload_file(
206
  path_or_fileobj=json_path,
207
  path_in_repo=metadata_location,
208
  repo_id="vericudebuget/ok4231",
209
  repo_type="space",
210
  )
211
 
 
212
  update_index_file(metadata_location)
213
 
214
  return metadata
215
 
216
  finally:
 
217
  if os.path.exists(temp_dir):
218
  shutil.rmtree(temp_dir)
219
 
 
221
  st.title("Upload your video")
222
  st.markdown("---")
223
 
224
+ uploaded_video = st.file_uploader("Choose video file", type=["mp4", "avi", "mov", "webm", "mkv"])
 
225
 
226
  if uploaded_video:
 
227
  with st.form("video_details"):
228
  st.write("Video Details")
229
  title = st.text_input("Title", placeholder="Enter video title")
230
  description = st.text_area("Description", placeholder="Enter video description")
231
  uploader = st.text_input("Uploader Name", placeholder="Enter your name")
232
 
233
+ # Create a temporary file to get video duration
234
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
235
+ temp_video.write(uploaded_video.getvalue())
236
+ video_duration = get_video_length(temp_video.name)
237
+ os.unlink(temp_video.name) # Clean up temp file
238
+
239
+ # Subtitle generation toggle, disabled if video is longer than 1 hour
240
+ generate_subtitles = st.toggle("Generate Subtitles", disabled=video_duration > 3600)
241
+ if video_duration > 3600 and generate_subtitles:
242
+ st.warning("Subtitle generation is disabled for videos longer than 1 hour.")
243
+
244
  custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
245
 
 
246
  submit_button = st.form_submit_button("Upload Video")
247
 
248
  if submit_button:
249
  if not title or not uploader:
250
  st.error("Please fill in the title and uploader name.")
251
  else:
252
+ with st.spinner("Uploading video, generating thumbnail and metadata... This may take some time. Please wait."):
253
  metadata = upload_video_to_hf(
254
  uploaded_video,
255
  uploaded_video.name,
256
  title,
257
  description,
258
  uploader,
259
+ generate_subtitles,
260
  custom_thumbnail
261
  )
262
  if metadata:
263
  st.success("Upload completed successfully!")
264
  st.json(metadata)
265
  else:
266
+ st.info("Please upload a video file to begin.")