Imarticuslearning commited on
Commit
a10b1ba
·
verified ·
1 Parent(s): 05fd88e

Modified Recording Logic

Browse files

New streamlit approach has been added on to the model for modification of the audio input response

Files changed (1) hide show
  1. app.py +77 -5
app.py CHANGED
@@ -19,6 +19,7 @@ from twilio.rest import Client
19
  import logging
20
  import whisper
21
  import speech_recognition as sr
 
22
  #model = whisper.load_model("base")
23
 
24
 
@@ -830,7 +831,7 @@ if st.session_state["generated_questions"]:
830
  "question_played": True,
831
  "question_start_time": time.time(),
832
  "record_phase": "audio_playing",
833
- "recorded_text": ""
834
  })
835
 
836
  st.markdown(f"**Q{idx + 1}:** {question}")
@@ -881,7 +882,78 @@ if st.session_state["generated_questions"]:
881
  if remaining > 0:
882
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
883
 
884
- audio_value = st.audio_input("🎤 Tap to record — then stop when done", key=f"audio_{idx}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885
  if audio_value and "response_file" not in st.session_state:
886
  wav_path = f"response_{idx}.wav"
887
  with open(wav_path, "wb") as f:
@@ -904,7 +976,7 @@ if st.session_state["generated_questions"]:
904
  st.session_state["record_phase"] = "listening"
905
  st.success("✅ Audio uploaded. You may now confirm your answer.")
906
  #st.audio(wav_path, format="audio/wav")
907
- """
908
  if st.button("⏹️ Confirm & Next"):
909
  try:
910
  with st.spinner("🧠 Transcribing your answer..."):
@@ -916,7 +988,7 @@ if st.session_state["generated_questions"]:
916
  except Exception as e:
917
  st.error(f"❌ Transcription error: {e}")
918
  transcript = "[Transcription error]"
919
- """
920
 
921
  st.session_state["answers"].append({
922
  "question": question,
@@ -991,7 +1063,7 @@ if st.session_state["generated_questions"]:
991
  evaluate_answers()
992
  st.session_state["show_summary"] = True
993
  st.rerun()
994
-
995
 
996
  # === Summary Display ===
997
 
 
19
  import logging
20
  import whisper
21
  import speech_recognition as sr
22
+ from audiorecorder import audiorecorder
23
  #model = whisper.load_model("base")
24
 
25
 
 
831
  "question_played": True,
832
  "question_start_time": time.time(),
833
  "record_phase": "audio_playing",
834
+ # "recorded_text": ""
835
  })
836
 
837
  st.markdown(f"**Q{idx + 1}:** {question}")
 
882
  if remaining > 0:
883
  st.markdown(f"<h4 class='timer-text'>🎙️ {remaining} seconds to answer...</h4>", unsafe_allow_html=True)
884
 
885
+ # audio_value = st.audio_input("🎤 Tap to record — then stop when done", key=f"audio_{idx}")
886
+ audio = audiorecorder("🎙️ Start Recording", "⏹️ Stop Recording")
887
+ if len(audio) > 0 and "response_file" not in st.session_state:
888
+ if np.max(audio) > 0.01:
889
+ wav_path = f"response_{idx}.wav"
890
+ with open(wav_path, "wb") as f:
891
+ f.write(audio.tobytes())
892
+ st.session_state["response_file"] = wav_path
893
+ st.session_state["record_phase"] = "listening"
894
+ st.success("✅ Audio recorded. Please confirm to proceed.")
895
+ st.audio(wav_path, format="audio/wav")
896
+ st.rerun()
897
+ else:
898
+ st.warning("⚠️ Silence detected. Please try again.")
899
+ st.rerun()
900
+
901
+ else:
902
+ time.sleep(1)
903
+ st.rerun()
904
+
905
+ else:
906
+ if not st.session_state.get("response_file"):
907
+ st.warning("⚠️ No audio captured. Moving to next question.")
908
+ st.session_state["answers"].append({"question": question, "response": "[No response]"})
909
+ st.session_state.update({
910
+ "record_phase": "idle",
911
+ "question_played": False,
912
+ "current_question_index": idx + 1
913
+ })
914
+ if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
915
+ evaluate_answers()
916
+ st.session_state["show_summary"] = True
917
+ st.rerun()
918
+ elif st.session_state["record_phase"] == "listening":
919
+ st.success("🎧 Review your recorded response below:")
920
+ st.audio(st.session_state["response_file"], format="audio/wav")
921
+
922
+ if st.button("⏹️ Confirm & Next"):
923
+ recognizer = sr.Recognizer()
924
+ try:
925
+ with sr.AudioFile(st.session_state["response_file"]) as source:
926
+ audio = recognizer.record(source)
927
+ transcript = recognizer.recognize_google(audio)
928
+ except sr.UnknownValueError:
929
+ transcript = "[Could not understand audio]"
930
+ except sr.RequestError:
931
+ transcript = "[Google API error]"
932
+ except Exception as e:
933
+ transcript = f"[Transcription failed: {e}]"
934
+
935
+ st.session_state["answers"].append({
936
+ "question": question,
937
+ "response_file": st.session_state["response_file"],
938
+ "response": transcript
939
+ })
940
+
941
+ st.session_state.update({
942
+ "record_phase": "idle",
943
+ "recording_started": False,
944
+ "question_played": False,
945
+ "question_start_time": 0.0,
946
+ "current_question_index": idx + 1,
947
+ "response_file": None,
948
+ "audio_waiting": True
949
+ })
950
+
951
+ if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
952
+ evaluate_answers()
953
+ st.session_state["show_summary"] = True
954
+ st.rerun()
955
+
956
+ """
957
  if audio_value and "response_file" not in st.session_state:
958
  wav_path = f"response_{idx}.wav"
959
  with open(wav_path, "wb") as f:
 
976
  st.session_state["record_phase"] = "listening"
977
  st.success("✅ Audio uploaded. You may now confirm your answer.")
978
  #st.audio(wav_path, format="audio/wav")
979
+ ""
980
  if st.button("⏹️ Confirm & Next"):
981
  try:
982
  with st.spinner("🧠 Transcribing your answer..."):
 
988
  except Exception as e:
989
  st.error(f"❌ Transcription error: {e}")
990
  transcript = "[Transcription error]"
991
+ ""
992
 
993
  st.session_state["answers"].append({
994
  "question": question,
 
1063
  evaluate_answers()
1064
  st.session_state["show_summary"] = True
1065
  st.rerun()
1066
+ """
1067
 
1068
  # === Summary Display ===
1069