Imarticuslearning commited on
Commit
8b07eda
·
verified ·
1 Parent(s): 4ef81e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -815,7 +815,9 @@ if st.session_state["generated_questions"]:
815
  elif st.session_state["record_phase"] == "waiting_to_start":
816
  remaining = 10 - int(elapsed)
817
  if remaining > 0:
818
- st.markdown(f"<h4 class='timer-text'>⏳ {remaining} seconds to click 'Start Recording'...</h4>", unsafe_allow_html=True)
 
 
819
  if st.button("🎙️ Start Recording"):
820
  st.session_state.update({
821
  "record_phase": "recording",
@@ -843,55 +845,58 @@ 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
  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]"})
 
815
  elif st.session_state["record_phase"] == "waiting_to_start":
816
  remaining = 10 - int(elapsed)
817
  if remaining > 0:
818
+ st.markdown(f"<h4 class='timer-text'>⏳ {remaining} seconds to click 'Start Recording'...</h4>",
819
+ unsafe_allow_html=True)
820
+
821
  if st.button("🎙️ Start Recording"):
822
  st.session_state.update({
823
  "record_phase": "recording",
 
845
  remaining = 15 - int(now - st.session_state.get("timer_start", 0))
846
  if remaining > 0:
847
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
848
+
849
  webrtc_ctx = webrtc_streamer(
850
  key=f"webrtc_{idx}",
851
  mode=WebRtcMode.SENDONLY,
852
  media_stream_constraints={"audio": True, "video": False},
853
  audio_receiver_size=256
854
  )
855
+
856
  if webrtc_ctx.state.playing:
857
  if st.button("⏹️ Stop Recording"):
858
  wav_path = f"response_{idx}.wav"
 
 
 
 
 
 
859
  try:
860
+ frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
 
 
 
 
 
 
 
 
861
  except Exception as e:
862
+ st.error(f"⚠️ Audio capture error: {e}")
863
+ frames = []
864
+ if frames:
865
+ try:
866
+ pcm = np.concatenate([f.to_ndarray() for f in frames], axis=0)
867
+ sample_rate = frames[0].sample_rate
868
+ sf.write(wav_path, pcm, sample_rate)
869
+ st.audio(wav_path)
870
+ st.session_state["answers"].append({
871
+ "question": question,
872
+ "response_file": wav_path
873
+ })
874
+
875
+ except Exception as e:
876
+ st.error(f"⚠️ Error saving recording: {e}")
877
+ st.session_state["answers"].append({
878
  "question": question,
879
  "response": "[Error saving recording]"
880
+ })
881
+ else:
882
+ st.warning("⚠️ No audio captured.")
883
+ st.session_state["answers"].append({
884
  "question": question,
885
  "response": "[No response]"
886
+ })
887
 
888
+ st.session_state.update({
889
  "record_phase": "idle",
890
  "question_played": False,
891
  "recording_started": False,
892
  "current_question_index": idx + 1
893
+ })
894
+
895
+ if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
896
  evaluate_answers()
897
  st.session_state["show_summary"] = True
898
+ st.experimental_rerun()
899
+
900
  else:
901
  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)
902
  st.session_state["answers"].append({"question": question, "response": "[No response]"})