Download app.py from Sib83/French: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/spaces/Sib83/French/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sib83/French/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sib83/French/resolve/main/app.py
1.92 kB
| 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") | |