Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,8 +54,9 @@ st.set_page_config(
|
|
| 54 |
)
|
| 55 |
|
| 56 |
|
|
|
|
| 57 |
def create_speech_component():
|
| 58 |
-
"""Create speech recognition component
|
| 59 |
|
| 60 |
speech_recognition_html = """
|
| 61 |
<div style="padding: 20px;">
|
|
@@ -66,15 +67,24 @@ def create_speech_component():
|
|
| 66 |
</div>
|
| 67 |
<div id="status" style="margin: 10px 0; padding: 10px; background: #e8f5e9;">Ready</div>
|
| 68 |
<div id="output" style="white-space: pre-wrap; padding: 15px; background: #f5f5f5; min-height: 100px; max-height: 400px; overflow-y: auto;"></div>
|
|
|
|
| 69 |
|
| 70 |
<script>
|
| 71 |
let currentTranscript = '';
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 79 |
const startButton = document.getElementById('start');
|
| 80 |
const stopButton = document.getElementById('stop');
|
|
@@ -97,11 +107,13 @@ def create_speech_component():
|
|
| 97 |
status.textContent = 'Stopped';
|
| 98 |
startButton.disabled = false;
|
| 99 |
stopButton.disabled = true;
|
|
|
|
| 100 |
};
|
| 101 |
|
| 102 |
clearButton.onclick = () => {
|
| 103 |
currentTranscript = '';
|
| 104 |
output.textContent = '';
|
|
|
|
| 105 |
};
|
| 106 |
|
| 107 |
recognition.onresult = (event) => {
|
|
@@ -120,6 +132,10 @@ def create_speech_component():
|
|
| 120 |
|
| 121 |
output.textContent = currentTranscript + (interimTranscript ? '... ' + interimTranscript : '');
|
| 122 |
output.scrollTop = output.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
};
|
| 124 |
|
| 125 |
recognition.onend = () => {
|
|
@@ -136,71 +152,41 @@ def create_speech_component():
|
|
| 136 |
</div>
|
| 137 |
"""
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
js_code = "getCurrentTranscript()"
|
| 145 |
-
try:
|
| 146 |
-
return components.eval_js(js_code)
|
| 147 |
-
except Exception as e:
|
| 148 |
-
st.error(f"Error getting transcript: {str(e)}")
|
| 149 |
-
return None
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
def integrate_speech_component():
|
| 158 |
-
"""Integrate speech component with
|
| 159 |
if "voice_transcript" not in st.session_state:
|
| 160 |
st.session_state.voice_transcript = ""
|
| 161 |
if "last_update" not in st.session_state:
|
| 162 |
st.session_state.last_update = time.time()
|
| 163 |
|
| 164 |
-
#
|
| 165 |
-
show_debug = st.checkbox("Show Debug Info")
|
| 166 |
-
|
| 167 |
-
# Create the speech component
|
| 168 |
-
create_speech_component()
|
| 169 |
-
|
| 170 |
-
# Create placeholder for transcript display
|
| 171 |
transcript_container = st.empty()
|
| 172 |
-
|
| 173 |
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
-
if current_time - st.session_state.last_update >= 10:
|
| 177 |
-
new_transcript = get_transcript()
|
| 178 |
-
if show_debug:
|
| 179 |
-
debug_container.write(f"Debug: Checking for updates... Got: {new_transcript}")
|
| 180 |
-
|
| 181 |
-
if new_transcript and new_transcript != st.session_state.voice_transcript:
|
| 182 |
-
if show_debug:
|
| 183 |
-
st.write("Debug: Updating transcript")
|
| 184 |
-
st.write(f"Old: {st.session_state.voice_transcript}")
|
| 185 |
-
st.write(f"New: {new_transcript}")
|
| 186 |
-
|
| 187 |
-
st.session_state.voice_transcript = new_transcript
|
| 188 |
-
st.session_state.last_update = current_time
|
| 189 |
|
| 190 |
# Display current transcript
|
|
|
|
| 191 |
transcript_container.text_area(
|
| 192 |
"Voice Transcript:",
|
| 193 |
-
value=
|
| 194 |
height=100,
|
| 195 |
-
key=f"transcript_display_{int(
|
| 196 |
)
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
|
| 205 |
|
| 206 |
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
|
| 57 |
+
|
| 58 |
def create_speech_component():
|
| 59 |
+
"""Create speech recognition component using postMessage for communication."""
|
| 60 |
|
| 61 |
speech_recognition_html = """
|
| 62 |
<div style="padding: 20px;">
|
|
|
|
| 67 |
</div>
|
| 68 |
<div id="status" style="margin: 10px 0; padding: 10px; background: #e8f5e9;">Ready</div>
|
| 69 |
<div id="output" style="white-space: pre-wrap; padding: 15px; background: #f5f5f5; min-height: 100px; max-height: 400px; overflow-y: auto;"></div>
|
| 70 |
+
<div id="debug" style="margin-top: 10px; color: #666;"></div>
|
| 71 |
|
| 72 |
<script>
|
| 73 |
let currentTranscript = '';
|
| 74 |
+
const debug = document.getElementById('debug');
|
| 75 |
|
| 76 |
+
function sendTranscriptUpdate() {
|
| 77 |
+
// Send transcript to parent (Streamlit)
|
| 78 |
+
window.parent.postMessage({
|
| 79 |
+
type: 'transcript_update',
|
| 80 |
+
data: currentTranscript
|
| 81 |
+
}, '*');
|
| 82 |
+
debug.textContent = `Last update: ${new Date().toLocaleTimeString()} - Length: ${currentTranscript.length}`;
|
| 83 |
}
|
| 84 |
|
| 85 |
+
// Set up periodic updates
|
| 86 |
+
setInterval(sendTranscriptUpdate, 3000); // Send update every 3 seconds
|
| 87 |
+
|
| 88 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 89 |
const startButton = document.getElementById('start');
|
| 90 |
const stopButton = document.getElementById('stop');
|
|
|
|
| 107 |
status.textContent = 'Stopped';
|
| 108 |
startButton.disabled = false;
|
| 109 |
stopButton.disabled = true;
|
| 110 |
+
sendTranscriptUpdate(); // Send final update when stopped
|
| 111 |
};
|
| 112 |
|
| 113 |
clearButton.onclick = () => {
|
| 114 |
currentTranscript = '';
|
| 115 |
output.textContent = '';
|
| 116 |
+
sendTranscriptUpdate(); // Send empty transcript
|
| 117 |
};
|
| 118 |
|
| 119 |
recognition.onresult = (event) => {
|
|
|
|
| 132 |
|
| 133 |
output.textContent = currentTranscript + (interimTranscript ? '... ' + interimTranscript : '');
|
| 134 |
output.scrollTop = output.scrollHeight;
|
| 135 |
+
|
| 136 |
+
if (finalTranscript) {
|
| 137 |
+
sendTranscriptUpdate(); // Send update when we have final transcript
|
| 138 |
+
}
|
| 139 |
};
|
| 140 |
|
| 141 |
recognition.onend = () => {
|
|
|
|
| 152 |
</div>
|
| 153 |
"""
|
| 154 |
|
| 155 |
+
# Return both the component value
|
| 156 |
+
return components.html(
|
| 157 |
+
speech_recognition_html,
|
| 158 |
+
height=400,
|
| 159 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
def integrate_speech_component():
|
| 162 |
+
"""Integrate speech component with session state management."""
|
| 163 |
if "voice_transcript" not in st.session_state:
|
| 164 |
st.session_state.voice_transcript = ""
|
| 165 |
if "last_update" not in st.session_state:
|
| 166 |
st.session_state.last_update = time.time()
|
| 167 |
|
| 168 |
+
# Create placeholders for display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
transcript_container = st.empty()
|
| 170 |
+
status_container = st.empty()
|
| 171 |
|
| 172 |
+
# Create component
|
| 173 |
+
component_val = create_speech_component()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
# Display current transcript
|
| 176 |
+
current_transcript = st.session_state.voice_transcript
|
| 177 |
transcript_container.text_area(
|
| 178 |
"Voice Transcript:",
|
| 179 |
+
value=current_transcript,
|
| 180 |
height=100,
|
| 181 |
+
key=f"transcript_display_{int(time.time())}"
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# Show status
|
| 185 |
+
status_container.text(
|
| 186 |
+
f"Last updated: {datetime.fromtimestamp(st.session_state.last_update).strftime('%H:%M:%S')}"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
return current_transcript
|
| 190 |
|
| 191 |
|
| 192 |
|