Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import soundfile as sf
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
6 |
+
# Assuming you have your .env file configured with necessary API keys or configurations
|
7 |
+
# load_dotenv()
|
8 |
+
|
9 |
+
# Initialize the model outside the main app function to load it only once
|
10 |
+
|
11 |
+
|
12 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
13 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
14 |
+
|
15 |
+
model_id = "distil-whisper/distil-large-v2"
|
16 |
+
|
17 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
18 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
19 |
+
)
|
20 |
+
model.to(device)
|
21 |
+
|
22 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
23 |
+
|
24 |
+
pipe = pipeline(
|
25 |
+
"automatic-speech-recognition",
|
26 |
+
model=model,
|
27 |
+
tokenizer=processor.tokenizer,
|
28 |
+
feature_extractor=processor.feature_extractor,
|
29 |
+
max_new_tokens=128,
|
30 |
+
chunk_length_s=15,
|
31 |
+
batch_size=16,
|
32 |
+
torch_dtype=torch_dtype,
|
33 |
+
device=device,
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
def transcribe_audio(audio_file):
|
39 |
+
# Save the audio file to a temporary file
|
40 |
+
with open("temp_audio_file", "wb") as f:
|
41 |
+
f.write(audio_file.getbuffer())
|
42 |
+
|
43 |
+
# Transcribe the audio file using the Whisper model
|
44 |
+
result = pipe("temp_audio_file")
|
45 |
+
return result["text"]
|
46 |
+
|
47 |
+
# Streamlit app
|
48 |
+
def main():
|
49 |
+
st.title('BETTER TRANSCRIBER')
|
50 |
+
|
51 |
+
# Audio file uploader
|
52 |
+
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "m4a", "ogg", "flac"])
|
53 |
+
|
54 |
+
if uploaded_file is not None:
|
55 |
+
# Show a button to start the transcription process
|
56 |
+
if st.button('Transcribe'):
|
57 |
+
# Show a message while transcribing
|
58 |
+
with st.spinner('Transcribing...'):
|
59 |
+
text = transcribe_audio(uploaded_file)
|
60 |
+
|
61 |
+
# Show the transcription
|
62 |
+
st.subheader('Transcription:')
|
63 |
+
st.write(text)
|
64 |
+
else:
|
65 |
+
st.write('Upload an audio file to get started.')
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
main()
|