Sib83 commited on
Commit
19a77c0
·
verified ·
1 Parent(s): 6279894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -36
app.py CHANGED
@@ -1,47 +1,61 @@
1
- from flask import Flask, render_template, request, jsonify
2
  import speech_recognition as sr
3
- from gtts import gTTS
4
- import os
5
- import tempfile
6
 
7
- app = Flask(__name__)
 
8
 
9
- # Function to convert text to speech using gTTS
10
- def speak_text(text):
11
- tts = gTTS(text=text, lang='en')
12
- temp_file = tempfile.NamedTemporaryFile(delete=False)
13
- tts.save(temp_file.name)
14
- os.system(f"mpg321 {temp_file.name}") # Plays the saved audio file
15
- os.remove(temp_file.name) # Deletes the file after playing
16
-
17
- # Initialize recognizer
18
  recognizer = sr.Recognizer()
19
 
20
- # Route to handle speech input and process it
21
- @app.route("/start_recording", methods=["POST"])
22
- def start_recording():
 
 
 
 
23
  with sr.Microphone() as source:
24
- recognizer.adjust_for_ambient_noise(source)
25
- print("Listening for speech... Please speak.")
26
- audio_data = recognizer.listen(source)
27
-
28
  try:
29
- # Recognize speech and convert to text
30
- print("Recognizing speech...")
31
- text = recognizer.recognize_google(audio_data)
32
- print(f"Recognized text: {text}")
33
- # Respond with text-to-speech
34
- speak_text(f"You said: {text}")
35
- return jsonify({"text": text, "status": "success"})
36
-
37
  except sr.UnknownValueError:
38
- return jsonify({"error": "Could not understand the audio", "status": "error"})
 
39
  except sr.RequestError:
40
- return jsonify({"error": "Could not request results from Google Speech Recognition service", "status": "error"})
 
41
 
42
- @app.route("/")
43
- def index():
44
- return render_template("index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- if __name__ == "__main__":
47
- app.run(debug=True)
 
1
+ import streamlit as st
2
  import speech_recognition as sr
3
+ import pyttsx3
4
+ from langdetect import detect
 
5
 
6
+ # Initialize text-to-speech engine
7
+ engine = pyttsx3.init()
8
 
9
+ # Set up speech recognition
 
 
 
 
 
 
 
 
10
  recognizer = sr.Recognizer()
11
 
12
+ # Function for speaking out text
13
+ def speak_text(text):
14
+ engine.say(text)
15
+ engine.runAndWait()
16
+
17
+ # Function to handle speech recognition
18
+ def recognize_speech():
19
  with sr.Microphone() as source:
20
+ st.write("Listening...")
21
+ audio = recognizer.listen(source)
 
 
22
  try:
23
+ st.write("Recognizing...")
24
+ text = recognizer.recognize_google(audio)
25
+ return text
 
 
 
 
 
26
  except sr.UnknownValueError:
27
+ st.write("Sorry, I could not understand the audio.")
28
+ return ""
29
  except sr.RequestError:
30
+ st.write("Could not request results; check your internet connection.")
31
+ return ""
32
 
33
+ # Function to detect language
34
+ def detect_language(text):
35
+ try:
36
+ lang = detect(text)
37
+ return lang
38
+ except Exception as e:
39
+ st.write(f"Error in language detection: {e}")
40
+ return "Unknown"
41
+
42
+ # Streamlit UI setup
43
+ st.title("Real-Time Speech Recognition and Language Detection")
44
+
45
+ # Add a button to start and stop speech recording
46
+ if st.button("Start Recording"):
47
+ text = recognize_speech()
48
+ if text:
49
+ st.write(f"Recognized Text: {text}")
50
+
51
+ # Detect language
52
+ lang = detect_language(text)
53
+ st.write(f"Detected Language: {lang}")
54
+
55
+ # Speak the recognized text back
56
+ speak_text(f"You said: {text}")
57
+
58
+ elif st.button("Stop Recording"):
59
+ st.write("Stopped Recording")
60
 
61
+ # Note: If you need to implement a live transcription service (continuous listening), you can modify the flow above to keep listening in a loop.