Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,6 +158,8 @@ elif page == "Semantic Search":
|
|
| 158 |
user_role = st.selectbox("Your Role", USER_ROLES, key="sem_role")
|
| 159 |
with col4:
|
| 160 |
explanation_language = st.selectbox("Explanation Language", EXPLANATION_LANGUAGES, key="sem_expl")
|
|
|
|
|
|
|
| 161 |
# Initialize session state variables for voice input and auto run
|
| 162 |
if "voice_question" not in st.session_state:
|
| 163 |
st.session_state.voice_question = ""
|
|
@@ -165,76 +167,57 @@ elif page == "Semantic Search":
|
|
| 165 |
st.session_state.auto_run_search = False
|
| 166 |
|
| 167 |
# Container for question input and voice button
|
| 168 |
-
col_question, col_voice = st.columns([
|
| 169 |
with col_question:
|
| 170 |
question = st.text_input("Ask a question about your code", value=st.session_state.voice_question, key="question_input")
|
| 171 |
with col_voice:
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
const
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
recognition.
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
""",
|
| 211 |
-
unsafe_allow_html=True
|
| 212 |
-
)
|
| 213 |
-
|
| 214 |
-
# Listen for the custom event and update session state via Streamlit's experimental_rerun hack
|
| 215 |
-
# This requires a small hack using st.experimental_get_query_params and st.experimental_set_query_params
|
| 216 |
-
# We will use st.experimental_get_query_params to detect voice input from URL params
|
| 217 |
-
|
| 218 |
-
# Check if voice input is passed via query params
|
| 219 |
-
query_params = st.experimental_get_query_params()
|
| 220 |
-
if "voice_input" in query_params:
|
| 221 |
-
voice_text = query_params["voice_input"][0]
|
| 222 |
-
if voice_text != st.session_state.voice_question:
|
| 223 |
-
st.session_state.voice_question = voice_text
|
| 224 |
-
st.session_state.auto_run_search = True
|
| 225 |
-
# Clear the query param to avoid repeated triggers
|
| 226 |
-
st.experimental_set_query_params()
|
| 227 |
-
|
| 228 |
-
# Run semantic search automatically if flag is set
|
| 229 |
if st.session_state.auto_run_search:
|
| 230 |
st.session_state.auto_run_search = False
|
| 231 |
-
if not code_input.strip() or not st.session_state.
|
| 232 |
st.error("Both code and question are required.")
|
| 233 |
elif not code_matches_language(code_input, programming_language):
|
| 234 |
st.error(f"Language mismatch. Please check your code and language selection.")
|
| 235 |
else:
|
| 236 |
with st.spinner("Running Semantic Search..."):
|
| 237 |
-
answer = call_groq_api(f"{st.session_state.
|
| 238 |
st.success("Answer:")
|
| 239 |
st.write(answer)
|
| 240 |
|
|
|
|
| 158 |
user_role = st.selectbox("Your Role", USER_ROLES, key="sem_role")
|
| 159 |
with col4:
|
| 160 |
explanation_language = st.selectbox("Explanation Language", EXPLANATION_LANGUAGES, key="sem_expl")
|
| 161 |
+
import streamlit_javascript
|
| 162 |
+
|
| 163 |
# Initialize session state variables for voice input and auto run
|
| 164 |
if "voice_question" not in st.session_state:
|
| 165 |
st.session_state.voice_question = ""
|
|
|
|
| 167 |
st.session_state.auto_run_search = False
|
| 168 |
|
| 169 |
# Container for question input and voice button
|
| 170 |
+
col_question, col_voice = st.columns([9,1])
|
| 171 |
with col_question:
|
| 172 |
question = st.text_input("Ask a question about your code", value=st.session_state.voice_question, key="question_input")
|
| 173 |
with col_voice:
|
| 174 |
+
# Use streamlit_javascript component to run JS for voice recognition
|
| 175 |
+
js_code = """
|
| 176 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 177 |
+
if (!SpeechRecognition) {
|
| 178 |
+
alert("Speech Recognition not supported in this browser.");
|
| 179 |
+
} else {
|
| 180 |
+
const recognition = new SpeechRecognition();
|
| 181 |
+
recognition.lang = 'en-US';
|
| 182 |
+
recognition.interimResults = false;
|
| 183 |
+
recognition.maxAlternatives = 1;
|
| 184 |
+
|
| 185 |
+
const micBtn = document.getElementById('mic-btn');
|
| 186 |
+
micBtn.onclick = () => {
|
| 187 |
+
recognition.start();
|
| 188 |
+
micBtn.textContent = 'ποΈ';
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
recognition.onresult = (event) => {
|
| 192 |
+
const transcript = event.results[0][0].transcript;
|
| 193 |
+
window.streamlitSetValue('question_input', transcript);
|
| 194 |
+
micBtn.textContent = 'π€';
|
| 195 |
+
};
|
| 196 |
+
|
| 197 |
+
recognition.onerror = (event) => {
|
| 198 |
+
console.error('Speech recognition error', event.error);
|
| 199 |
+
micBtn.textContent = 'π€';
|
| 200 |
+
};
|
| 201 |
+
}
|
| 202 |
+
"""
|
| 203 |
+
streamlit_javascript.st_javascript(js_code, key="voice_recognition_js")
|
| 204 |
+
|
| 205 |
+
st.markdown('<button id="mic-btn" title="Click to speak" style="height:38px; width:38px; font-size:20px;">π€</button>', unsafe_allow_html=True)
|
| 206 |
+
|
| 207 |
+
# Automatically run semantic search if voice_question updated
|
| 208 |
+
if st.session_state.voice_question and st.session_state.voice_question != question:
|
| 209 |
+
st.session_state.auto_run_search = True
|
| 210 |
+
st.session_state.question_input = st.session_state.voice_question
|
| 211 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
if st.session_state.auto_run_search:
|
| 213 |
st.session_state.auto_run_search = False
|
| 214 |
+
if not code_input.strip() or not st.session_state.question_input.strip():
|
| 215 |
st.error("Both code and question are required.")
|
| 216 |
elif not code_matches_language(code_input, programming_language):
|
| 217 |
st.error(f"Language mismatch. Please check your code and language selection.")
|
| 218 |
else:
|
| 219 |
with st.spinner("Running Semantic Search..."):
|
| 220 |
+
answer = call_groq_api(f"{st.session_state.question_input}\n\nCode:\n{code_input}")
|
| 221 |
st.success("Answer:")
|
| 222 |
st.write(answer)
|
| 223 |
|