Ansaribinhyder commited on
Commit
facadcf
·
1 Parent(s): b7234f0

With updated Mic Code

Browse files
Files changed (1) hide show
  1. app.py +144 -40
app.py CHANGED
@@ -3,13 +3,13 @@ import speech_recognition as sr
3
  from googletrans import Translator
4
  import requests
5
  import os
6
-
7
  app = Flask(__name__)
8
-
9
  # Initialize recognizer and translator
10
  recognizer = sr.Recognizer()
11
  translator = Translator()
12
-
13
  # Language settings mapping
14
  language_mapping = {
15
  1: ('ta-IN', 'ta', 'en'), # Tamil
@@ -17,57 +17,161 @@ language_mapping = {
17
  3: ('hi-IN', 'hi', 'en'), # Hindi
18
  4: ('ms-MY', 'ms', 'en') # Malay
19
  }
20
-
 
 
 
 
 
 
 
 
 
21
  @app.route('/')
22
  def index():
23
  return render_template("index.html")
24
-
25
  @app.route('/speech-to-text', methods=['POST'])
26
  def speech_to_text():
27
  """Convert speech to text, translate, and query the /ask endpoint"""
28
- language = int(request.form.get("language", 0))
29
-
30
- if language not in language_mapping:
31
- return jsonify({"error": "Invalid language selection"}), 400
32
-
33
- recognition_lang, src_lang, dest_lang = language_mapping[language]
34
-
35
- with sr.Microphone() as source:
36
- print(f"Listening for {recognition_lang}...")
37
- recognizer.adjust_for_ambient_noise(source)
38
- audio = recognizer.listen(source)
39
-
40
- try:
 
 
 
 
 
41
  text = recognizer.recognize_google(audio, language=recognition_lang)
42
  print(f"Recognized: {text}")
43
-
44
  # Translate text
45
  translated = translator.translate(text, src=src_lang, dest=dest_lang)
46
  print(f"Translated: {translated.text}")
47
-
48
- # Send to /ask API
49
  API_KEY = os.getenv("RAG_API_KEY")
50
-
51
- # Hugging Face API endpoint
52
- url = "https://aitestingworkspace-aiagentchatup.hf.space/ask"
53
-
54
- # Headers with authentication
55
- headers = {
56
- "Authorization": f"Bearer {API_KEY}"
57
- }
58
-
59
- response = requests.post(url=url,data={"question": translated.text},headers=headers)
60
-
61
  return jsonify({
62
  "recognized_text": text,
63
  "translated_text": translated.text,
64
- "server_response": response.json()['answer']
65
  })
66
-
67
- except sr.UnknownValueError:
68
- return jsonify({"error": "Could not understand audio"}), 400
69
- except Exception as e:
70
- return jsonify({"error": str(e)}), 500
71
-
 
 
 
 
72
  if __name__ == "__main__":
73
- app.run(host="0.0.0.0", port=5000, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from googletrans import Translator
4
  import requests
5
  import os
6
+
7
  app = Flask(__name__)
8
+
9
  # Initialize recognizer and translator
10
  recognizer = sr.Recognizer()
11
  translator = Translator()
12
+
13
  # Language settings mapping
14
  language_mapping = {
15
  1: ('ta-IN', 'ta', 'en'), # Tamil
 
17
  3: ('hi-IN', 'hi', 'en'), # Hindi
18
  4: ('ms-MY', 'ms', 'en') # Malay
19
  }
20
+
21
+ # Check microphone availability at startup
22
+ try:
23
+ mic_list = sr.Microphone.list_microphone_names()
24
+ print("Available microphones:", mic_list)
25
+ if not mic_list:
26
+ print("WARNING: No microphones detected - audio input disabled")
27
+ except OSError as e:
28
+ print(f"Microphone check failed: {str(e)}")
29
+
30
  @app.route('/')
31
  def index():
32
  return render_template("index.html")
33
+
34
  @app.route('/speech-to-text', methods=['POST'])
35
  def speech_to_text():
36
  """Convert speech to text, translate, and query the /ask endpoint"""
37
+ try:
38
+ language = int(request.form.get("language", 0))
39
+ if language not in language_mapping:
40
+ return jsonify({"error": "Invalid language selection"}), 400
41
+
42
+ recognition_lang, src_lang, dest_lang = language_mapping[language]
43
+
44
+ # Get available microphone devices
45
+ mic_devices = sr.Microphone.list_microphone_names()
46
+ if not mic_devices:
47
+ return jsonify({"error": "No audio input devices available"}), 400
48
+
49
+ # Use first available microphone explicitly
50
+ with sr.Microphone(device_index=0) as source:
51
+ print(f"Listening for {recognition_lang}...")
52
+ recognizer.adjust_for_ambient_noise(source, duration=1)
53
+ audio = recognizer.listen(source, timeout=10)
54
+
55
  text = recognizer.recognize_google(audio, language=recognition_lang)
56
  print(f"Recognized: {text}")
57
+
58
  # Translate text
59
  translated = translator.translate(text, src=src_lang, dest=dest_lang)
60
  print(f"Translated: {translated.text}")
61
+
62
+ # API call with error handling
63
  API_KEY = os.getenv("RAG_API_KEY")
64
+ if not API_KEY:
65
+ return jsonify({"error": "API key not configured"}), 500
66
+
67
+ response = requests.post(
68
+ url="https://aitestingworkspace-aiagentchatup.hf.space/ask",
69
+ headers={"Authorization": f"Bearer {API_KEY}"},
70
+ data={"question": translated.text},
71
+ timeout=15
72
+ )
73
+
 
74
  return jsonify({
75
  "recognized_text": text,
76
  "translated_text": translated.text,
77
+ "server_response": response.json().get('answer', 'No response')
78
  })
79
+
80
+ except sr.UnknownValueError:
81
+ return jsonify({"error": "Could not understand audio"}), 400
82
+ except sr.RequestError as e:
83
+ return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
84
+ except OSError as e:
85
+ return jsonify({"error": f"Audio device error: {str(e)}"}), 500
86
+ except Exception as e:
87
+ return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
88
+
89
  if __name__ == "__main__":
90
+ app.run(host="0.0.0.0", port=5000, debug=False) # Disable debug for production
91
+
92
+
93
+ ###################This is the Existing CODE########################################
94
+ # from flask import Flask, render_template, request, jsonify
95
+ # import speech_recognition as sr
96
+ # from googletrans import Translator
97
+ # import requests
98
+ # import os
99
+ # # import pyttsx3
100
+
101
+ # app = Flask(__name__)
102
+
103
+ # # Initialize recognizer and translator
104
+ # recognizer = sr.Recognizer()
105
+ # translator = Translator()
106
+ # # engine = pyttsx3.init()
107
+
108
+ # # Language settings mapping
109
+ # language_mapping = {
110
+ # 1: ('ta-IN', 'ta', 'en'), # Tamil
111
+ # 2: ('en-US', 'en', 'en'), # English
112
+ # 3: ('hi-IN', 'hi', 'en'), # Hindi
113
+ # 4: ('ms-MY', 'ms', 'en') # Malay
114
+ # }
115
+
116
+ # @app.route('/')
117
+ # def index():
118
+ # return render_template("index.html")
119
+
120
+ # # def speak(text):
121
+ # # engine.say(text)
122
+ # # engine.runAndWait()
123
+
124
+ # @app.route('/speech-to-text', methods=['POST'])
125
+ # def speech_to_text():
126
+ # """Convert speech to text, translate, and query the /ask endpoint"""
127
+ # language = int(request.form.get("language", 0))
128
+
129
+ # if language not in language_mapping:
130
+ # return jsonify({"error": "Invalid language selection"}), 400
131
+
132
+ # recognition_lang, src_lang, dest_lang = language_mapping[language]
133
+
134
+ # with sr.Microphone() as source:
135
+ # print(f"Listening for {recognition_lang}...")
136
+ # recognizer.adjust_for_ambient_noise(source)
137
+ # audio = recognizer.listen(source)
138
+
139
+ # try:
140
+ # text = recognizer.recognize_google(audio, language=recognition_lang)
141
+ # print(f"Recognized: {text}")
142
+
143
+ # # Translate text
144
+ # translated = translator.translate(text, src=src_lang, dest=dest_lang)
145
+ # print(f"Translated: {translated.text}")
146
+
147
+ # # Send to /ask API
148
+ # API_KEY = os.getenv("RAG_API_KEY")
149
+
150
+ # # Hugging Face API endpoint
151
+ # url = "https://aitestingworkspace-aiagentchatup.hf.space/ask"
152
+
153
+ # # Headers with authentication
154
+ # headers = {
155
+ # "Authorization": f"Bearer {API_KEY}"
156
+ # }
157
+
158
+ # response = requests.post(url=url,data={"question": translated.text},headers=headers)
159
+
160
+ # # # Get response and speak it
161
+ # # server_response = response.json().get('answer', 'No response received.')
162
+ # # print(f"Server Response: {server_response}")
163
+ # # speak(server_response)
164
+
165
+ # return jsonify({
166
+ # "recognized_text": text,
167
+ # "translated_text": translated.text,
168
+ # "server_response": response.json()['answer']
169
+ # })
170
+
171
+ # except sr.UnknownValueError:
172
+ # return jsonify({"error": "Could not understand audio"}), 400
173
+ # except Exception as e:
174
+ # return jsonify({"error": str(e)}), 500
175
+
176
+ # if __name__ == "__main__":
177
+ # app.run(host="0.0.0.0", port=5000, debug=True)