Imarticuslearning commited on
Commit
d03f528
·
verified ·
1 Parent(s): 738bf1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -14
app.py CHANGED
@@ -843,23 +843,55 @@ if st.session_state["generated_questions"]:
843
  remaining = 15 - int(now - st.session_state.get("timer_start", 0))
844
  if remaining > 0:
845
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
846
- recognizer = sr.Recognizer()
847
- with sr.Microphone() as source:
848
- recognizer.adjust_for_ambient_noise(source)
 
 
 
 
 
 
849
  try:
850
- audio = recognizer.listen(source, timeout=1, phrase_time_limit=1)
 
 
 
 
851
  try:
852
- result = recognizer.recognize_google(audio)
853
- st.session_state.update({
854
- "recorded_text": result,
855
- "record_phase": "listening"
 
 
 
856
  })
857
- except sr.UnknownValueError:
858
- pass
859
- except sr.WaitTimeoutError:
860
- pass
861
- time.sleep(1)
862
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
863
  else:
864
  st.markdown("<div style='padding:10px; background:#fff3e0; border-left:5px solid orange;'>⚠️ <strong>No response detected.</strong> Moving to next question...</div>", unsafe_allow_html=True)
865
  st.session_state["answers"].append({"question": question, "response": "[No response]"})
 
843
  remaining = 15 - int(now - st.session_state.get("timer_start", 0))
844
  if remaining > 0:
845
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
846
+ webrtc_ctx = webrtc_streamer(
847
+ key=f"webrtc_{idx}",
848
+ mode=WebRtcMode.SENDONLY,
849
+ media_stream_constraints={"audio": True, "video": False},
850
+ audio_receiver_size=256
851
+ )
852
+ if webrtc_ctx.state.playing:
853
+ if st.button("⏹️ Stop Recording"):
854
+ wav_path = f"response_{idx}.wav"
855
  try:
856
+ frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
857
+ except Exception as e:
858
+ st.error(f"⚠️ Audio capture error: {e}")
859
+ frames = []
860
+ if frames:
861
  try:
862
+ pcm = np.concatenate([f.to_ndarray() for f in frames], axis=0)
863
+ sample_rate = frames[0].sample_rate
864
+ sf.write(wav_path, pcm, sample_rate)
865
+ st.audio(wav_path)
866
+ st.session_state["answers"].append({
867
+ "question": question,
868
+ "response_file": wav_path
869
  })
870
+
871
+ except Exception as e:
872
+ st.error(f"⚠️ Error saving recording: {e}")
873
+ st.session_state["answers"].append({
874
+ "question": question,
875
+ "response": "[Error saving recording]"
876
+ })
877
+ else:
878
+ st.warning("⚠️ No audio captured.")
879
+ st.session_state["answers"].append({
880
+ "question": question,
881
+ "response": "[No response]"
882
+ })
883
+
884
+ st.session_state.update({
885
+ "record_phase": "idle",
886
+ "question_played": False,
887
+ "recording_started": False,
888
+ "current_question_index": idx + 1
889
+ })
890
+ if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
891
+ evaluate_answers()
892
+ st.session_state["show_summary"] = True
893
+ st.experimental_rerun()
894
+ st.experimental_rerun()
895
  else:
896
  st.markdown("<div style='padding:10px; background:#fff3e0; border-left:5px solid orange;'>⚠️ <strong>No response detected.</strong> Moving to next question...</div>", unsafe_allow_html=True)
897
  st.session_state["answers"].append({"question": question, "response": "[No response]"})