zaiffi commited on
Commit
c142ac1
·
1 Parent(s): b1ba922

Microphone not available in Hugging Face message added

Browse files
Files changed (1) hide show
  1. app.py +31 -12
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
- with sr.Microphone() as source:
54
- st.write("Listening...")
55
- audio = recognizer.listen(source)
56
- try:
57
- text = recognizer.recognize_google(audio, language=language_code)
58
- st.write(f"You said: {text}")
59
- return text
60
- except Exception as e:
61
- st.write("Sorry, I couldn't understand the audio.")
62
- return None
 
 
 
 
 
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):