vericudebuget commited on
Commit
bf20bee
·
verified ·
1 Parent(s): 4f89214

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -101,7 +101,7 @@ def update_index_file(new_metadata_path):
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)
@@ -129,9 +129,10 @@ def generate_subtitles(video_path):
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)
@@ -163,21 +164,24 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
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}"
@@ -237,8 +241,8 @@ if uploaded_video:
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"])
@@ -256,7 +260,7 @@ if uploaded_video:
256
  title,
257
  description,
258
  uploader,
259
- generate_subtitles,
260
  custom_thumbnail
261
  )
262
  if metadata:
 
101
  if os.path.exists(temp_dir):
102
  shutil.rmtree(temp_dir)
103
 
104
+ def create_subtitles(video_path): # Renamed from generate_subtitles
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)
 
129
 
130
  vtt_content += f"{start_time_vtt} --> {end_time_vtt}\n{text}\n\n"
131
 
132
+ os.unlink(temp_audio.name) # Clean up temp file
133
  return vtt_content
134
 
135
+ def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None): # Renamed parameter
136
  temp_dir = "temp"
137
  if not os.path.exists(temp_dir):
138
  os.makedirs(temp_dir)
 
164
 
165
  # Generate and upload subtitles if requested and video is not too long
166
  subtitle_location = ""
167
+ if should_generate_subs and video_length <= 3600: # 1 hour in seconds
168
+ try:
169
+ vtt_content = create_subtitles(video_path) # Using renamed function
170
+ subtitle_name = f"{base_name}.vtt"
171
+ subtitle_path = os.path.join(temp_dir, subtitle_name)
172
+
173
+ with open(subtitle_path, 'w') as f:
174
+ f.write(vtt_content)
175
+
176
+ subtitle_location = f"subtitles/{subtitle_name}"
177
+ hf_api.upload_file(
178
+ path_or_fileobj=subtitle_path,
179
+ path_in_repo=subtitle_location,
180
+ repo_id="vericudebuget/ok4231",
181
+ repo_type="space",
182
+ )
183
+ except Exception as e:
184
+ st.warning(f"Failed to generate subtitles: {str(e)}")
185
 
186
  # Upload video and thumbnail
187
  video_location = f"videos/{video_name}"
 
241
  os.unlink(temp_video.name) # Clean up temp file
242
 
243
  # Subtitle generation toggle, disabled if video is longer than 1 hour
244
+ should_generate_subs = st.toggle("Generate Subtitles", disabled=video_duration > 3600, value=True) # Renamed variable
245
+ if video_duration > 3600 and should_generate_subs:
246
  st.warning("Subtitle generation is disabled for videos longer than 1 hour.")
247
 
248
  custom_thumbnail = st.file_uploader("Upload custom thumbnail (optional)", type=["jpg", "jpeg", "png"])
 
260
  title,
261
  description,
262
  uploader,
263
+ should_generate_subs, # Using renamed variable
264
  custom_thumbnail
265
  )
266
  if metadata: