import streamlit as st
# 创建一个文本输入框,用于显示语音识别结果
text_input = st.text_input("Speech to Text", key="speech_to_text")
# 使用 Streamlit 的 HTML 功能来添加一个按钮和一些 JavaScript
st.markdown("""
<button id="start_button" onclick="startDictation()">Start Dictation</button>
<script>
function startDictation() {
if (window.hasOwnProperty('webkitSpeechRecognition')) {
var recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = "en-US";
recognition.start();
recognition.onresult = function(e) {
document.getElementById('speech_to_text').value
= e.results[0][0].transcript;
recognition.stop();
// Call Streamlit function to set the value
let event = new Event('input', { bubbles: true });
document.getElementById('speech_to_text').dispatchEvent(event);
};
recognition.onerror = function(e) {
recognition.stop();
}
}
}
</script>
""", unsafe_allow_html=True)
# 显示语音输入结果
if text_input:
st.write(f"You said: {text_input}")
markdown
最新推荐文章于 2024-09-13 23:28:21 发布