Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,61 @@
|
|
| 1 |
-
|
| 2 |
import speech_recognition as sr
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import tempfile
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def speak_text(text):
|
| 11 |
-
tts = gTTS(text=text, lang='en')
|
| 12 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 13 |
-
tts.save(temp_file.name)
|
| 14 |
-
os.system(f"mpg321 {temp_file.name}") # Plays the saved audio file
|
| 15 |
-
os.remove(temp_file.name) # Deletes the file after playing
|
| 16 |
-
|
| 17 |
-
# Initialize recognizer
|
| 18 |
recognizer = sr.Recognizer()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with sr.Microphone() as source:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
audio_data = recognizer.listen(source)
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print(f"Recognized text: {text}")
|
| 33 |
-
# Respond with text-to-speech
|
| 34 |
-
speak_text(f"You said: {text}")
|
| 35 |
-
return jsonify({"text": text, "status": "success"})
|
| 36 |
-
|
| 37 |
except sr.UnknownValueError:
|
| 38 |
-
|
|
|
|
| 39 |
except sr.RequestError:
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
def
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import speech_recognition as sr
|
| 3 |
+
import pyttsx3
|
| 4 |
+
from langdetect import detect
|
|
|
|
| 5 |
|
| 6 |
+
# Initialize text-to-speech engine
|
| 7 |
+
engine = pyttsx3.init()
|
| 8 |
|
| 9 |
+
# Set up speech recognition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
recognizer = sr.Recognizer()
|
| 11 |
|
| 12 |
+
# Function for speaking out text
|
| 13 |
+
def speak_text(text):
|
| 14 |
+
engine.say(text)
|
| 15 |
+
engine.runAndWait()
|
| 16 |
+
|
| 17 |
+
# Function to handle speech recognition
|
| 18 |
+
def recognize_speech():
|
| 19 |
with sr.Microphone() as source:
|
| 20 |
+
st.write("Listening...")
|
| 21 |
+
audio = recognizer.listen(source)
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
+
st.write("Recognizing...")
|
| 24 |
+
text = recognizer.recognize_google(audio)
|
| 25 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except sr.UnknownValueError:
|
| 27 |
+
st.write("Sorry, I could not understand the audio.")
|
| 28 |
+
return ""
|
| 29 |
except sr.RequestError:
|
| 30 |
+
st.write("Could not request results; check your internet connection.")
|
| 31 |
+
return ""
|
| 32 |
|
| 33 |
+
# Function to detect language
|
| 34 |
+
def detect_language(text):
|
| 35 |
+
try:
|
| 36 |
+
lang = detect(text)
|
| 37 |
+
return lang
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.write(f"Error in language detection: {e}")
|
| 40 |
+
return "Unknown"
|
| 41 |
+
|
| 42 |
+
# Streamlit UI setup
|
| 43 |
+
st.title("Real-Time Speech Recognition and Language Detection")
|
| 44 |
+
|
| 45 |
+
# Add a button to start and stop speech recording
|
| 46 |
+
if st.button("Start Recording"):
|
| 47 |
+
text = recognize_speech()
|
| 48 |
+
if text:
|
| 49 |
+
st.write(f"Recognized Text: {text}")
|
| 50 |
+
|
| 51 |
+
# Detect language
|
| 52 |
+
lang = detect_language(text)
|
| 53 |
+
st.write(f"Detected Language: {lang}")
|
| 54 |
+
|
| 55 |
+
# Speak the recognized text back
|
| 56 |
+
speak_text(f"You said: {text}")
|
| 57 |
+
|
| 58 |
+
elif st.button("Stop Recording"):
|
| 59 |
+
st.write("Stopped Recording")
|
| 60 |
|
| 61 |
+
# Note: If you need to implement a live transcription service (continuous listening), you can modify the flow above to keep listening in a loop.
|
|
|