Sib83 commited on
Commit
2ad3a18
·
verified ·
1 Parent(s): 2873436

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -52
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
- # Load spaCy model dynamically if not already installed
11
- if not spacy.util.is_package('en_core_web_sm'):
12
- spacy.cli.download('en_core_web_sm')
13
 
14
- # Load spaCy for grammar analysis
15
- nlp = spacy.load('en_core_web_sm')
 
16
 
17
- # Load Hugging Face Grammar Correction Model
18
- grammar_correction_pipeline = pipeline("text2text-generation", model="facebook/bart-large-mnli")
 
 
 
 
 
 
 
 
 
19
 
20
  # Function to transcribe speech to text
21
- def transcribe_audio(audio_file):
22
- recognizer = sr.Recognizer()
23
- with audio_file as source:
24
- audio = recognizer.record(source)
25
- text = recognizer.recognize_google(audio)
26
- return text
27
-
28
- # Function to detect language of the text
29
- def detect_language(text):
30
- return detect(text)
31
-
32
- # Function to correct grammar, syntax, and vocabulary mistakes
33
- def correct_text(text):
34
- corrected = grammar_correction_pipeline(text)[0]['generated_text']
35
- return corrected
36
-
37
- # Streamlit UI
38
- st.title("French Speech Correction App")
39
- st.subheader("Speak in French, and the app will correct your mistakes.")
40
-
41
- # Audio input: Upload an audio file
42
- audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
43
-
44
- if audio_file is not None:
45
- # Transcribe speech to text
46
- text = transcribe_audio(audio_file)
47
- st.write(f"Transcribed text: {text}")
48
-
49
- # Detect language
50
- language = detect_language(text)
51
- st.write(f"Detected Language: {language}")
52
-
53
- # If the detected language is French, proceed with corrections
54
- if language == 'fr':
55
- corrected_text = correct_text(text)
56
- st.write(f"Corrected Text: {corrected_text}")
57
- else:
58
- st.write("Please speak in French for the best results.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)