Spaces:
Sleeping
Sleeping
aldan.creo
commited on
Commit
·
21eb51f
1
Parent(s):
7e7acc6
Highlight
Browse files
app.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import numpy as np
|
|
|
|
|
|
|
|
|
4 |
|
5 |
transcriber = pipeline(
|
6 |
"automatic-speech-recognition",
|
@@ -10,10 +14,8 @@ transcriber = pipeline(
|
|
10 |
|
11 |
|
12 |
def transcribe_live(state, words_list, new_chunk):
|
13 |
-
print(f"state: {state}")
|
14 |
-
|
15 |
try:
|
16 |
-
words_to_check_for = [word.strip() for word in words_list.split(",")]
|
17 |
except:
|
18 |
gr.Warning("Please enter a valid list of words to check for")
|
19 |
words_to_check_for = []
|
@@ -50,24 +52,49 @@ def transcribe_live(state, words_list, new_chunk):
|
|
50 |
print(f"Transcription failed. Error: {e}")
|
51 |
return state, previous_counts_of_words, previous_transcription
|
52 |
|
53 |
-
print(f"new transcription: {new_transcription}")
|
54 |
full_transcription_text = new_transcription["text"]
|
55 |
|
56 |
full_transcription_text_lower = full_transcription_text.lower()
|
57 |
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
}
|
61 |
|
62 |
new_state = {
|
63 |
"stream": stream,
|
64 |
"full_transcription": full_transcription_text,
|
65 |
"counts_of_words": new_counts_of_words,
|
|
|
66 |
}
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
|
72 |
|
73 |
with gr.Blocks() as demo:
|
@@ -82,14 +109,24 @@ with gr.Blocks() as demo:
|
|
82 |
recording = gr.Audio(streaming=True, label="Recording")
|
83 |
|
84 |
word_counts = gr.JSON(label="Filler words count", value={})
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
recording.stream(
|
88 |
transcribe_live,
|
89 |
inputs=[state, filler_words, recording],
|
90 |
-
outputs=[state, word_counts, transcription],
|
91 |
stream_every=5,
|
92 |
-
time_limit
|
93 |
)
|
94 |
|
95 |
demo.launch(show_error=True)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import re
|
6 |
+
from collections import Counter
|
7 |
+
from functools import reduce
|
8 |
|
9 |
transcriber = pipeline(
|
10 |
"automatic-speech-recognition",
|
|
|
14 |
|
15 |
|
16 |
def transcribe_live(state, words_list, new_chunk):
|
|
|
|
|
17 |
try:
|
18 |
+
words_to_check_for = [word.strip().lower() for word in words_list.split(",")]
|
19 |
except:
|
20 |
gr.Warning("Please enter a valid list of words to check for")
|
21 |
words_to_check_for = []
|
|
|
52 |
print(f"Transcription failed. Error: {e}")
|
53 |
return state, previous_counts_of_words, previous_transcription
|
54 |
|
|
|
55 |
full_transcription_text = new_transcription["text"]
|
56 |
|
57 |
full_transcription_text_lower = full_transcription_text.lower()
|
58 |
|
59 |
+
# Use re to find all the words in the transcription, and their start and end indices
|
60 |
+
matches: list[re.Match] = list(
|
61 |
+
re.finditer(
|
62 |
+
r"\b(" + "|".join(words_to_check_for) + r")\b",
|
63 |
+
full_transcription_text_lower,
|
64 |
+
)
|
65 |
+
)
|
66 |
+
|
67 |
+
counter = Counter(
|
68 |
+
match.group(0) for match in matches if match.group(0) in words_to_check_for
|
69 |
+
)
|
70 |
+
|
71 |
+
new_counts_of_words = {word: counter.get(word, 0) for word in words_to_check_for}
|
72 |
+
|
73 |
+
new_highlighted_transcription = {
|
74 |
+
"text": full_transcription_text,
|
75 |
+
"entities": [
|
76 |
+
{
|
77 |
+
"entity": "FILLER",
|
78 |
+
"start": match.start(),
|
79 |
+
"end": match.end(),
|
80 |
+
}
|
81 |
+
for match in matches
|
82 |
+
],
|
83 |
}
|
84 |
|
85 |
new_state = {
|
86 |
"stream": stream,
|
87 |
"full_transcription": full_transcription_text,
|
88 |
"counts_of_words": new_counts_of_words,
|
89 |
+
"highlighted_transcription": new_highlighted_transcription,
|
90 |
}
|
91 |
|
92 |
+
return (
|
93 |
+
new_state,
|
94 |
+
new_counts_of_words,
|
95 |
+
full_transcription_text,
|
96 |
+
new_highlighted_transcription,
|
97 |
+
)
|
98 |
|
99 |
|
100 |
with gr.Blocks() as demo:
|
|
|
109 |
recording = gr.Audio(streaming=True, label="Recording")
|
110 |
|
111 |
word_counts = gr.JSON(label="Filler words count", value={})
|
112 |
+
# word_counts = gr.BarPlot(label="Filler words count", value={})
|
113 |
+
transcription = gr.Textbox(label="Transcription", value="", visible=False)
|
114 |
+
|
115 |
+
highlighted_transcription = gr.HighlightedText(
|
116 |
+
label="Transcription",
|
117 |
+
value={
|
118 |
+
"text": "",
|
119 |
+
"entities": [],
|
120 |
+
},
|
121 |
+
color_map={"FILLER": "red"},
|
122 |
+
)
|
123 |
|
124 |
recording.stream(
|
125 |
transcribe_live,
|
126 |
inputs=[state, filler_words, recording],
|
127 |
+
outputs=[state, word_counts, transcription, highlighted_transcription],
|
128 |
stream_every=5,
|
129 |
+
time_limit=-1,
|
130 |
)
|
131 |
|
132 |
demo.launch(show_error=True)
|