Imarticuslearning commited on
Commit
b32fad9
·
verified ·
1 Parent(s): 4a9194f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -57
app.py CHANGED
@@ -14,8 +14,6 @@ import edge_tts
14
  import pandas as pd
15
  import tempfile
16
  import traceback
17
- import soundfile as sf
18
- from streamlit_webrtc import webrtc_streamer, WebRtcMode
19
 
20
 
21
  # ✅ MUST be the first Streamlit command
@@ -815,9 +813,7 @@ 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>",
819
- unsafe_allow_html=True)
820
-
821
  if st.button("🎙️ Start Recording"):
822
  st.session_state.update({
823
  "record_phase": "recording",
@@ -845,60 +841,23 @@ if st.session_state["generated_questions"]:
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
-
860
  try:
861
- frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
862
- except Exception as e:
863
- st.error(f"⚠️ Audio capture error: {e}")
864
- frames = []
865
-
866
- if frames:
867
- try:
868
- pcm = np.concatenate([f.to_ndarray() for f in frames], axis=0)
869
- sample_rate = frames[0].sample_rate
870
- sf.write(wav_path, pcm, sample_rate)
871
- st.audio(wav_path)
872
- st.session_state["answers"].append({
873
- "question": question,
874
- "response_file": wav_path
875
- })
876
-
877
- except Exception as e:
878
- st.error(f"⚠️ Error saving recording: {e}")
879
- st.session_state["answers"].append({
880
- "question": question,
881
- "response": "[Error saving recording]"
882
- })
883
- else:
884
- st.warning("⚠️ No audio captured.")
885
- st.session_state["answers"].append({
886
- "question": question,
887
- "response": "[No response]"
888
  })
889
-
890
- st.session_state.update({
891
- "record_phase": "idle",
892
- "question_played": False,
893
- "recording_started": False,
894
- "current_question_index": idx + 1
895
- })
896
-
897
- if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
898
- evaluate_answers()
899
- st.session_state["show_summary"] = True
900
- st.experimental_rerun()
901
-
902
  else:
903
  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)
904
  st.session_state["answers"].append({"question": question, "response": "[No response]"})
 
14
  import pandas as pd
15
  import tempfile
16
  import traceback
 
 
17
 
18
 
19
  # ✅ MUST be the first Streamlit command
 
813
  elif st.session_state["record_phase"] == "waiting_to_start":
814
  remaining = 10 - int(elapsed)
815
  if remaining > 0:
816
+ st.markdown(f"<h4 class='timer-text'>⏳ {remaining} seconds to click 'Start Recording'...</h4>", unsafe_allow_html=True)
 
 
817
  if st.button("🎙️ Start Recording"):
818
  st.session_state.update({
819
  "record_phase": "recording",
 
841
  remaining = 15 - int(now - st.session_state.get("timer_start", 0))
842
  if remaining > 0:
843
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
844
+ recognizer = sr.Recognizer()
845
+ with sr.Microphone() as source:
846
+ recognizer.adjust_for_ambient_noise(source)
847
+ try:
848
+ audio = recognizer.listen(source, timeout=1, phrase_time_limit=1)
 
 
 
 
 
 
 
849
  try:
850
+ result = recognizer.recognize_google(audio)
851
+ st.session_state.update({
852
+ "recorded_text": result,
853
+ "record_phase": "listening"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
854
  })
855
+ except sr.UnknownValueError:
856
+ pass
857
+ except sr.WaitTimeoutError:
858
+ pass
859
+ time.sleep(1)
860
+ st.rerun()
 
 
 
 
 
 
 
861
  else:
862
  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)
863
  st.session_state["answers"].append({"question": question, "response": "[No response]"})