Gopikanth123 commited on
Commit
50419c1
·
verified ·
1 Parent(s): 037896f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -9,10 +9,14 @@ speech_recognition_html = """
9
  <p id="output">Speak something...</p>
10
 
11
  <script>
 
 
 
 
 
12
  var recognition = new webkitSpeechRecognition();
13
  recognition.continuous = false; // Stops after speech input
14
  recognition.interimResults = true;
15
- recognition.lang = "en-US"; // Set the language for recognition
16
 
17
  recognition.onresult = function(event) {
18
  var transcript = event.results[event.resultIndex][0].transcript;
@@ -27,12 +31,7 @@ speech_recognition_html = """
27
  };
28
 
29
  function startRecognition() {
30
- try {
31
- recognition.start();
32
- } catch (error) {
33
- console.error("Error starting recognition:", error);
34
- document.getElementById('output').textContent = "Error starting recognition";
35
- }
36
  }
37
  </script>
38
  </body>
@@ -49,8 +48,19 @@ components.html(speech_recognition_html, height=200)
49
  # Output area where the recognized speech will be displayed
50
  output = st.empty()
51
 
52
- # The Streamlit component listens for the postMessage event and updates the output
53
  st.write("Recognized Text:")
54
  transcript = st.text_area("Transcript:", "", height=150)
55
 
56
- # JavaScript is sending postMessage with 'update_output' func, you need to listen for this in the frontend
 
 
 
 
 
 
 
 
 
 
 
 
9
  <p id="output">Speak something...</p>
10
 
11
  <script>
12
+ // Check if the browser supports speech recognition
13
+ if (!('webkitSpeechRecognition' in window)) {
14
+ document.getElementById('output').textContent = "Speech recognition not supported in this browser.";
15
+ }
16
+
17
  var recognition = new webkitSpeechRecognition();
18
  recognition.continuous = false; // Stops after speech input
19
  recognition.interimResults = true;
 
20
 
21
  recognition.onresult = function(event) {
22
  var transcript = event.results[event.resultIndex][0].transcript;
 
31
  };
32
 
33
  function startRecognition() {
34
+ recognition.start();
 
 
 
 
 
35
  }
36
  </script>
37
  </body>
 
48
  # Output area where the recognized speech will be displayed
49
  output = st.empty()
50
 
51
+ # This is where the recognized text will be shown on the Streamlit side
52
  st.write("Recognized Text:")
53
  transcript = st.text_area("Transcript:", "", height=150)
54
 
55
+ # Listen for postMessage events from the iframe to update the text area
56
+ components.html("""
57
+ <script>
58
+ window.addEventListener('message', function(event) {
59
+ if (event.data.func === 'update_output') {
60
+ document.getElementById('output').textContent = event.data.transcript;
61
+ // Update the Streamlit text area with the transcript
62
+ window.parent.postMessage({func: 'update_text_area', text: event.data.transcript}, '*');
63
+ }
64
+ });
65
+ </script>
66
+ """, height=0)