Spaces:
Running
on
T4
Running
on
T4
update test
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import time
|
2 |
import os
|
|
|
3 |
|
4 |
import torch
|
5 |
|
@@ -21,7 +22,7 @@ lang = "no"
|
|
21 |
share = (os.environ.get("SHARE", "False")[0].lower() in "ty1") or None
|
22 |
auth_token = os.environ.get("AUTH_TOKEN") or True
|
23 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
24 |
-
print(f"
|
25 |
|
26 |
@spaces.GPU(duration=60 * 2)
|
27 |
def pipe(file, return_timestamps=False):
|
@@ -41,9 +42,15 @@ def pipe(file, return_timestamps=False):
|
|
41 |
)
|
42 |
return asr(file, return_timestamps=return_timestamps, batch_size=24)
|
43 |
|
|
|
|
|
|
|
|
|
|
|
44 |
def transcribe(file, return_timestamps=False):
|
45 |
if not return_timestamps:
|
46 |
text = pipe(file)["text"]
|
|
|
47 |
else:
|
48 |
chunks = pipe(file, return_timestamps=True)["chunks"]
|
49 |
text = []
|
@@ -52,8 +59,8 @@ def transcribe(file, return_timestamps=False):
|
|
52 |
end_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][1])) if chunk["timestamp"][1] is not None else "??:??:??"
|
53 |
line = f"[{start_time} -> {end_time}] {chunk['text']}"
|
54 |
text.append(line)
|
55 |
-
|
56 |
-
return
|
57 |
|
58 |
def _return_yt_html_embed(yt_url):
|
59 |
video_id = yt_url.split("?v=")[-1]
|
@@ -83,7 +90,7 @@ def yt_transcribe(yt_url, return_timestamps=False):
|
|
83 |
|
84 |
return html_embed_str, text
|
85 |
|
86 |
-
#
|
87 |
|
88 |
demo = gr.Blocks()
|
89 |
|
@@ -92,35 +99,35 @@ with demo:
|
|
92 |
fn=transcribe,
|
93 |
inputs=[
|
94 |
gr.components.Audio(sources=['upload', 'microphone'], type="filepath"),
|
95 |
-
gr.components.Checkbox(label="
|
96 |
],
|
97 |
outputs="text",
|
98 |
title="NB-Whisper",
|
99 |
description=(
|
100 |
-
"
|
101 |
-
f"
|
102 |
-
"
|
103 |
),
|
104 |
allow_flagging="never",
|
105 |
)
|
106 |
|
107 |
-
#
|
108 |
# yt_transcribe_interface = gr.Interface(
|
109 |
# fn=yt_transcribe,
|
110 |
# inputs=[
|
111 |
-
# gr.components.Textbox(lines=1, placeholder="
|
112 |
-
# gr.components.Checkbox(label="
|
113 |
# ],
|
114 |
# examples=[["https://www.youtube.com/watch?v=mukeSSa5GKo"]],
|
115 |
# outputs=["html", "text"],
|
116 |
-
# title="Whisper Demo:
|
117 |
# description=(
|
118 |
-
# "
|
119 |
-
# f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME})
|
120 |
-
# "
|
121 |
# ),
|
122 |
# allow_flagging="never",
|
123 |
# )
|
124 |
|
125 |
-
#
|
126 |
demo.launch(share=share).queue()
|
|
|
1 |
import time
|
2 |
import os
|
3 |
+
import re
|
4 |
|
5 |
import torch
|
6 |
|
|
|
22 |
share = (os.environ.get("SHARE", "False")[0].lower() in "ty1") or None
|
23 |
auth_token = os.environ.get("AUTH_TOKEN") or True
|
24 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
25 |
+
print(f"Bruker enhet: {device}")
|
26 |
|
27 |
@spaces.GPU(duration=60 * 2)
|
28 |
def pipe(file, return_timestamps=False):
|
|
|
42 |
)
|
43 |
return asr(file, return_timestamps=return_timestamps, batch_size=24)
|
44 |
|
45 |
+
def format_output(text):
|
46 |
+
# Add a newline after ".", "!", ":", or "?", but not for multiple instances like "..."
|
47 |
+
text = re.sub(r'(?<!\.)[.!:?](?!\.)', lambda m: m.group() + '\n', text)
|
48 |
+
return text
|
49 |
+
|
50 |
def transcribe(file, return_timestamps=False):
|
51 |
if not return_timestamps:
|
52 |
text = pipe(file)["text"]
|
53 |
+
formatted_text = format_output(text)
|
54 |
else:
|
55 |
chunks = pipe(file, return_timestamps=True)["chunks"]
|
56 |
text = []
|
|
|
59 |
end_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][1])) if chunk["timestamp"][1] is not None else "??:??:??"
|
60 |
line = f"[{start_time} -> {end_time}] {chunk['text']}"
|
61 |
text.append(line)
|
62 |
+
formatted_text = "\n".join(text)
|
63 |
+
return formatted_text
|
64 |
|
65 |
def _return_yt_html_embed(yt_url):
|
66 |
video_id = yt_url.split("?v=")[-1]
|
|
|
90 |
|
91 |
return html_embed_str, text
|
92 |
|
93 |
+
# Lag Gradio-appen uten faner
|
94 |
|
95 |
demo = gr.Blocks()
|
96 |
|
|
|
99 |
fn=transcribe,
|
100 |
inputs=[
|
101 |
gr.components.Audio(sources=['upload', 'microphone'], type="filepath"),
|
102 |
+
gr.components.Checkbox(label="Inkluder tidsstempler"),
|
103 |
],
|
104 |
outputs="text",
|
105 |
title="NB-Whisper",
|
106 |
description=(
|
107 |
+
"Transkriber lange lydopptak fra mikrofon eller lydfiler med et enkelt klikk! Demoen bruker den fintunede"
|
108 |
+
f" modellen [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) og 🤗 Transformers til å transkribere lydfiler"
|
109 |
+
" av vilkårlig lengde."
|
110 |
),
|
111 |
allow_flagging="never",
|
112 |
)
|
113 |
|
114 |
+
# Uncomment to add the YouTube transcription interface if needed
|
115 |
# yt_transcribe_interface = gr.Interface(
|
116 |
# fn=yt_transcribe,
|
117 |
# inputs=[
|
118 |
+
# gr.components.Textbox(lines=1, placeholder="Lim inn URL til en YouTube-video her", label="YouTube URL"),
|
119 |
+
# gr.components.Checkbox(label="Inkluder tidsstempler"),
|
120 |
# ],
|
121 |
# examples=[["https://www.youtube.com/watch?v=mukeSSa5GKo"]],
|
122 |
# outputs=["html", "text"],
|
123 |
+
# title="Whisper Demo: Transkriber YouTube",
|
124 |
# description=(
|
125 |
+
# "Transkriber lange YouTube-videoer med et enkelt klikk! Demoen bruker den fintunede modellen:"
|
126 |
+
# f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) og 🤗 Transformers til å transkribere lydfiler av"
|
127 |
+
# " vilkårlig lengde."
|
128 |
# ),
|
129 |
# allow_flagging="never",
|
130 |
# )
|
131 |
|
132 |
+
# Start demoen uten faner
|
133 |
demo.launch(share=share).queue()
|