File size: 1,917 Bytes
19a77c0
509c3b8
b725c54
19a77c0
b725c54
 
e77a2ce
19a77c0
509c3b8
2ad3a18
b725c54
19a77c0
b725c54
 
 
 
 
 
19a77c0
 
 
509c3b8
19a77c0
 
509c3b8
19a77c0
 
 
509c3b8
19a77c0
 
509c3b8
19a77c0
 
2ad3a18
19a77c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ad3a18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import speech_recognition as sr
from gtts import gTTS
from langdetect import detect
import os
import tempfile

# Set up speech recognition
recognizer = sr.Recognizer()

# Function for speaking out text using Google Text-to-Speech (gTTS)
def speak_text(text):
    tts = gTTS(text=text, lang='en')
    # Save the speech to a temporary file
    with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
        tmpfile_name = tmpfile.name + '.mp3'
        tts.save(tmpfile_name)
        os.system(f"mpg321 {tmpfile_name}")  # You can use any audio player for this

# Function to handle speech recognition
def recognize_speech():
    with sr.Microphone() as source:
        st.write("Listening...")
        audio = recognizer.listen(source)
        try:
            st.write("Recognizing...")
            text = recognizer.recognize_google(audio)
            return text
        except sr.UnknownValueError:
            st.write("Sorry, I could not understand the audio.")
            return ""
        except sr.RequestError:
            st.write("Could not request results; check your internet connection.")
            return ""

# Function to detect language
def detect_language(text):
    try:
        lang = detect(text)
        return lang
    except Exception as e:
        st.write(f"Error in language detection: {e}")
        return "Unknown"

# Streamlit UI setup
st.title("Real-Time Speech Recognition and Language Detection")

# Add a button to start and stop speech recording
if st.button("Start Recording"):
    text = recognize_speech()
    if text:
        st.write(f"Recognized Text: {text}")
        
        # Detect language
        lang = detect_language(text)
        st.write(f"Detected Language: {lang}")
        
        # Speak the recognized text back
        speak_text(f"You said: {text}")
        
elif st.button("Stop Recording"):
    st.write("Stopped Recording")