Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from pydub import AudioSegment
|
4 |
+
|
5 |
+
def convert_audio_to_wav(audio_file):
|
6 |
+
audio = AudioSegment.from_file(audio_file)
|
7 |
+
wav_file = audio_file.name.split(".")[0] + ".wav"
|
8 |
+
audio.export(wav_file, format="wav")
|
9 |
+
return wav_file
|
10 |
+
|
11 |
+
def speech_to_text(audio_file):
|
12 |
+
recognizer = sr.Recognizer()
|
13 |
+
with sr.AudioFile(audio_file) as source:
|
14 |
+
audio = recognizer.record(source)
|
15 |
+
try:
|
16 |
+
text = recognizer.recognize_google(audio)
|
17 |
+
return text
|
18 |
+
except sr.UnknownValueError:
|
19 |
+
return "Could not understand audio"
|
20 |
+
except sr.RequestError as e:
|
21 |
+
return f"Error: {str(e)}"
|
22 |
+
|
23 |
+
def main():
|
24 |
+
st.title("Speech to Text Converter")
|
25 |
+
st.write("Upload an audio file and convert it to text.")
|
26 |
+
|
27 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
file_details = {"Filename": uploaded_file.name, "FileType": uploaded_file.type}
|
31 |
+
st.write(file_details)
|
32 |
+
|
33 |
+
if uploaded_file.type == "audio/mp3":
|
34 |
+
uploaded_file = convert_audio_to_wav(uploaded_file)
|
35 |
+
|
36 |
+
text = speech_to_text(uploaded_file)
|
37 |
+
st.write("Converted Text:")
|
38 |
+
st.write(text)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
main()
|