Microphone not available in Hugging Face message added
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import os
|
|
8 |
import datetime
|
9 |
import json
|
10 |
from dotenv import load_dotenv
|
|
|
11 |
|
12 |
# Load environment variables from .env file
|
13 |
load_dotenv()
|
@@ -47,19 +48,37 @@ language_map = {
|
|
47 |
'Urdu': 'ur'
|
48 |
}
|
49 |
|
50 |
-
# Function to recognize speech from the microphone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def recognize_speech(language_code):
|
52 |
-
"""Listen to the microphone and return the recognized text in the selected language"""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# Function to toggle the listening state
|
65 |
def toggle_listen(button_key):
|
|
|
8 |
import datetime
|
9 |
import json
|
10 |
from dotenv import load_dotenv
|
11 |
+
import sys
|
12 |
|
13 |
# Load environment variables from .env file
|
14 |
load_dotenv()
|
|
|
48 |
'Urdu': 'ur'
|
49 |
}
|
50 |
|
51 |
+
# # Function to recognize speech from the microphone
|
52 |
+
# def recognize_speech(language_code):
|
53 |
+
# """Listen to the microphone and return the recognized text in the selected language"""
|
54 |
+
# with sr.Microphone() as source:
|
55 |
+
# st.write("Listening...")
|
56 |
+
# audio = recognizer.listen(source)
|
57 |
+
# try:
|
58 |
+
# text = recognizer.recognize_google(audio, language=language_code)
|
59 |
+
# st.write(f"You said: {text}")
|
60 |
+
# return text
|
61 |
+
# except Exception as e:
|
62 |
+
# st.write("Sorry, I couldn't understand the audio.")
|
63 |
+
# return None
|
64 |
+
|
65 |
def recognize_speech(language_code):
|
66 |
+
"""Listen to the microphone and return the recognized text in the selected language."""
|
67 |
+
try:
|
68 |
+
with sr.Microphone() as source:
|
69 |
+
st.write("Listening...")
|
70 |
+
audio = recognizer.listen(source)
|
71 |
+
try:
|
72 |
+
text = recognizer.recognize_google(audio, language=language_code)
|
73 |
+
st.write(f"You said: {text}")
|
74 |
+
return text
|
75 |
+
except Exception as e:
|
76 |
+
st.write("Sorry, I couldn't understand the audio.")
|
77 |
+
return None
|
78 |
+
except OSError as e:
|
79 |
+
# If microphone is not available, show a message to use text input
|
80 |
+
st.write("Microphone not detected in this environment. Please use text input instead.")
|
81 |
+
return None
|
82 |
|
83 |
# Function to toggle the listening state
|
84 |
def toggle_listen(button_key):
|