Nick088 commited on
Commit
b6d6c4d
·
verified ·
1 Parent(s): df9a14a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -256,31 +256,32 @@ def translate_audio(audio_file_path, prompt):
256
 
257
  # subtitles maker
258
 
259
- def create_srt_from_json(transcription_json):
260
- """Converts Whisper JSON transcription to SRT format."""
261
  srt_lines = []
262
- for i, segment in enumerate(transcription_json["segments"]):
263
- start_time = timedelta(seconds=segment["start"])
264
- end_time = timedelta(seconds=segment["end"])
265
- text = segment["text"]
266
-
267
- # Format SRT timestamp
268
- start_timestamp = f"{start_time.seconds}:{start_time.microseconds // 1000:03}"
269
- end_timestamp = f"{end_time.seconds}:{end_time.microseconds // 1000:03}"
270
-
271
- srt_lines.append(f"{i+1}\n{start_timestamp} --> {end_timestamp}\n{text}\n\n")
272
-
273
  return "".join(srt_lines)
274
 
275
 
276
  def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
 
277
  # Check and process the file first
278
  processed_path, error_message = check_file(audio_file_path)
279
 
280
  # If there's an error during file check
281
  if error_message:
282
  return error_message
283
-
284
  with open(processed_path, "rb") as file:
285
  transcription_json = client.audio.transcriptions.create(
286
  file=(os.path.basename(processed_path), file.read()),
@@ -291,11 +292,10 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
291
  temperature=0.0,
292
  )
293
 
294
- transcription_json = json.loads(transcription_json.to_json())
 
295
 
296
- print(transcription_json)
297
-
298
- srt_content = create_srt_from_json(transcription_json)
299
  return srt_content
300
 
301
 
 
256
 
257
  # subtitles maker
258
 
259
+ def create_srt_from_text(transcription_text):
260
+ """Converts Groq text transcription to SRT format (assuming no timestamps)."""
261
  srt_lines = []
262
+ # Assuming no timestamps, we'll assign a default duration of 1 second to each line
263
+ duration = timedelta(seconds=1)
264
+ text_parts = transcription_text.split(".") # Split by periods to separate sentences
265
+ start_time = timedelta(seconds=0)
266
+ for i, text_part in enumerate(text_parts):
267
+ text_part = text_part.strip()
268
+ if text_part: # Only add lines with text
269
+ start_timestamp = f"{start_time.seconds}:{start_time.microseconds // 1000:03}"
270
+ end_timestamp = f"{(start_time + duration).seconds}:{(start_time + duration).microseconds // 1000:03}"
271
+ srt_lines.append(f"{i+1}\n{start_timestamp} --> {end_timestamp}\n{text_part.strip()}\n\n")
272
+ start_time += duration
273
  return "".join(srt_lines)
274
 
275
 
276
  def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
277
+ """Converts Whisper JSON transcription to SRT format."""
278
  # Check and process the file first
279
  processed_path, error_message = check_file(audio_file_path)
280
 
281
  # If there's an error during file check
282
  if error_message:
283
  return error_message
284
+
285
  with open(processed_path, "rb") as file:
286
  transcription_json = client.audio.transcriptions.create(
287
  file=(os.path.basename(processed_path), file.read()),
 
292
  temperature=0.0,
293
  )
294
 
295
+ # Extract the text from the Groq JSON response
296
+ transcription_text = transcription_json['text']
297
 
298
+ srt_content = create_srt_from_text(transcription_text)
 
 
299
  return srt_content
300
 
301