Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,66 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
import mimetypes
|
| 5 |
from pydub import AudioSegment
|
| 6 |
-
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
device = "cpu"
|
| 10 |
torch_dtype = torch.float32
|
| 11 |
|
| 12 |
-
# Load KB-Whisper model
|
| 13 |
model_id = "KBLab/kb-whisper-large"
|
| 14 |
|
| 15 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 16 |
-
model_id, torch_dtype=torch_dtype
|
| 17 |
-
).to(device)
|
| 18 |
-
|
| 19 |
processor = AutoProcessor.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
"automatic-speech-recognition",
|
| 23 |
-
model=model,
|
| 24 |
-
tokenizer=processor.tokenizer,
|
| 25 |
-
feature_extractor=processor.feature_extractor,
|
| 26 |
-
device=device,
|
| 27 |
-
torch_dtype=torch_dtype,
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
def transcribe(audio_path):
|
| 31 |
-
try:
|
| 32 |
-
# Get file extension
|
| 33 |
-
ext = os.path.splitext(audio_path)[1].lower()
|
| 34 |
-
|
| 35 |
-
# Convert to WAV if not already
|
| 36 |
-
if ext != ".wav":
|
| 37 |
-
try:
|
| 38 |
-
sound = AudioSegment.from_file(audio_path)
|
| 39 |
-
converted_path = audio_path.replace(ext, ".converted.wav")
|
| 40 |
-
sound.export(converted_path, format="wav")
|
| 41 |
-
audio_path = converted_path
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"Error converting audio to WAV: {str(e)}"
|
| 44 |
-
|
| 45 |
-
# Transcribe
|
| 46 |
-
result = pipe(audio_path, chunk_length_s=30, generate_kwargs={"task": "transcribe", "language": "sv"})
|
| 47 |
-
return result["text"]
|
| 48 |
-
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"Transcription failed: {str(e)}"
|
| 51 |
-
|
| 52 |
-
# Gradio UI
|
| 53 |
gr.Interface(
|
| 54 |
-
fn=
|
| 55 |
-
inputs=gr.Audio(type="filepath", label="Upload Audio
|
| 56 |
-
outputs=gr.Textbox(label="
|
| 57 |
-
title="Swedish
|
| 58 |
-
description="Supports .m4a, .mp3, .wav
|
| 59 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 6 |
+
import tempfile
|
| 7 |
+
import math
|
| 8 |
|
| 9 |
+
from datasets import load_dataset, Audio
|
| 10 |
+
import numpy as np
|
| 11 |
+
import torchaudio
|
| 12 |
+
|
| 13 |
+
# Set up model
|
| 14 |
device = "cpu"
|
| 15 |
torch_dtype = torch.float32
|
| 16 |
|
|
|
|
| 17 |
model_id = "KBLab/kb-whisper-large"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 20 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
|
| 21 |
+
|
| 22 |
+
# Helper: Split audio into chunks
|
| 23 |
+
def split_audio(audio_path, chunk_duration_ms=10000):
|
| 24 |
+
audio = AudioSegment.from_file(audio_path)
|
| 25 |
+
chunks = [audio[i:i + chunk_duration_ms] for i in range(0, len(audio), chunk_duration_ms)]
|
| 26 |
+
return chunks
|
| 27 |
+
|
| 28 |
+
# Helper: Transcribe a single chunk
|
| 29 |
+
def transcribe_chunk(chunk):
|
| 30 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
|
| 31 |
+
chunk.export(tmpfile.name, format="wav")
|
| 32 |
+
input_audio, _ = torchaudio.load(tmpfile.name)
|
| 33 |
+
input_features = processor(input_audio.squeeze(), sampling_rate=16000, return_tensors="pt").input_features
|
| 34 |
+
input_features = input_features.to(device)
|
| 35 |
+
predicted_ids = model.generate(input_features, task="transcribe", language="sv")
|
| 36 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 37 |
+
os.remove(tmpfile.name)
|
| 38 |
+
return transcription
|
| 39 |
+
|
| 40 |
+
# Full transcription function with progress
|
| 41 |
+
def transcribe_with_progress(audio_path, progress=gr.Progress()):
|
| 42 |
+
ext = os.path.splitext(audio_path)[1].lower()
|
| 43 |
+
if ext != ".wav":
|
| 44 |
+
sound = AudioSegment.from_file(audio_path)
|
| 45 |
+
audio_path = audio_path.replace(ext, ".converted.wav")
|
| 46 |
+
sound.export(audio_path, format="wav")
|
| 47 |
+
|
| 48 |
+
chunks = split_audio(audio_path, chunk_duration_ms=8000)
|
| 49 |
+
full_transcript = ""
|
| 50 |
+
total_chunks = len(chunks)
|
| 51 |
+
|
| 52 |
+
for i, chunk in enumerate(chunks):
|
| 53 |
+
partial_text = transcribe_chunk(chunk)
|
| 54 |
+
full_transcript += partial_text + " "
|
| 55 |
+
progress(i + 1, total_chunks) # Update progress bar
|
| 56 |
+
yield full_transcript.strip() # Stream updated text to UI
|
| 57 |
|
| 58 |
+
# UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
gr.Interface(
|
| 60 |
+
fn=transcribe_with_progress,
|
| 61 |
+
inputs=gr.Audio(type="filepath", label="Upload Swedish Audio"),
|
| 62 |
+
outputs=gr.Textbox(label="Live Transcript (Swedish)"),
|
| 63 |
+
title="Live Swedish Transcriber (KB-Whisper)",
|
| 64 |
+
description="Streams transcription word-by-word with visual progress. Supports .m4a, .mp3, .wav. May be slow on CPU.",
|
| 65 |
+
live=True
|
| 66 |
+
).launch()
|