rajsecrets0 commited on
Commit
9bd361c
·
verified ·
1 Parent(s): 646dcf6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import speech_recognition as sr
3
+ import os
4
+ import tempfile
5
+ import librosa
6
+ import soundfile as sf
7
+
8
+ def convert_to_wav(audio_file):
9
+ # Load the audio file
10
+ y, sr = librosa.load(audio_file, sr=None)
11
+
12
+ # Create a temporary WAV file
13
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_wav:
14
+ sf.write(tmp_wav.name, y, sr, format='wav')
15
+ return tmp_wav.name
16
+
17
+ def transcribe_audio(audio_file):
18
+ recognizer = sr.Recognizer()
19
+ with sr.AudioFile(audio_file) as source:
20
+ audio = recognizer.record(source)
21
+ try:
22
+ return recognizer.recognize_google(audio)
23
+ except sr.UnknownValueError:
24
+ return "Speech recognition could not understand the audio"
25
+ except sr.RequestError as e:
26
+ return f"Could not request results from speech recognition service; {e}"
27
+
28
+ def main():
29
+ st.title("Speech-to-Text Converter")
30
+
31
+ uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "m4a", "ogg", "flac"])
32
+
33
+ if uploaded_file is not None:
34
+ st.audio(uploaded_file)
35
+
36
+ if st.button("Transcribe"):
37
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.' + uploaded_file.name.split('.')[-1]) as tmp_file:
38
+ tmp_file.write(uploaded_file.getvalue())
39
+ tmp_file_path = tmp_file.name
40
+
41
+ try:
42
+ # Convert to WAV
43
+ wav_file_path = convert_to_wav(tmp_file_path)
44
+
45
+ # Transcribe
46
+ transcription = transcribe_audio(wav_file_path)
47
+
48
+ st.write("Transcription:")
49
+ st.write(transcription)
50
+
51
+ except Exception as e:
52
+ st.error(f"An error occurred: {str(e)}")
53
+
54
+ finally:
55
+ # Clean up
56
+ os.unlink(tmp_file_path)
57
+ if 'wav_file_path' in locals():
58
+ os.unlink(wav_file_path)
59
+
60
+ if __name__ == "__main__":
61
+ main()