Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -167,23 +167,27 @@ elif page == "Semantic Search":
|
|
167 |
st.session_state['voice_question'] = ''
|
168 |
if 'run_semantic_search' not in st.session_state:
|
169 |
st.session_state['run_semantic_search'] = False
|
|
|
|
|
170 |
|
171 |
-
# Custom input with mic button (HTML/JS)
|
172 |
-
question_html = """
|
173 |
<style>
|
174 |
-
.input-mic-container {
|
175 |
position: relative;
|
176 |
width: 100%;
|
177 |
max-width: 500px;
|
178 |
-
}
|
179 |
-
.input-mic {
|
180 |
width: 100%;
|
181 |
padding-right: 40px;
|
182 |
height: 38px;
|
183 |
font-size: 16px;
|
184 |
box-sizing: border-box;
|
185 |
-
|
186 |
-
|
|
|
|
|
187 |
position: absolute;
|
188 |
right: 5px;
|
189 |
top: 4px;
|
@@ -192,65 +196,66 @@ elif page == "Semantic Search":
|
|
192 |
font-size: 22px;
|
193 |
cursor: pointer;
|
194 |
outline: none;
|
195 |
-
}
|
196 |
</style>
|
197 |
<div class="input-mic-container">
|
198 |
-
<input id="questionInput" class="input-mic" type="text" placeholder="Ask a question about your code" value="{
|
199 |
<button class="mic-btn" id="micBtn" title="Speak your question">π€</button>
|
200 |
</div>
|
201 |
<script>
|
202 |
const input = document.getElementById('questionInput');
|
203 |
const micBtn = document.getElementById('micBtn');
|
204 |
let recognition;
|
205 |
-
if ('webkitSpeechRecognition' in window) {
|
206 |
recognition = new webkitSpeechRecognition();
|
207 |
recognition.lang = 'en-US';
|
208 |
recognition.continuous = false;
|
209 |
recognition.interimResults = false;
|
210 |
-
micBtn.onclick = function(e) {
|
211 |
e.preventDefault();
|
212 |
recognition.start();
|
213 |
micBtn.textContent = 'ποΈ';
|
214 |
-
};
|
215 |
-
recognition.onresult = function(event) {
|
216 |
const transcript = event.results[0][0].transcript;
|
217 |
input.value = transcript;
|
218 |
-
window.parent.postMessage({isStreamlitMessage: true, type: 'streamlit:setComponentValue', value: transcript}, '*');
|
219 |
micBtn.textContent = 'π€';
|
220 |
-
};
|
221 |
-
recognition.onerror = function() {
|
222 |
micBtn.textContent = 'π€';
|
223 |
-
};
|
224 |
-
recognition.onend = function() {
|
225 |
micBtn.textContent = 'π€';
|
226 |
-
};
|
227 |
-
} else {
|
228 |
micBtn.disabled = true;
|
229 |
micBtn.title = 'Voice not supported';
|
230 |
-
}
|
231 |
// Send value on input change
|
232 |
-
input.onchange = function() {
|
233 |
-
window.parent.postMessage({isStreamlitMessage: true, type: 'streamlit:setComponentValue', value: input.value}, '*');
|
234 |
-
}
|
235 |
</script>
|
236 |
-
"""
|
237 |
|
238 |
# Render the custom input+mic
|
239 |
-
|
240 |
|
241 |
-
#
|
242 |
-
# For demo, use a
|
243 |
-
question = st.
|
244 |
|
245 |
# If the value is updated, validate and set
|
246 |
-
if question and question != st.session_state.get('
|
247 |
if is_coding_question(question):
|
248 |
-
st.session_state['
|
249 |
st.session_state['run_semantic_search'] = True
|
250 |
st.success(f"Question recognized: {question}")
|
251 |
else:
|
252 |
st.warning("Please ask a relevant question.")
|
253 |
st.session_state['voice_question'] = ''
|
|
|
254 |
|
255 |
run_btn = st.button("Run Semantic Search")
|
256 |
run_search = run_btn or st.session_state.get('run_semantic_search', False)
|
|
|
167 |
st.session_state['voice_question'] = ''
|
168 |
if 'run_semantic_search' not in st.session_state:
|
169 |
st.session_state['run_semantic_search'] = False
|
170 |
+
if 'last_validated_question' not in st.session_state:
|
171 |
+
st.session_state['last_validated_question'] = ''
|
172 |
|
173 |
+
# Custom input with mic button (HTML/JS) - replaces st.text_input
|
174 |
+
question_html = f"""
|
175 |
<style>
|
176 |
+
.input-mic-container {{
|
177 |
position: relative;
|
178 |
width: 100%;
|
179 |
max-width: 500px;
|
180 |
+
}}
|
181 |
+
.input-mic {{
|
182 |
width: 100%;
|
183 |
padding-right: 40px;
|
184 |
height: 38px;
|
185 |
font-size: 16px;
|
186 |
box-sizing: border-box;
|
187 |
+
border: 1px solid #ccc;
|
188 |
+
border-radius: 4px;
|
189 |
+
}}
|
190 |
+
.mic-btn {{
|
191 |
position: absolute;
|
192 |
right: 5px;
|
193 |
top: 4px;
|
|
|
196 |
font-size: 22px;
|
197 |
cursor: pointer;
|
198 |
outline: none;
|
199 |
+
}}
|
200 |
</style>
|
201 |
<div class="input-mic-container">
|
202 |
+
<input id="questionInput" class="input-mic" type="text" placeholder="Ask a question about your code" value="{st.session_state['voice_question']}" />
|
203 |
<button class="mic-btn" id="micBtn" title="Speak your question">π€</button>
|
204 |
</div>
|
205 |
<script>
|
206 |
const input = document.getElementById('questionInput');
|
207 |
const micBtn = document.getElementById('micBtn');
|
208 |
let recognition;
|
209 |
+
if ('webkitSpeechRecognition' in window) {{
|
210 |
recognition = new webkitSpeechRecognition();
|
211 |
recognition.lang = 'en-US';
|
212 |
recognition.continuous = false;
|
213 |
recognition.interimResults = false;
|
214 |
+
micBtn.onclick = function(e) {{
|
215 |
e.preventDefault();
|
216 |
recognition.start();
|
217 |
micBtn.textContent = 'ποΈ';
|
218 |
+
}};
|
219 |
+
recognition.onresult = function(event) {{
|
220 |
const transcript = event.results[0][0].transcript;
|
221 |
input.value = transcript;
|
222 |
+
window.parent.postMessage({{isStreamlitMessage: true, type: 'streamlit:setComponentValue', value: transcript}}, '*');
|
223 |
micBtn.textContent = 'π€';
|
224 |
+
}};
|
225 |
+
recognition.onerror = function() {{
|
226 |
micBtn.textContent = 'π€';
|
227 |
+
}};
|
228 |
+
recognition.onend = function() {{
|
229 |
micBtn.textContent = 'π€';
|
230 |
+
}};
|
231 |
+
}} else {{
|
232 |
micBtn.disabled = true;
|
233 |
micBtn.title = 'Voice not supported';
|
234 |
+
}}
|
235 |
// Send value on input change
|
236 |
+
input.onchange = function() {{
|
237 |
+
window.parent.postMessage({{isStreamlitMessage: true, type: 'streamlit:setComponentValue', value: input.value}}, '*');
|
238 |
+
}}
|
239 |
</script>
|
240 |
+
"""
|
241 |
|
242 |
# Render the custom input+mic
|
243 |
+
components.html(question_html, height=60)
|
244 |
|
245 |
+
# Get the value from Streamlit's custom component
|
246 |
+
# For demo, use session_state (in production, use a custom Streamlit component for robust JS->Python)
|
247 |
+
question = st.session_state.get('voice_question', '')
|
248 |
|
249 |
# If the value is updated, validate and set
|
250 |
+
if question and question != st.session_state.get('last_validated_question', ''):
|
251 |
if is_coding_question(question):
|
252 |
+
st.session_state['last_validated_question'] = question
|
253 |
st.session_state['run_semantic_search'] = True
|
254 |
st.success(f"Question recognized: {question}")
|
255 |
else:
|
256 |
st.warning("Please ask a relevant question.")
|
257 |
st.session_state['voice_question'] = ''
|
258 |
+
st.session_state['last_validated_question'] = ''
|
259 |
|
260 |
run_btn = st.button("Run Semantic Search")
|
261 |
run_search = run_btn or st.session_state.get('run_semantic_search', False)
|