Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -818,181 +818,7 @@ def get_ice_servers():
|
|
| 818 |
|
| 819 |
|
| 820 |
# === Main QA Interface ===
|
| 821 |
-
"""
|
| 822 |
-
if st.session_state["generated_questions"]:
|
| 823 |
-
idx = st.session_state["current_question_index"]
|
| 824 |
-
if idx < len(st.session_state["generated_questions"]):
|
| 825 |
-
question = st.session_state["generated_questions"][idx].lstrip("1234567890. ").strip()
|
| 826 |
-
|
| 827 |
-
# Phase 0: Play audio first and wait 5s before countdown
|
| 828 |
-
if not st.session_state.get("question_played"):
|
| 829 |
-
st.session_state["question_audio_file"] = asyncio.run(generate_question_audio(question))
|
| 830 |
-
st.session_state.update({
|
| 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}")
|
| 838 |
-
st.audio(st.session_state["question_audio_file"], format="audio/mp3")
|
| 839 |
-
|
| 840 |
-
now = time.time()
|
| 841 |
-
elapsed = now - st.session_state.get("question_start_time", 0)
|
| 842 |
-
|
| 843 |
-
if st.session_state["record_phase"] == "audio_playing":
|
| 844 |
-
if elapsed < 5:
|
| 845 |
-
st.markdown(f"<h4 class='timer-text'>🔊 Playing question audio... Please listen</h4>", unsafe_allow_html=True)
|
| 846 |
-
time.sleep(1)
|
| 847 |
-
st.rerun()
|
| 848 |
-
else:
|
| 849 |
-
st.session_state["record_phase"] = "waiting_to_start"
|
| 850 |
-
st.session_state["question_start_time"] = time.time()
|
| 851 |
-
st.rerun()
|
| 852 |
-
|
| 853 |
-
elif st.session_state["record_phase"] == "waiting_to_start":
|
| 854 |
-
remaining = 10 - int(elapsed)
|
| 855 |
-
if remaining > 0:
|
| 856 |
-
st.markdown(f"<h4 class='timer-text'>⏳ {remaining} seconds to click 'Start Recording'...</h4>", unsafe_allow_html=True)
|
| 857 |
-
if st.button("🎙️ Start Recording"):
|
| 858 |
-
st.session_state.update({
|
| 859 |
-
"record_phase": "recording",
|
| 860 |
-
"timer_start": time.time(),
|
| 861 |
-
"recording_started": False
|
| 862 |
-
})
|
| 863 |
-
st.rerun()
|
| 864 |
-
time.sleep(1)
|
| 865 |
-
st.rerun()
|
| 866 |
-
else:
|
| 867 |
-
st.markdown("<div style='padding:10px; background:#fff8e1; border-left:5px solid orange;color: #212529;'>⚠️ <strong>No action detected.</strong> Automatically skipping to next question...</div>", unsafe_allow_html=True)
|
| 868 |
-
st.session_state["answers"].append({"question": question, "response": "[No response]"})
|
| 869 |
-
st.session_state.update({
|
| 870 |
-
"record_phase": "idle",
|
| 871 |
-
"question_played": False,
|
| 872 |
-
"question_start_time": 0.0,
|
| 873 |
-
"current_question_index": idx + 1
|
| 874 |
-
})
|
| 875 |
-
if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
|
| 876 |
-
evaluate_answers()
|
| 877 |
-
st.session_state["show_summary"] = True
|
| 878 |
-
st.rerun()
|
| 879 |
|
| 880 |
-
elif st.session_state["record_phase"] == "recording":
|
| 881 |
-
remaining = 15 - int(now - st.session_state.get("timer_start", 0))
|
| 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 |
-
if audio_value and "response_file" not in st.session_state:
|
| 887 |
-
wav_path = f"response_{idx}.wav"
|
| 888 |
-
with open(wav_path, "wb") as f:
|
| 889 |
-
f.write(audio_value.getbuffer())
|
| 890 |
-
recognizer = sr.Recognizer()
|
| 891 |
-
try:
|
| 892 |
-
with sr.AudioFile(wav_path) as source:
|
| 893 |
-
audio = recognizer.record(source)
|
| 894 |
-
transcript = recognizer.recognize_google(audio)
|
| 895 |
-
except sr.UnknownValueError:
|
| 896 |
-
transcript = "[Could not understand audio]"
|
| 897 |
-
except sr.RequestError:
|
| 898 |
-
transcript = "[Google API error]"
|
| 899 |
-
except Exception as e:
|
| 900 |
-
transcript = f"[Transcription failed: {e}]"
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
#st.audio(wav_path, format="audio/wav")
|
| 904 |
-
st.session_state["response_file"] = wav_path
|
| 905 |
-
st.session_state["record_phase"] = "listening"
|
| 906 |
-
st.success("✅ Audio uploaded. You may now confirm your answer.")
|
| 907 |
-
#st.audio(wav_path, format="audio/wav")
|
| 908 |
-
""
|
| 909 |
-
if st.button("⏹️ Confirm & Next"):
|
| 910 |
-
try:
|
| 911 |
-
with st.spinner("🧠 Transcribing your answer..."):
|
| 912 |
-
result = model.transcribe(st.session_state["response_file"])
|
| 913 |
-
transcript = result["text"].strip()
|
| 914 |
-
if not transcript:
|
| 915 |
-
transcript = "[Transcription failed or empty]"
|
| 916 |
-
|
| 917 |
-
except Exception as e:
|
| 918 |
-
st.error(f"❌ Transcription error: {e}")
|
| 919 |
-
transcript = "[Transcription error]"
|
| 920 |
-
""
|
| 921 |
-
|
| 922 |
-
st.session_state["answers"].append({
|
| 923 |
-
"question": question,
|
| 924 |
-
"response_file": st.session_state["response_file"],
|
| 925 |
-
"response": transcript
|
| 926 |
-
})
|
| 927 |
-
|
| 928 |
-
if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
|
| 929 |
-
evaluate_answers()
|
| 930 |
-
st.session_state["show_summary"] = True
|
| 931 |
-
st.rerun()
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
if elapsed > 15 and "response_file" not in st.session_state:
|
| 936 |
-
st.warning("⚠️ No audio captured. Moving to next question.")
|
| 937 |
-
st.session_state["answers"].append({
|
| 938 |
-
"question": question,
|
| 939 |
-
"response": "[No response]"
|
| 940 |
-
})
|
| 941 |
-
|
| 942 |
-
st.session_state.update({
|
| 943 |
-
"record_phase": "idle",
|
| 944 |
-
"question_played": False,
|
| 945 |
-
"current_question_index": idx + 1
|
| 946 |
-
})
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
|
| 950 |
-
evaluate_answers()
|
| 951 |
-
st.session_state["show_summary"] = True
|
| 952 |
-
st.rerun()
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
else:
|
| 957 |
-
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)
|
| 958 |
-
st.session_state["answers"].append({"question": question, "response": "[No response]"})
|
| 959 |
-
st.session_state.update({
|
| 960 |
-
"record_phase": "idle",
|
| 961 |
-
"recording_started": False,
|
| 962 |
-
"question_played": False,
|
| 963 |
-
"question_start_time": 0.0,
|
| 964 |
-
"current_question_index": idx + 1
|
| 965 |
-
})
|
| 966 |
-
if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
|
| 967 |
-
evaluate_answers()
|
| 968 |
-
st.session_state["show_summary"] = True
|
| 969 |
-
st.rerun()
|
| 970 |
-
|
| 971 |
-
elif st.session_state["record_phase"] == "listening":
|
| 972 |
-
st.success("🎧 Review your recorded response below:")
|
| 973 |
-
st.audio(st.session_state["response_file"], format="audio/wav")
|
| 974 |
-
|
| 975 |
-
if st.button("⏹️ Confirm & Next"):
|
| 976 |
-
st.session_state["answers"].append({
|
| 977 |
-
"question": question,
|
| 978 |
-
"response_file": st.session_state["response_file"]
|
| 979 |
-
})
|
| 980 |
-
|
| 981 |
-
st.session_state.update({
|
| 982 |
-
"record_phase": "idle",
|
| 983 |
-
"recording_started": False,
|
| 984 |
-
"question_played": False,
|
| 985 |
-
"question_start_time": 0.0,
|
| 986 |
-
"current_question_index": idx + 1,
|
| 987 |
-
"response_file": None,
|
| 988 |
-
"audio_waiting": True
|
| 989 |
-
})
|
| 990 |
-
|
| 991 |
-
if st.session_state["current_question_index"] == len(st.session_state["generated_questions"]):
|
| 992 |
-
evaluate_answers()
|
| 993 |
-
st.session_state["show_summary"] = True
|
| 994 |
-
st.rerun()
|
| 995 |
-
"""
|
| 996 |
|
| 997 |
if st.session_state.get("generated_questions"):
|
| 998 |
idx = st.session_state.get("current_question_index", 0)
|
|
|
|
| 818 |
|
| 819 |
|
| 820 |
# === Main QA Interface ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
|
| 823 |
if st.session_state.get("generated_questions"):
|
| 824 |
idx = st.session_state.get("current_question_index", 0)
|