Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,14 +3,34 @@ asr = pipeline(task="automatic-speech-recognition",
|
|
3 |
model="distil-whisper/distil-small.en")
|
4 |
import os
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
demo = gr.Blocks()
|
8 |
def transcribe_speech(filepath):
|
9 |
if filepath is None:
|
10 |
gr.Warning("No audio found, please retry.")
|
11 |
return ""
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
mic_transcribe = gr.Interface(
|
16 |
fn=transcribe_speech,
|
|
|
3 |
model="distil-whisper/distil-small.en")
|
4 |
import os
|
5 |
import gradio as gr
|
6 |
+
from pydub import AudioSegment
|
7 |
|
8 |
demo = gr.Blocks()
|
9 |
def transcribe_speech(filepath):
|
10 |
if filepath is None:
|
11 |
gr.Warning("No audio found, please retry.")
|
12 |
return ""
|
13 |
+
|
14 |
+
# Load audio using pydub
|
15 |
+
audio = AudioSegment.from_file(filepath)
|
16 |
+
chunk_length_ms = 30 * 1000 # 30 seconds
|
17 |
+
chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
|
18 |
+
|
19 |
+
full_transcription = ""
|
20 |
+
|
21 |
+
for i, chunk in enumerate(chunks):
|
22 |
+
# Export chunk to temporary wav file
|
23 |
+
chunk_path = f"chunk_{i}.wav"
|
24 |
+
chunk.export(chunk_path, format="wav")
|
25 |
+
|
26 |
+
# Transcribe the chunk
|
27 |
+
result = asr(chunk_path)
|
28 |
+
full_transcription += result["text"] + " "
|
29 |
+
|
30 |
+
# Clean up (optional)
|
31 |
+
os.remove(chunk_path)
|
32 |
+
|
33 |
+
return full_transcription.strip()
|
34 |
|
35 |
mic_transcribe = gr.Interface(
|
36 |
fn=transcribe_speech,
|