AngelinaZanardi commited on
Commit
87118ef
·
verified ·
1 Parent(s): 3b6c548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -18,7 +18,7 @@ except ImportError:
18
  import yt_dlp # Added import for yt-dlp
19
 
20
  MODEL_NAME = "NbAiLab/nb-whisper-large"
21
- lang = "no"
22
 
23
  max_audio_length= 30 * 60
24
 
@@ -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,lang="no"):
32
  asr = pipeline(
33
  task="automatic-speech-recognition",
34
  model=MODEL_NAME,
@@ -38,12 +38,19 @@ def pipe(file, return_timestamps=False,lang="no"):
38
  torch_dtype=torch.float16,
39
  model_kwargs={"attn_implementation": "flash_attention_2", "num_beams": 5} if FLASH_ATTENTION else {"attn_implementation": "sdpa", "num_beams": 5},
40
  )
41
- asr.model.config.forced_decoder_ids = asr.tokenizer.get_decoder_prompt_ids(
42
- language=lang,
43
- task="transcribe",
44
- no_timestamps=not return_timestamps,
45
- )
46
- return asr(file, return_timestamps=return_timestamps, batch_size=24)
 
 
 
 
 
 
 
47
 
48
  def format_output(text):
49
  # Add a line break after ".", "!", ":", or "?" unless part of sequences like "..."
@@ -84,10 +91,10 @@ def transcribe(file, return_timestamps=False,lang_nn=False):
84
  formatted_text = "<br>".join(text)
85
  else:
86
  if not return_timestamps:
87
- text = pipe(file_to_transcribe,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 "??:??:??"
 
18
  import yt_dlp # Added import for yt-dlp
19
 
20
  MODEL_NAME = "NbAiLab/nb-whisper-large"
21
+ #lang = "no"
22
 
23
  max_audio_length= 30 * 60
24
 
 
28
  print(f"Bruker enhet: {device}")
29
 
30
  @spaces.GPU(duration=60 * 2)
31
+ def pipe(file, return_timestamps=False,lang_nn=False):
32
  asr = pipeline(
33
  task="automatic-speech-recognition",
34
  model=MODEL_NAME,
 
38
  torch_dtype=torch.float16,
39
  model_kwargs={"attn_implementation": "flash_attention_2", "num_beams": 5} if FLASH_ATTENTION else {"attn_implementation": "sdpa", "num_beams": 5},
40
  )
41
+ if not lang_nn:
42
+ asr.model.config.forced_decoder_ids = asr.tokenizer.get_decoder_prompt_ids(
43
+ language="no",
44
+ task="transcribe",
45
+ no_timestamps=not return_timestamps,
46
+ )
47
+ else:
48
+ asr.model.config.forced_decoder_ids = asr.tokenizer.get_decoder_prompt_ids(
49
+ language="nn",
50
+ task="transcribe",
51
+ no_timestamps=not return_timestamps,
52
+ )
53
+ return asr(file, return_timestamps=return_timestamps, lang_nn=lang_nn, batch_size=24)
54
 
55
  def format_output(text):
56
  # Add a line break after ".", "!", ":", or "?" unless part of sequences like "..."
 
91
  formatted_text = "<br>".join(text)
92
  else:
93
  if not return_timestamps:
94
+ text = pipe(file_to_transcribe,lang_nn=True)["text"]
95
  formatted_text = format_output(text)
96
  else:
97
+ chunks = pipe(file_to_transcribe, return_timestamps=True,lang_nn=True)["chunks"]
98
  text = []
99
  for chunk in chunks:
100
  start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"