created app.py
Browse filescreated

app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import torchaudio
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
import torch
|
7 |
+
|
8 |
+
# Create a Streamlit app title
|
9 |
+
st.title("ASR with Hugging Face Whisper")
|
10 |
+
|
11 |
+
# Load the ASR model
|
12 |
+
asr = pipeline(task = "automatic-speech-recognition", model="openai/whisper-large-v2",
|
13 |
+
device=0 if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
# Create a file uploader widget
|
16 |
+
uploaded_audio = st.file_uploader("Upload an audio file (wav/mp3)")
|
17 |
+
|
18 |
+
# Check if an audio file is uploaded
|
19 |
+
if uploaded_audio:
|
20 |
+
# Read the uploaded audio file
|
21 |
+
audio_data, sample_rate = torchaudio.load(uploaded_audio)
|
22 |
+
|
23 |
+
# Perform ASR on the uploaded audio
|
24 |
+
with st.spinner("Performing ASR..."):
|
25 |
+
transcriptions = asr(audio_data.numpy(), sample_rate=sample_rate)
|
26 |
+
|
27 |
+
# Display the ASR result
|
28 |
+
st.subheader("Transcription:")
|
29 |
+
for idx, transcription in enumerate(transcriptions):
|
30 |
+
st.write(f"Segment {idx + 1}: {transcription['text']}")
|
31 |
+
|
32 |
+
# Provide instructions
|
33 |
+
st.write("Instructions:")
|
34 |
+
st.write("1. Upload an audio file in WAV or MP3 format.")
|
35 |
+
st.write("2. Click the 'Perform ASR' button to transcribe the audio.")
|
36 |
+
|
37 |
+
# Add a sample audio file for testing (optional)
|
38 |
+
st.write("Sample Audio for Testing:")
|
39 |
+
sample_audio = "Wave_files_demos_Welcome.wav"
|
40 |
+
st.audio(sample_audio, format="audio/wav")
|
41 |
+
|
42 |
+
# Define the path to the sample audio file
|
43 |
+
sample_audio_path = os.path.join(os.getcwd(), sample_audio)
|
44 |
+
|
45 |
+
# Add a button to transcribe the sample audio (optional)
|
46 |
+
if st.button("Transcribe Sample Audio"):
|
47 |
+
# Read the sample audio file
|
48 |
+
sample_audio_data, sample_audio_rate = torchaudio.load(sample_audio_path)
|
49 |
+
|
50 |
+
# Perform ASR on the sample audio
|
51 |
+
with st.spinner("Performing ASR..."):
|
52 |
+
sample_transcriptions = asr(sample_audio_data.numpy(), sample_rate=sample_audio_rate)
|
53 |
+
|
54 |
+
# Display the ASR result for the sample audio
|
55 |
+
st.subheader("Transcription (Sample Audio):")
|
56 |
+
for idx, transcription in enumerate(sample_transcriptions):
|
57 |
+
st.write(f"Segment {idx + 1}: {transcription['text']}")
|