Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,47 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
import speech_recognition as sr
|
| 4 |
-
import pyttsx3
|
| 5 |
from flask import Flask, render_template, request, jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
recognizer = sr.Recognizer()
|
| 11 |
-
engine = pyttsx3.init()
|
| 12 |
-
|
| 13 |
-
# Temporary file to store the audio recording
|
| 14 |
-
audio_file_path = "temporary_audio.wav"
|
| 15 |
-
|
| 16 |
-
# Function to record audio from the microphone
|
| 17 |
-
def record_audio():
|
| 18 |
-
with sr.Microphone() as source:
|
| 19 |
-
print("Please start speaking...")
|
| 20 |
-
recognizer.adjust_for_ambient_noise(source) # Adjusts for ambient noise before listening
|
| 21 |
-
audio = recognizer.listen(source)
|
| 22 |
-
print("Recording completed.")
|
| 23 |
-
return audio
|
| 24 |
-
|
| 25 |
-
# Function to transcribe speech to text
|
| 26 |
-
def transcribe_audio(audio):
|
| 27 |
-
try:
|
| 28 |
-
print("Recognizing...")
|
| 29 |
-
text = recognizer.recognize_google(audio) # Using Google Speech Recognition API
|
| 30 |
-
return text
|
| 31 |
-
except sr.UnknownValueError:
|
| 32 |
-
return "Sorry, I could not understand the audio."
|
| 33 |
-
except sr.RequestError:
|
| 34 |
-
return "Sorry, there was an error with the speech recognition service."
|
| 35 |
-
|
| 36 |
-
# Function to convert text to speech
|
| 37 |
def speak_text(text):
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return render_template('index.html')
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
def start_recording():
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Transcribe the audio
|
| 52 |
-
transcribed_text = transcribe_audio(audio)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
@app.route(
|
| 63 |
-
def
|
| 64 |
-
|
| 65 |
-
# If there was any temporary file for the recording, we can delete it (not necessary in real-time recording)
|
| 66 |
-
if os.path.exists(audio_file_path):
|
| 67 |
-
os.remove(audio_file_path)
|
| 68 |
-
|
| 69 |
-
return jsonify({'status': 'success', 'message': 'Recording stopped and temporary files removed.'})
|
| 70 |
-
except Exception as e:
|
| 71 |
-
return jsonify({'status': 'error', 'message': str(e)})
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|