Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,48 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
gr.Audio(sources=["microphone"]),
|
| 18 |
-
"text",
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
|
|
|
| 3 |
|
| 4 |
+
# HTML and JavaScript for Speech Recognition using webkitSpeechRecognition
|
| 5 |
+
speech_recognition_html = """
|
| 6 |
+
<html>
|
| 7 |
+
<body>
|
| 8 |
+
<button onclick="startRecognition()">Start Speech Recognition</button>
|
| 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 |
+
|
| 16 |
+
recognition.onresult = function(event) {
|
| 17 |
+
var transcript = event.results[event.resultIndex][0].transcript;
|
| 18 |
+
document.getElementById('output').textContent = transcript;
|
| 19 |
+
// Send transcript back to Streamlit using postMessage
|
| 20 |
+
window.parent.postMessage({func: 'update_output', transcript: transcript}, '*');
|
| 21 |
+
};
|
| 22 |
|
| 23 |
+
recognition.onerror = function(event) {
|
| 24 |
+
console.error("Speech recognition error", event.error);
|
| 25 |
+
document.getElementById('output').textContent = "Error in recognition";
|
| 26 |
+
};
|
| 27 |
|
| 28 |
+
function startRecognition() {
|
| 29 |
+
recognition.start();
|
| 30 |
+
}
|
| 31 |
+
</script>
|
| 32 |
+
</body>
|
| 33 |
+
</html>
|
| 34 |
+
"""
|
| 35 |
|
| 36 |
+
# Streamlit UI
|
| 37 |
+
st.title("Speech-to-Text Demo")
|
| 38 |
+
st.write("Click the button below and start speaking. The recognized text will be shown here:")
|
| 39 |
|
| 40 |
+
# Display the HTML with the embedded speech recognition
|
| 41 |
+
components.html(speech_recognition_html, height=200)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Output area where the recognized speech will be displayed
|
| 44 |
+
output = st.empty()
|
| 45 |
+
|
| 46 |
+
# The Streamlit component listens for the postMessage event and updates the output
|
| 47 |
+
st.write("Recognized Text:")
|
| 48 |
+
transcript = st.text_area("Transcript:", "", height=150)
|