Naz786 commited on
Commit
8985573
Β·
verified Β·
1 Parent(s): 83d4476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -55
app.py CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
2
  import difflib
3
  import requests
4
  import datetime
5
- import streamlit.components.v1 as components
6
 
7
  # --- CONFIG ---
8
  GROQ_API_KEY = st.secrets.get('GROQ_API_KEY', 'YOUR_GROQ_API_KEY')
@@ -163,22 +162,15 @@ elif page == "Semantic Search":
163
  st.caption("Example questions:")
164
  st.write(", ".join(EXAMPLE_QUESTIONS))
165
 
166
- if 'voice_question' not in st.session_state:
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;
@@ -186,8 +178,8 @@ elif page == "Semantic Search":
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,75 +188,60 @@ elif page == "Semantic Search":
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)
262
- if run_search:
263
- st.session_state['run_semantic_search'] = False
264
  if not code_input.strip() or not question.strip():
265
  st.error("Both code and question are required.")
266
  elif not code_matches_language(code_input, programming_language):
267
  st.error(f"Language mismatch. Please check your code and language selection.")
 
 
268
  else:
269
  with st.spinner("Running Semantic Search..."):
270
  answer = call_groq_api(f"{question}\n\nCode:\n{code_input}")
 
2
  import difflib
3
  import requests
4
  import datetime
 
5
 
6
  # --- CONFIG ---
7
  GROQ_API_KEY = st.secrets.get('GROQ_API_KEY', 'YOUR_GROQ_API_KEY')
 
162
  st.caption("Example questions:")
163
  st.write(", ".join(EXAMPLE_QUESTIONS))
164
 
165
+ # --- Custom input with mic button (HTML/JS) ---
166
+ st.markdown("""
 
 
 
 
 
 
 
167
  <style>
168
+ .input-mic-container {
169
  position: relative;
170
  width: 100%;
171
  max-width: 500px;
172
+ }
173
+ .input-mic {
174
  width: 100%;
175
  padding-right: 40px;
176
  height: 38px;
 
178
  box-sizing: border-box;
179
  border: 1px solid #ccc;
180
  border-radius: 4px;
181
+ }
182
+ .mic-btn {
183
  position: absolute;
184
  right: 5px;
185
  top: 4px;
 
188
  font-size: 22px;
189
  cursor: pointer;
190
  outline: none;
191
+ }
192
  </style>
193
  <div class="input-mic-container">
194
+ <input id="questionInput" class="input-mic" type="text" placeholder="Ask a question about your code" />
195
  <button class="mic-btn" id="micBtn" title="Speak your question">🎀</button>
196
  </div>
197
  <script>
198
  const input = document.getElementById('questionInput');
199
  const micBtn = document.getElementById('micBtn');
200
  let recognition;
201
+ if ('webkitSpeechRecognition' in window) {
202
  recognition = new webkitSpeechRecognition();
203
  recognition.lang = 'en-US';
204
  recognition.continuous = false;
205
  recognition.interimResults = false;
206
+ micBtn.onclick = function(e) {
207
  e.preventDefault();
208
  recognition.start();
209
  micBtn.textContent = 'πŸŽ™οΈ';
210
+ };
211
+ recognition.onresult = function(event) {
212
  const transcript = event.results[0][0].transcript;
213
  input.value = transcript;
214
+ // Copy to Streamlit input
215
+ const streamlitInput = window.parent.document.querySelector('input[data-testid="stTextInput"]');
216
+ if (streamlitInput) {
217
+ streamlitInput.value = transcript;
218
+ streamlitInput.dispatchEvent(new Event('input', { bubbles: true }));
219
+ }
220
  micBtn.textContent = '🎀';
221
+ };
222
+ recognition.onerror = function() {
223
  micBtn.textContent = '🎀';
224
+ };
225
+ recognition.onend = function() {
226
  micBtn.textContent = '🎀';
227
+ };
228
+ } else {
229
  micBtn.disabled = true;
230
  micBtn.title = 'Voice not supported';
231
+ }
 
 
 
 
232
  </script>
233
+ """, unsafe_allow_html=True)
234
 
235
+ # The actual Streamlit input (for Python to read)
236
+ question = st.text_input("Ask a question about your code", key="sem_question")
237
 
238
+ if st.button("Run Semantic Search"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  if not code_input.strip() or not question.strip():
240
  st.error("Both code and question are required.")
241
  elif not code_matches_language(code_input, programming_language):
242
  st.error(f"Language mismatch. Please check your code and language selection.")
243
+ elif not is_coding_question(question):
244
+ st.warning("Please ask a relevant question.")
245
  else:
246
  with st.spinner("Running Semantic Search..."):
247
  answer = call_groq_api(f"{question}\n\nCode:\n{code_input}")