lelafav502 commited on
Commit
edc4e08
·
verified ·
1 Parent(s): 8bb68f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -2,11 +2,7 @@ import gradio as gr
2
  import json
3
  from faster_whisper import WhisperModel # Assuming you have installed this library
4
 
5
- def split_text_into_lines(data):
6
- MaxChars = 30
7
- MaxDuration = 2.5
8
- MaxGap = 1.5
9
-
10
  subtitles = []
11
  line = []
12
  line_duration = 0
@@ -21,9 +17,9 @@ def split_text_into_lines(data):
21
 
22
  temp = " ".join(item["word"] for item in line)
23
 
24
- duration_exceeded = line_duration > MaxDuration
25
- chars_exceeded = len(temp) > MaxChars
26
- maxgap_exceeded = (word_data['start'] - data[idx - 1]['end']) > MaxGap if idx > 0 else False
27
 
28
  if duration_exceeded or chars_exceeded or maxgap_exceeded:
29
  if line:
@@ -48,7 +44,7 @@ def split_text_into_lines(data):
48
 
49
  return subtitles
50
 
51
- def transcribe_audio(audiofilename):
52
  model_size = "medium"
53
  model = WhisperModel(model_size)
54
 
@@ -60,15 +56,18 @@ def transcribe_audio(audiofilename):
60
  for word in segment.words:
61
  wordlevel_info.append({'word': word.word, 'start': word.start, 'end': word.end})
62
 
63
- linelevel_subtitles = split_text_into_lines(wordlevel_info)
64
  return linelevel_subtitles
65
 
66
- def audio_transcription(audiofile):
67
- transcription = transcribe_audio(audiofile)
68
  return json.dumps(transcription, indent=4)
69
 
70
  iface = gr.Interface(audio_transcription,
71
- gr.Audio(sources="upload", type="filepath"),
 
 
 
72
  "text",
73
  description="Upload an audio file and get its transcription in JSON format.")
74
  iface.launch()
 
2
  import json
3
  from faster_whisper import WhisperModel # Assuming you have installed this library
4
 
5
+ def split_text_into_lines(data, max_chars, max_duration, max_gap):
 
 
 
 
6
  subtitles = []
7
  line = []
8
  line_duration = 0
 
17
 
18
  temp = " ".join(item["word"] for item in line)
19
 
20
+ duration_exceeded = line_duration > max_duration
21
+ chars_exceeded = len(temp) > max_chars
22
+ maxgap_exceeded = (word_data['start'] - data[idx - 1]['end']) > max_gap if idx > 0 else False
23
 
24
  if duration_exceeded or chars_exceeded or maxgap_exceeded:
25
  if line:
 
44
 
45
  return subtitles
46
 
47
+ def transcribe_audio(audiofilename, max_chars, max_duration, max_gap):
48
  model_size = "medium"
49
  model = WhisperModel(model_size)
50
 
 
56
  for word in segment.words:
57
  wordlevel_info.append({'word': word.word, 'start': word.start, 'end': word.end})
58
 
59
+ linelevel_subtitles = split_text_into_lines(wordlevel_info, max_chars, max_duration, max_gap)
60
  return linelevel_subtitles
61
 
62
+ def audio_transcription(audiofile, max_chars, max_duration, max_gap):
63
+ transcription = transcribe_audio(audiofile, max_chars, max_duration, max_gap)
64
  return json.dumps(transcription, indent=4)
65
 
66
  iface = gr.Interface(audio_transcription,
67
+ [gr.Audio(sources="upload", type="filepath"),
68
+ gr.inputs.Number(label="MaxChars"),
69
+ gr.inputs.Number(label="MaxDuration"),
70
+ gr.inputs.Number(label="MaxGap")],
71
  "text",
72
  description="Upload an audio file and get its transcription in JSON format.")
73
  iface.launch()