Naz786 commited on
Commit
f410a0a
Β·
verified Β·
1 Parent(s): 1a9063d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -43
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 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 = ""
166
  if "auto_run_search" not in st.session_state:
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
 
224
- # Also keep the manual button for fallback
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.")