Nick088 commited on
Commit
dc61e08
·
verified ·
1 Parent(s): 02d3e15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -257,8 +257,8 @@ def translate_audio(audio_file_path, prompt, model):
257
 
258
  # subtitles maker
259
 
 
260
  def create_srt_from_text(transcription_text):
261
- """Converts Groq text transcription to SRT format (assuming no timestamps)."""
262
  srt_lines = []
263
  # Assuming no timestamps, we'll assign a default duration of 1 second to each line
264
  duration = timedelta(seconds=1)
@@ -267,6 +267,7 @@ def create_srt_from_text(transcription_text):
267
  for i, text_part in enumerate(text_parts):
268
  text_part = text_part.strip()
269
  if text_part: # Only add lines with text
 
270
  start_timestamp = f"{start_time.seconds}:{start_time.microseconds // 1000:03}"
271
  end_timestamp = f"{(start_time + duration).seconds}:{(start_time + duration).microseconds // 1000:03}"
272
  srt_lines.append(f"{i+1}\n{start_timestamp} --> {end_timestamp}\n{text_part.strip()}\n\n")
@@ -274,14 +275,14 @@ def create_srt_from_text(transcription_text):
274
  return "".join(srt_lines)
275
 
276
 
 
277
  def generate_subtitles(audio_file_path, prompt, language, auto_detect_language, model):
278
- """Converts Whisper JSON transcription to SRT format."""
279
  # Check and process the file first
280
  processed_path, error_message = check_file(audio_file_path)
281
 
282
  # If there's an error during file check
283
  if error_message:
284
- return error_message, None, None, None # Return None for the download button
285
 
286
  with open(processed_path, "rb") as file:
287
  transcription_json = client.audio.transcriptions.create(
@@ -304,7 +305,7 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language,
304
  temp_srt_path = temp_srt_file.name
305
  temp_srt_file.write(srt_content)
306
 
307
- # Generate subtitles and add to video if MP4
308
  if audio_file_path.lower().endswith((".mp4", ".webm")):
309
  try:
310
  # Use ffmpeg to add subtitles to the video
@@ -334,9 +335,9 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language,
334
  ],
335
  check=True,
336
  )
337
- return None, output_file_path, None, temp_srt_path # Return the temp_srt_path
338
  except subprocess.CalledProcessError as e:
339
- return None, None, f"Error during subtitle addition: {e}", temp_srt_path # Return the temp_srt_path
340
 
341
  return temp_srt_path, None, None
342
 
@@ -345,7 +346,8 @@ with gr.Blocks() as demo:
345
  gr.Markdown(
346
  """
347
  # Groq API UI
348
- Inference by Groq. Hugging Face Space by [Nick088](https://linktr.ee/Nick088)
 
349
  """
350
  )
351
  with gr.Tabs():
 
257
 
258
  # subtitles maker
259
 
260
+ # helper function convert json transcription to srt
261
  def create_srt_from_text(transcription_text):
 
262
  srt_lines = []
263
  # Assuming no timestamps, we'll assign a default duration of 1 second to each line
264
  duration = timedelta(seconds=1)
 
267
  for i, text_part in enumerate(text_parts):
268
  text_part = text_part.strip()
269
  if text_part: # Only add lines with text
270
+ # Format timestamps correctly
271
  start_timestamp = f"{start_time.seconds}:{start_time.microseconds // 1000:03}"
272
  end_timestamp = f"{(start_time + duration).seconds}:{(start_time + duration).microseconds // 1000:03}"
273
  srt_lines.append(f"{i+1}\n{start_timestamp} --> {end_timestamp}\n{text_part.strip()}\n\n")
 
275
  return "".join(srt_lines)
276
 
277
 
278
+ # getting transcription + using helper function + adding subs to video if input is video
279
  def generate_subtitles(audio_file_path, prompt, language, auto_detect_language, model):
 
280
  # Check and process the file first
281
  processed_path, error_message = check_file(audio_file_path)
282
 
283
  # If there's an error during file check
284
  if error_message:
285
+ return error_message, None, None
286
 
287
  with open(processed_path, "rb") as file:
288
  transcription_json = client.audio.transcriptions.create(
 
305
  temp_srt_path = temp_srt_file.name
306
  temp_srt_file.write(srt_content)
307
 
308
+ # Generate subtitles and add to video if input is video
309
  if audio_file_path.lower().endswith((".mp4", ".webm")):
310
  try:
311
  # Use ffmpeg to add subtitles to the video
 
335
  ],
336
  check=True,
337
  )
338
+ return None, output_file_path, None
339
  except subprocess.CalledProcessError as e:
340
+ return None, None, gr.Error(f"Error during subtitle addition: {e}")
341
 
342
  return temp_srt_path, None, None
343
 
 
346
  gr.Markdown(
347
  """
348
  # Groq API UI
349
+ Inference by Groq
350
+ Hugging Face Space by [Nick088](https://linktr.ee/Nick088)
351
  """
352
  )
353
  with gr.Tabs():