johnpaulbin commited on
Commit
067d472
1 Parent(s): 541820f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -1
app.py CHANGED
@@ -21,7 +21,96 @@ def handle_transcription(username):
21
  elif request.method == 'GET':
22
  transcription = transcriptions.get(username, 'N/A')
23
  return jsonify({"transcription": transcription})
24
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  if __name__ == "__main__":
27
  config = Config()
 
21
  elif request.method == 'GET':
22
  transcription = transcriptions.get(username, 'N/A')
23
  return jsonify({"transcription": transcription})
24
+
25
+
26
+ @app.route('/')
27
+ def home():
28
+ html_content = """
29
+ <html lang="en"><head>
30
+ <meta charset="UTF-8">
31
+ <title>Speech to Text</title>
32
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
33
+ </head>
34
+ <body>
35
+ <h1>Speech Recognition</h1>
36
+ <button id="start">Start Listening</button>
37
+ <button id="stop">Stop Listening</button>
38
+ <div>
39
+ <label for="username">Username:</label>
40
+ <input type="text" id="username" placeholder="Enter your username">
41
+ </div>
42
+
43
+ <div id="transcription" style="border: 1px solid #ccc; padding: 10px; margin-top: 5px; min-height: 50px;"></div>
44
+ <script>
45
+ // Check for browser support
46
+ window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
47
+
48
+ if (!window.SpeechRecognition) {
49
+ alert("Your browser does not support Speech Recognition. Try Google Chrome.");
50
+ }
51
+
52
+ let recognition = new window.SpeechRecognition();
53
+ recognition.continuous = true; // Keep listening even after getting a result
54
+ recognition.interimResults = true; // Show interim results
55
+ let liveOutput = document.getElementById('transcription');
56
+
57
+ let lastTranscription = '';
58
+ let intervalId = null;
59
+
60
+ recognition.onresult = (event) => {
61
+ let currentTranscription = '';
62
+
63
+ // Concatenate all the transcriptions
64
+ for (const result of event.results) {
65
+ currentTranscription += result[0].transcript;
66
+ }
67
+
68
+
69
+ liveOutput.textContent = currentTranscription;
70
+
71
+ // Check if there's a new transcription
72
+ if (lastTranscription !== currentTranscription) {
73
+ lastTranscription = currentTranscription;
74
+ console.log(currentTranscription);
75
+ }
76
+ };
77
+
78
+ recognition.onerror = (event) => {
79
+ console.error('Speech recognition error', event.error);
80
+ };
81
+
82
+ function sendTranscriptionToServer(transcription) {
83
+ let username = document.getElementById('username').value;
84
+ $.ajax({
85
+ url: 'https://johnpaulbin-api-test-stt.hf.space/' + username, // Your server endpoint
86
+ type: 'POST',
87
+ contentType: 'application/json',
88
+ data: JSON.stringify({ transcription: transcription }),
89
+ success: function(response) {
90
+ console.log('Transcription sent', response);
91
+ },
92
+ error: function(error) {
93
+ console.error('Error sending transcription', error);
94
+ }
95
+ });
96
+ }
97
+
98
+ // Start and stop functions
99
+ document.getElementById('start').addEventListener('click', () => {
100
+ recognition.start();
101
+ intervalId = setInterval(() => sendTranscriptionToServer(lastTranscription), 600);
102
+ });
103
+
104
+ document.getElementById('stop').addEventListener('click', () => {
105
+ recognition.stop();
106
+ clearInterval(intervalId);
107
+ });
108
+ </script>
109
+
110
+
111
+ </body></html>
112
+ """
113
+ return Response(html_content, mimetype='text/html')
114
 
115
  if __name__ == "__main__":
116
  config = Config()