Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,74 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import speech_recognition as sr
|
| 3 |
-
from langdetect import detect
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
import spacy
|
| 6 |
-
import spacy.cli
|
| 7 |
-
from google.cloud import speech
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
if not spacy.util.is_package('en_core_web_sm'):
|
| 12 |
-
spacy.cli.download('en_core_web_sm')
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Function to transcribe speech to text
|
| 21 |
-
def transcribe_audio(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Initialize the recognizer and text-to-speech engine
|
| 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 |
+
engine.say(text)
|
| 39 |
+
engine.runAndWait()
|
| 40 |
+
|
| 41 |
+
@app.route('/')
|
| 42 |
+
def index():
|
| 43 |
+
return render_template('index.html')
|
| 44 |
+
|
| 45 |
+
@app.route('/start_recording', methods=['POST'])
|
| 46 |
+
def start_recording():
|
| 47 |
+
try:
|
| 48 |
+
# Record the audio
|
| 49 |
+
audio = record_audio()
|
| 50 |
+
|
| 51 |
+
# Transcribe the audio
|
| 52 |
+
transcribed_text = transcribe_audio(audio)
|
| 53 |
+
|
| 54 |
+
# Speak the transcribed text
|
| 55 |
+
speak_text(transcribed_text)
|
| 56 |
+
|
| 57 |
+
# Return the transcription and status
|
| 58 |
+
return jsonify({'status': 'success', 'transcription': transcribed_text})
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return jsonify({'status': 'error', 'message': str(e)})
|
| 61 |
+
|
| 62 |
+
@app.route('/stop_recording', methods=['POST'])
|
| 63 |
+
def stop_recording():
|
| 64 |
+
try:
|
| 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)
|