Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import tempfile
|
|
7 |
import shutil
|
8 |
import re
|
9 |
import speech_recognition as sr # Library for voice recognition
|
|
|
10 |
|
11 |
def translate_to_japanese(api_key, text):
|
12 |
"""
|
@@ -77,16 +78,23 @@ def generate_audio_from_text(text):
|
|
77 |
tts.save(temp_audio_file.name)
|
78 |
return temp_audio_file.name
|
79 |
|
80 |
-
# Function to capture and transcribe voice input
|
81 |
def transcribe_voice_input():
|
82 |
recognizer = sr.Recognizer()
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
try:
|
88 |
-
|
89 |
-
text = recognizer.recognize_google(audio)
|
90 |
st.success(f"Transcribed text: {text}")
|
91 |
return text
|
92 |
except sr.UnknownValueError:
|
|
|
7 |
import shutil
|
8 |
import re
|
9 |
import speech_recognition as sr # Library for voice recognition
|
10 |
+
import sounddevice as sd
|
11 |
|
12 |
def translate_to_japanese(api_key, text):
|
13 |
"""
|
|
|
78 |
tts.save(temp_audio_file.name)
|
79 |
return temp_audio_file.name
|
80 |
|
|
|
81 |
def transcribe_voice_input():
|
82 |
recognizer = sr.Recognizer()
|
83 |
+
|
84 |
+
# Set parameters for sounddevice (e.g., 44100 Hz, 1 channel)
|
85 |
+
duration = 5 # Seconds to record
|
86 |
+
fs = 44100 # Sampling frequency
|
87 |
+
|
88 |
+
st.info("Please speak now...")
|
89 |
+
|
90 |
+
# Record the audio using sounddevice
|
91 |
+
audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
|
92 |
+
sd.wait() # Wait until recording is finished
|
93 |
+
|
94 |
+
# Convert the numpy array to audio and recognize the speech
|
95 |
+
with sr.AudioData(audio_data.tobytes(), fs, 2) as source:
|
96 |
try:
|
97 |
+
text = recognizer.recognize_google(source)
|
|
|
98 |
st.success(f"Transcribed text: {text}")
|
99 |
return text
|
100 |
except sr.UnknownValueError:
|