AngelinaZanardi commited on
Commit
dc1ede9
·
verified ·
1 Parent(s): b4f4567

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -28,7 +28,7 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
28
  print(f"Bruker enhet: {device}")
29
 
30
  @spaces.GPU(duration=60 * 2)
31
- def pipe(file, return_timestamps=False):
32
  asr = pipeline(
33
  task="automatic-speech-recognition",
34
  model=MODEL_NAME,
@@ -52,7 +52,8 @@ def format_output(text):
52
  text = re.sub(r'(\.{3,}|[.!:?])', lambda m: m.group() + '<br>', text)
53
  return text
54
 
55
- def transcribe(file, return_timestamps=False):
 
56
  waveform, sample_rate = torchaudio.load(file)
57
  audio_duration = waveform.size(1) / sample_rate
58
 
@@ -66,19 +67,34 @@ def transcribe(file, return_timestamps=False):
66
  else:
67
  file_to_transcribe = file
68
  truncated = False
 
69
 
70
- if not return_timestamps:
71
- text = pipe(file_to_transcribe)["text"]
72
- formatted_text = format_output(text)
 
 
 
 
 
 
 
 
 
 
73
  else:
74
- chunks = pipe(file_to_transcribe, return_timestamps=True)["chunks"]
75
- text = []
76
- for chunk in chunks:
77
- start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
78
- end_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][1])) if chunk["timestamp"][1] is not None else "??:??:??"
79
- line = f"[{start_time} -> {end_time}] {chunk['text']}"
80
- text.append(line)
81
- formatted_text = "<br>".join(text)
 
 
 
 
82
 
83
  if truncated:
84
  link="https://github.com/NbAiLab/nostram/blob/main/leverandorer.md"
@@ -144,13 +160,14 @@ with demo:
144
  inputs=[
145
  gr.components.Audio(sources=['upload', 'microphone'], type="filepath"),
146
  gr.components.Checkbox(label="Inkluder tidsstempler"),
 
147
  ],
148
  outputs=gr.HTML(label="text"),
149
  #outputs="text",
150
 
151
  description=(
152
- "Transkriber lange lydopptak fra mikrofon eller lydfiler med et enkelt klikk! Demoen bruker den fintunede"
153
- f" modellen <br> [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) og 🤗 Transformers til å transkribere lydfiler opp til 30 minutter."
154
  ),
155
  allow_flagging="never",
156
  #show_submit_button=False,
 
28
  print(f"Bruker enhet: {device}")
29
 
30
  @spaces.GPU(duration=60 * 2)
31
+ def pipe(file, return_timestamps=False,lang="no"):
32
  asr = pipeline(
33
  task="automatic-speech-recognition",
34
  model=MODEL_NAME,
 
52
  text = re.sub(r'(\.{3,}|[.!:?])', lambda m: m.group() + '<br>', text)
53
  return text
54
 
55
+ def transcribe(file, return_timestamps=False,lang="no"):
56
+
57
  waveform, sample_rate = torchaudio.load(file)
58
  audio_duration = waveform.size(1) / sample_rate
59
 
 
67
  else:
68
  file_to_transcribe = file
69
  truncated = False
70
+
71
 
72
+ if lang="no":
73
+ if not return_timestamps:
74
+ text = pipe(file_to_transcribe)["text"]
75
+ formatted_text = format_output(text)
76
+ else:
77
+ chunks = pipe(file_to_transcribe, return_timestamps=True)["chunks"]
78
+ text = []
79
+ for chunk in chunks:
80
+ start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
81
+ end_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][1])) if chunk["timestamp"][1] is not None else "??:??:??"
82
+ line = f"[{start_time} -> {end_time}] {chunk['text']}"
83
+ text.append(line)
84
+ formatted_text = "<br>".join(text)
85
  else:
86
+ if not return_timestamps:
87
+ text = pipe(file_to_transcribe, return_timestams=False,lang="nn")["text"]
88
+ formatted_text = format_output(text)
89
+ else:
90
+ chunks = pipe(file_to_transcribe, return_timestamps=True,lang="nn")["chunks"]
91
+ text = []
92
+ for chunk in chunks:
93
+ start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
94
+ end_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][1])) if chunk["timestamp"][1] is not None else "??:??:??"
95
+ line = f"[{start_time} -> {end_time}] {chunk['text']}"
96
+ text.append(line)
97
+ formatted_text = "<br>".join(text)
98
 
99
  if truncated:
100
  link="https://github.com/NbAiLab/nostram/blob/main/leverandorer.md"
 
160
  inputs=[
161
  gr.components.Audio(sources=['upload', 'microphone'], type="filepath"),
162
  gr.components.Checkbox(label="Inkluder tidsstempler"),
163
+ gr.components.Checkbox(label="Nynorsk"),
164
  ],
165
  outputs=gr.HTML(label="text"),
166
  #outputs="text",
167
 
168
  description=(
169
+ "Transkriber lange lydopptak fra mikrofon eller lydfiler med et enkelt klikk! <br> Demoen bruker den fintunede"
170
+ f" modellen [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) og 🤗 Transformers til å transkribere lydfiler opp til 30 minutter."
171
  ),
172
  allow_flagging="never",
173
  #show_submit_button=False,