Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -54,102 +54,49 @@ CACHE_EXAMPLES = torch.device('cuda') and os.getenv("CACHE_EXAMPLES", "0") == "1
|
|
54 |
device = torch.device('cuda')
|
55 |
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
56 |
|
57 |
-
file = file_upload
|
58 |
-
|
59 |
-
def chunk_audio(file, chunk_length_ms=30000, overlap_length_ms=5000):
|
60 |
-
# -- pydub
|
61 |
-
audio = AudioSegment.from_file(file)
|
62 |
-
|
63 |
-
# -- create chunks with overlap
|
64 |
-
chunks = []
|
65 |
-
for i in range(0, len(audio), chunk_length_ms - overlap_length_ms):
|
66 |
-
start = max(0, i)
|
67 |
-
end = min(len(audio), i + chunk_length_ms)
|
68 |
-
chunks.append(audio[start:end])
|
69 |
-
|
70 |
-
return chunks
|
71 |
-
|
72 |
-
def transcribe(file_upload, progress=gr.Progress(track_tqdm=True)):
|
73 |
-
start_time = time.time()
|
74 |
-
|
75 |
-
# Load the speech recognition model
|
76 |
-
with torch.no_grad():
|
77 |
-
pipe = pipeline("automatic-speech-recognition", model="NbAiLab/nb-whisper-large", device=device)
|
78 |
-
|
79 |
-
# -- chunking
|
80 |
-
chunks = chunk_audio(file, chunk_length_ms=30000, overlap_length_ms=5000)
|
81 |
-
|
82 |
-
full_transcription = []
|
83 |
-
for chunk in chunks:
|
84 |
-
# -- convert to temporary file-like object
|
85 |
-
temp_audio = chunk.export(format="wav")
|
86 |
-
|
87 |
-
# -- transcribe chunk
|
88 |
-
text = pipe(temp_audio)["text"]
|
89 |
-
full_transcription.append(text)
|
90 |
-
|
91 |
-
# -- join
|
92 |
-
full_text = " ".join(full_transcription)
|
93 |
-
|
94 |
-
# -- timimg, word count
|
95 |
-
end_time = time.time()
|
96 |
-
output_time = end_time - start_time
|
97 |
-
word_count = len(full_text.split())
|
98 |
-
|
99 |
-
# -- metrics
|
100 |
-
memory = psutil.virtual_memory()
|
101 |
-
cpu_usage = psutil.cpu_percent(interval=1)
|
102 |
-
|
103 |
-
# --system info string
|
104 |
-
system_info = f"""
|
105 |
-
Processing time: {output_time:.2f} seconds.
|
106 |
-
Number of words: {word_count}
|
107 |
-
"""
|
108 |
-
|
109 |
-
return full_text, system_info
|
110 |
-
|
111 |
#@spaces.GPU
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
116 |
|
117 |
#--------------____________________________________________--------------"
|
118 |
|
119 |
-
|
120 |
-
|
121 |
|
122 |
-
|
123 |
|
124 |
#--------------____________________________________________--------------"
|
125 |
|
126 |
-
|
127 |
-
|
128 |
|
129 |
# --Word count
|
130 |
-
|
131 |
|
132 |
# --Memory metrics
|
133 |
-
|
134 |
|
135 |
# --CPU metric
|
136 |
-
|
137 |
|
138 |
# --GPU metric
|
139 |
-
|
140 |
|
141 |
# --system info string
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
|
147 |
#--------------____________________________________________--------------"
|
|
|
148 |
#CPU Usage: {cpu_usage}%
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
|
154 |
|
155 |
###############################################################################
|
|
|
54 |
device = torch.device('cuda')
|
55 |
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
#@spaces.GPU
|
58 |
+
def transcribe(file_upload, progress=gr.Progress(track_tqdm=True)): # microphone
|
59 |
|
60 |
+
file = file_upload # microphone if microphone is not None else
|
61 |
+
start_time = time.time()
|
62 |
|
63 |
#--------------____________________________________________--------------"
|
64 |
|
65 |
+
with torch.no_grad():
|
66 |
+
pipe = pipeline("automatic-speech-recognition", model="NbAiLab/nb-whisper-large", device=device)
|
67 |
|
68 |
+
text = pipe(file)["text"]
|
69 |
|
70 |
#--------------____________________________________________--------------"
|
71 |
|
72 |
+
end_time = time.time()
|
73 |
+
output_time = end_time - start_time
|
74 |
|
75 |
# --Word count
|
76 |
+
word_count = len(text.split())
|
77 |
|
78 |
# --Memory metrics
|
79 |
+
memory = psutil.virtual_memory()
|
80 |
|
81 |
# --CPU metric
|
82 |
+
cpu_usage = psutil.cpu_percent(interval=1)
|
83 |
|
84 |
# --GPU metric
|
85 |
+
gpu_utilization, gpu_memory = GPUInfo.gpu_usage()
|
86 |
|
87 |
# --system info string
|
88 |
+
system_info = f"""
|
89 |
+
Processing time: {output_time:.2f} seconds.
|
90 |
+
Number of words: {word_count}
|
91 |
+
GPU Memory: {gpu_memory}"""
|
92 |
|
93 |
#--------------____________________________________________--------------"
|
94 |
+
|
95 |
#CPU Usage: {cpu_usage}%
|
96 |
+
Memory used: {memory.percent}%
|
97 |
+
GPU Utilization: {gpu_utilization}%
|
98 |
+
|
99 |
+
return text, system_info
|
100 |
|
101 |
|
102 |
###############################################################################
|