Ansaribinhyder commited on
Commit
b4c6ab5
·
1 Parent(s): a1592a0

All in Flask

Browse files
Files changed (1) hide show
  1. main.py +93 -8
main.py CHANGED
@@ -1,15 +1,100 @@
1
- from flask import Flask, render_template
2
 
3
- app = Flask(__name__)
4
 
5
- @app.route("/")
6
 
7
- def index():
8
 
9
- # Render the index.html file from the templates folder
10
 
11
- return render_template("index.html")
12
 
13
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- app.run(debug=True)
 
 
1
+ # from flask import Flask, render_template
2
 
3
+ # app = Flask(__name__)
4
 
5
+ # @app.route("/")
6
 
7
+ # def index():
8
 
9
+ # # Render the index.html file from the templates folder
10
 
11
+ # return render_template("index.html")
12
 
13
+ # if __name__ == "__main__":
14
+
15
+ # app.run(debug=True)
16
+
17
+
18
+ from flask import Flask, request, jsonify
19
+ import speech_recognition as sr
20
+
21
+ app = Flask(__name__)
22
+
23
+ @app.route('/')
24
+ def index():
25
+ return '''
26
+ <!DOCTYPE html>
27
+ <html lang="en">
28
+ <head>
29
+ <meta charset="UTF-8">
30
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
31
+ <title>Speech Recognition with Python</title>
32
+ </head>
33
+ <body>
34
+ <h1>Speech Recognition with Python</h1>
35
+ <button onclick="startRecording()">Start Recording</button>
36
+ <div id="transcription"></div>
37
+
38
+ <script>
39
+ function startRecording() {
40
+ const button = document.querySelector('button');
41
+ button.disabled = true;
42
+ button.textContent = "Recording...";
43
+
44
+ // Create a new form data object to send audio
45
+ let formData = new FormData();
46
+ navigator.mediaDevices.getUserMedia({ audio: true })
47
+ .then(stream => {
48
+ let mediaRecorder = new MediaRecorder(stream);
49
+ mediaRecorder.start();
50
+
51
+ mediaRecorder.ondataavailable = function(event) {
52
+ formData.append('audio', event.data);
53
+ fetch('/recognize', {
54
+ method: 'POST',
55
+ body: formData
56
+ })
57
+ .then(response => response.json())
58
+ .then(data => {
59
+ document.getElementById('transcription').textContent = data.transcript;
60
+ button.disabled = false;
61
+ button.textContent = "Start Recording";
62
+ })
63
+ .catch(err => {
64
+ console.error('Error during recognition:', err);
65
+ button.disabled = false;
66
+ button.textContent = "Start Recording";
67
+ });
68
+ };
69
+ })
70
+ .catch(err => {
71
+ console.error('Permission denied:', err);
72
+ button.disabled = false;
73
+ button.textContent = "Start Recording";
74
+ });
75
+ }
76
+ </script>
77
+ </body>
78
+ </html>
79
+ '''
80
+
81
+ @app.route('/recognize', methods=['POST'])
82
+ def recognize():
83
+ if 'audio' not in request.files:
84
+ return jsonify({'error': 'No audio file found in request'}), 400
85
+
86
+ audio_file = request.files['audio']
87
+ recognizer = sr.Recognizer()
88
+
89
+ try:
90
+ # Use SpeechRecognition to process the audio
91
+ with sr.AudioFile(audio_file) as source:
92
+ audio = recognizer.record(source)
93
+ transcript = recognizer.recognize_google(audio) # Google Web Speech API
94
+ return jsonify({'transcript': transcript})
95
+
96
+ except Exception as e:
97
+ return jsonify({'error': str(e)}), 500
98
 
99
+ if __name__ == '__main__':
100
+ app.run(debug=True)