Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
6 |
+
from audiorecorder import audiorecorder
|
7 |
+
from pydub import AudioSegment
|
8 |
+
|
9 |
+
# Setup model
|
10 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
12 |
+
model_id = "KBLab/kb-whisper-tiny"
|
13 |
+
|
14 |
+
@st.cache_resource
|
15 |
+
def load_model():
|
16 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
17 |
+
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache"
|
18 |
+
)
|
19 |
+
model.to(device)
|
20 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
21 |
+
return pipeline(
|
22 |
+
"automatic-speech-recognition",
|
23 |
+
model=model,
|
24 |
+
tokenizer=processor.tokenizer,
|
25 |
+
feature_extractor=processor.feature_extractor,
|
26 |
+
torch_dtype=torch_dtype,
|
27 |
+
device=device,
|
28 |
+
)
|
29 |
+
|
30 |
+
pipe = load_model()
|
31 |
+
|
32 |
+
def transcribe_audio(audio_path):
|
33 |
+
return pipe(audio_path, chunk_length_s=30, generate_kwargs={"task": "transcribe", "language": "sv"})
|
34 |
+
|
35 |
+
st.title("Speech-to-Text Transcription")
|
36 |
+
|
37 |
+
# Audio recording
|
38 |
+
st.subheader("Record Audio")
|
39 |
+
recorded_audio = audiorecorder("Start Recording", "Stop Recording")
|
40 |
+
|
41 |
+
if recorded_audio:
|
42 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
43 |
+
temp_file.write(recorded_audio.tobytes())
|
44 |
+
temp_file_path = temp_file.name
|
45 |
+
st.audio(temp_file_path, format="audio/wav")
|
46 |
+
result = transcribe_audio(temp_file_path)
|
47 |
+
st.write("### Transcription:")
|
48 |
+
st.write(result["text"])
|
49 |
+
os.remove(temp_file_path)
|
50 |
+
|
51 |
+
# File upload
|
52 |
+
st.subheader("Upload Audio File")
|
53 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "ogg", "flac"])
|
54 |
+
|
55 |
+
if uploaded_file:
|
56 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[-1]) as temp_file:
|
57 |
+
temp_file.write(uploaded_file.read())
|
58 |
+
temp_file_path = temp_file.name
|
59 |
+
st.audio(temp_file_path)
|
60 |
+
result = transcribe_audio(temp_file_path)
|
61 |
+
st.write("### Transcription:")
|
62 |
+
st.write(result["text"])
|
63 |
+
os.remove(temp_file_path)
|