Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
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,
|
@@ -38,18 +38,11 @@ def pipe(file, return_timestamps=False,lang_nn=False):
|
|
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 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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, batch_size=24)
|
54 |
|
55 |
def format_output(text):
|
@@ -81,10 +74,10 @@ def transcribe(file, return_timestamps=False,lang_nn=False):
|
|
81 |
|
82 |
if not lang_nn:
|
83 |
if not return_timestamps:
|
84 |
-
text = pipe(file_to_transcribe)["text"]
|
85 |
formatted_text = format_output(text)
|
86 |
else:
|
87 |
-
chunks = pipe(file_to_transcribe, return_timestamps=True)["chunks"]
|
88 |
text = []
|
89 |
for chunk in chunks:
|
90 |
start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
|
@@ -94,10 +87,10 @@ def transcribe(file, return_timestamps=False,lang_nn=False):
|
|
94 |
formatted_text = "<br>".join(text)
|
95 |
else:
|
96 |
if not return_timestamps:
|
97 |
-
text = pipe(file_to_transcribe,
|
98 |
formatted_text = format_output(text)
|
99 |
else:
|
100 |
-
chunks = pipe(file_to_transcribe, return_timestamps=True,
|
101 |
text = []
|
102 |
for chunk in chunks:
|
103 |
start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
|
|
|
28 |
print(f"Bruker enhet: {device}")
|
29 |
|
30 |
@spaces.GPU(duration=60 * 2)
|
31 |
+
def pipe(file, return_timestamps=False,lang):
|
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 |
+
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):
|
|
|
74 |
|
75 |
if not lang_nn:
|
76 |
if not return_timestamps:
|
77 |
+
text = pipe(file_to_transcribe,lang="no")["text"]
|
78 |
formatted_text = format_output(text)
|
79 |
else:
|
80 |
+
chunks = pipe(file_to_transcribe, return_timestamps=True,lang="no")["chunks"]
|
81 |
text = []
|
82 |
for chunk in chunks:
|
83 |
start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
|
|
|
87 |
formatted_text = "<br>".join(text)
|
88 |
else:
|
89 |
if not return_timestamps:
|
90 |
+
text = pipe(file_to_transcribe,lang="nn")["text"]
|
91 |
formatted_text = format_output(text)
|
92 |
else:
|
93 |
+
chunks = pipe(file_to_transcribe, return_timestamps=True,lang="nn")["chunks"]
|
94 |
text = []
|
95 |
for chunk in chunks:
|
96 |
start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
|