Prageeth-1 commited on
Commit
26eac2b
·
verified ·
1 Parent(s): c52a275

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -184,11 +184,10 @@ with tab2:
184
 
185
 
186
  question = st.text_input("Enter your question:")
187
- use_voice = False
188
 
189
- if st.button("🎤 Speak"):
190
- question = recognize_speech()
191
- use_voice = True
192
 
193
  if st.button("Get Answer") and context and question:
194
  with st.spinner("Searching for answers..."):
@@ -201,17 +200,30 @@ with tab2:
201
  st.subheader("Details")
202
  st.write(f"Confidence: {result['score']:.2f}")
203
 
 
 
 
 
 
 
 
204
 
205
- if use_voice:
206
- tts = gTTS(result['answer']) # Convert text answer to speech
207
- temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") # Create temp file
208
- tts.save(temp_audio.name)
209
 
210
- # Play the Answer
211
- st.audio(temp_audio.name, format="audio/mp3")
 
 
 
212
 
213
- # Cleanup temp file
214
- os.remove(temp_audio.name)
 
 
 
215
 
216
 
217
  with tab3:
 
184
 
185
 
186
  question = st.text_input("Enter your question:")
 
187
 
188
+ recognizer = sr.Recognizer()
189
+
190
+
191
 
192
  if st.button("Get Answer") and context and question:
193
  with st.spinner("Searching for answers..."):
 
200
  st.subheader("Details")
201
  st.write(f"Confidence: {result['score']:.2f}")
202
 
203
+ if st.button("🎤 Ask Question via Mic"):
204
+ with sr.Microphone() as source:
205
+ st.info("Listening...")
206
+ try:
207
+ audio = recognizer.listen(source, timeout=5)
208
+ question = recognizer.recognize_google(audio)
209
+ st.success(f"Question: {question}")
210
 
211
+ if uploaded_file is not None:
212
+ context = ' '.join(df['content'].tolist()) # Extracted from uploaded news data
213
+ qa_pipeline = load_qa_model()
214
+ result = qa_pipeline(question=question, context=context)
215
 
216
+ st.subheader("📝 Answer")
217
+ st.success(result['answer'])
218
+ st.write(f"Confidence: {result['score']:.2f}")
219
+ else:
220
+ st.warning("Please upload a CSV file first.")
221
 
222
+ except sr.UnknownValueError:
223
+ st.error("Sorry, could not understand the question. Please try again.")
224
+ except sr.RequestError:
225
+ st.error("Speech Recognition API is unavailable.")
226
+
227
 
228
 
229
  with tab3: