Rahul-Crudcook
commited on
Commit
•
727f85b
1
Parent(s):
07829aa
Update app.py
Browse files
app.py
CHANGED
@@ -11,18 +11,14 @@ st.title("Speech-to-Text with Translation to English")
|
|
11 |
# Initialize recognizer
|
12 |
recognizer = sr.Recognizer()
|
13 |
|
14 |
-
# Choice for input method
|
15 |
-
input_method = st.radio("Select Input Method", ("Record from Microphone", "Upload Audio File"))
|
16 |
-
|
17 |
# Choice for input language
|
18 |
language_options = {"English": "en", "Hindi": "hi"}
|
19 |
input_language = st.selectbox("Select Input Language", options=language_options.keys())
|
20 |
selected_lang_code = language_options[input_language]
|
21 |
|
22 |
# Function to convert audio chunk to text
|
23 |
-
def speech_to_text(audio_data, lang="en"):
|
24 |
try:
|
25 |
-
# Recognize speech
|
26 |
st.info("Converting speech to text...")
|
27 |
detected_text = recognizer.recognize_google(audio_data, language=lang)
|
28 |
return detected_text
|
@@ -30,61 +26,33 @@ def speech_to_text(audio_data, lang="en"): # Default language to English
|
|
30 |
st.error(f"Error in speech recognition: {e}")
|
31 |
return None
|
32 |
|
33 |
-
# Handle recording from microphone
|
34 |
-
if input_method == "Record from Microphone":
|
35 |
-
if st.button("Start Recording"):
|
36 |
-
with st.spinner("Recording... Please speak into the microphone."):
|
37 |
-
try:
|
38 |
-
# Capture audio input from the microphone
|
39 |
-
with sr.Microphone() as source:
|
40 |
-
st.info("Listening... Please speak now.")
|
41 |
-
recognizer.adjust_for_ambient_noise(source) # Adjust for background noise
|
42 |
-
audio_data = recognizer.listen(source)
|
43 |
-
st.success("Recording complete!")
|
44 |
-
|
45 |
-
# Process and convert speech to text
|
46 |
-
detected_text = speech_to_text(audio_data, lang=selected_lang_code)
|
47 |
-
if detected_text:
|
48 |
-
st.write("Detected Speech Text:", detected_text)
|
49 |
-
|
50 |
-
# Translate to English
|
51 |
-
translator = GoogleTranslator(source='auto', target='en')
|
52 |
-
translated_text = translator.translate(detected_text)
|
53 |
-
st.write("Translated Text (English):", translated_text)
|
54 |
-
|
55 |
-
except sr.UnknownValueError:
|
56 |
-
st.error("Could not understand the audio. Please try again.")
|
57 |
-
except sr.RequestError as e:
|
58 |
-
st.error(f"Could not request results from the service; {e}")
|
59 |
-
|
60 |
# Process uploaded audio file
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
st.error(f"Error processing the audio file: {e}")
|
|
|
11 |
# Initialize recognizer
|
12 |
recognizer = sr.Recognizer()
|
13 |
|
|
|
|
|
|
|
14 |
# Choice for input language
|
15 |
language_options = {"English": "en", "Hindi": "hi"}
|
16 |
input_language = st.selectbox("Select Input Language", options=language_options.keys())
|
17 |
selected_lang_code = language_options[input_language]
|
18 |
|
19 |
# Function to convert audio chunk to text
|
20 |
+
def speech_to_text(audio_data, lang="en"):
|
21 |
try:
|
|
|
22 |
st.info("Converting speech to text...")
|
23 |
detected_text = recognizer.recognize_google(audio_data, language=lang)
|
24 |
return detected_text
|
|
|
26 |
st.error(f"Error in speech recognition: {e}")
|
27 |
return None
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Process uploaded audio file
|
30 |
+
uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "ogg"])
|
31 |
+
if uploaded_file:
|
32 |
+
with st.spinner("Processing uploaded audio..."):
|
33 |
+
try:
|
34 |
+
# Convert uploaded file to WAV format using pydub
|
35 |
+
audio = AudioSegment.from_file(BytesIO(uploaded_file.read()))
|
36 |
+
# Split audio into 30-second chunks
|
37 |
+
chunk_duration_ms = 30000
|
38 |
+
chunks = [audio[i:i+chunk_duration_ms] for i in range(0, len(audio), chunk_duration_ms)]
|
39 |
+
text_output = ""
|
40 |
+
|
41 |
+
for i, chunk in enumerate(chunks):
|
42 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_wav_file:
|
43 |
+
chunk.export(tmp_wav_file.name, format="wav")
|
44 |
+
with sr.AudioFile(tmp_wav_file.name) as source:
|
45 |
+
audio_data = recognizer.record(source)
|
46 |
+
detected_text = speech_to_text(audio_data, lang=selected_lang_code)
|
47 |
+
if detected_text:
|
48 |
+
text_output += detected_text + " "
|
49 |
+
|
50 |
+
# Display detected text and translate
|
51 |
+
if text_output:
|
52 |
+
st.write("Detected Speech Text:", text_output)
|
53 |
+
translator = GoogleTranslator(source='auto', target='en')
|
54 |
+
translated_text = translator.translate(text_output)
|
55 |
+
st.write("Translated Text (English):", translated_text)
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
st.error(f"Error processing the audio file: {e}")
|
|