Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,70 +158,81 @@ 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 |
-
import
|
| 162 |
|
| 163 |
-
# Initialize session state variables
|
| 164 |
if "voice_question" not in st.session_state:
|
| 165 |
st.session_state.voice_question = ""
|
| 166 |
if "auto_run_search" not in st.session_state:
|
| 167 |
st.session_state.auto_run_search = False
|
| 168 |
|
| 169 |
-
#
|
| 170 |
-
col_question,
|
| 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
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 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 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 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.
|
| 221 |
st.success("Answer:")
|
| 222 |
st.write(answer)
|
| 223 |
|
| 224 |
-
#
|
| 225 |
if st.button("Run Semantic Search"):
|
| 226 |
if not code_input.strip() or not question.strip():
|
| 227 |
st.error("Both code and question are required.")
|
|
|
|
| 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.components.v1 as components
|
| 162 |
|
| 163 |
+
# Initialize session state variables
|
| 164 |
if "voice_question" not in st.session_state:
|
| 165 |
st.session_state.voice_question = ""
|
| 166 |
if "auto_run_search" not in st.session_state:
|
| 167 |
st.session_state.auto_run_search = False
|
| 168 |
|
| 169 |
+
# Layout: question input and mic button side by side
|
| 170 |
+
col_question, col_mic = 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_mic:
|
| 174 |
+
# Microphone button and JS for voice recognition embedded via components.html
|
| 175 |
+
components.html(
|
| 176 |
+
"""
|
| 177 |
+
<button id="mic-btn" style="height:38px; width:38px; font-size:20px;">π€</button>
|
| 178 |
+
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
const micBtn = document.getElementById('mic-btn');
|
| 180 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 181 |
+
if (!SpeechRecognition) {
|
| 182 |
+
alert("Speech Recognition not supported in this browser.");
|
| 183 |
+
micBtn.disabled = true;
|
| 184 |
+
} else {
|
| 185 |
+
const recognition = new SpeechRecognition();
|
| 186 |
+
recognition.lang = 'en-US';
|
| 187 |
+
recognition.interimResults = false;
|
| 188 |
+
recognition.maxAlternatives = 1;
|
| 189 |
+
|
| 190 |
+
micBtn.onclick = () => {
|
| 191 |
+
recognition.start();
|
| 192 |
+
micBtn.textContent = 'ποΈ';
|
| 193 |
+
};
|
| 194 |
+
|
| 195 |
+
recognition.onresult = (event) => {
|
| 196 |
+
const transcript = event.results[0][0].transcript;
|
| 197 |
+
// Send transcript to Streamlit by updating the input field
|
| 198 |
+
const inputBox = window.parent.document.querySelector('input[data-key="question_input"]');
|
| 199 |
+
if (inputBox) {
|
| 200 |
+
inputBox.value = transcript;
|
| 201 |
+
inputBox.dispatchEvent(new Event('input', { bubbles: true }));
|
| 202 |
+
}
|
| 203 |
+
micBtn.textContent = 'π€';
|
| 204 |
+
};
|
| 205 |
+
|
| 206 |
+
recognition.onerror = (event) => {
|
| 207 |
+
console.error('Speech recognition error', event.error);
|
| 208 |
+
micBtn.textContent = 'π€';
|
| 209 |
+
};
|
| 210 |
+
}
|
| 211 |
+
</script>
|
| 212 |
+
""",
|
| 213 |
+
height=50,
|
| 214 |
+
scrolling=False,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# Update session state voice_question if question input changed manually or by voice
|
| 218 |
+
if question != st.session_state.voice_question:
|
| 219 |
+
st.session_state.voice_question = question
|
| 220 |
st.session_state.auto_run_search = True
|
|
|
|
| 221 |
|
| 222 |
+
# Automatically run semantic search if flag is set
|
| 223 |
if st.session_state.auto_run_search:
|
| 224 |
st.session_state.auto_run_search = False
|
| 225 |
+
if not code_input.strip() or not st.session_state.voice_question.strip():
|
| 226 |
st.error("Both code and question are required.")
|
| 227 |
elif not code_matches_language(code_input, programming_language):
|
| 228 |
st.error(f"Language mismatch. Please check your code and language selection.")
|
| 229 |
else:
|
| 230 |
with st.spinner("Running Semantic Search..."):
|
| 231 |
+
answer = call_groq_api(f"{st.session_state.voice_question}\n\nCode:\n{code_input}")
|
| 232 |
st.success("Answer:")
|
| 233 |
st.write(answer)
|
| 234 |
|
| 235 |
+
# Manual button fallback
|
| 236 |
if st.button("Run Semantic Search"):
|
| 237 |
if not code_input.strip() or not question.strip():
|
| 238 |
st.error("Both code and question are required.")
|