import streamlit as st
import random
from gtts import gTTS
import io
import base64
import pygame
# Initialize pygame mixer
pygame.mixer.init()
# Define a list of questions and their corresponding correct answers
questions = [
{
"question": "ما هي عاصمة فرنسا؟",
"choices": ["لندن", "باريس", "برلين", "روما"],
"correct_answer": "باريس"
},
{
"question": "ما هو أكبر كوكب في النظام الشمسي؟",
"choices": ["المريخ", "المشتري", "الأرض", "زحل"],
"correct_answer": "المشتري"
},
# Add more questions here
]
# Function to shuffle the questions
def shuffle_questions():
random.shuffle(questions)
# Function to display a question
def display_question(question):
st.markdown(f"
{question['question']}
", unsafe_allow_html=True)
# Function to convert text to speech and return audio
def text_to_speech(text):
tts = gTTS(text=text, lang='ar')
speech = io.BytesIO()
tts.write_to_fp(speech)
return speech.getvalue()
# Function to calculate the score
def calculate_score(answers):
score = 0
for i, answer in enumerate(answers):
if answer == questions[i]["correct_answer"]:
score += 1
return score
# Function to play sound effect
def play_sound(sound_path):
pygame.mixer.music.load(sound_path)
pygame.mixer.music.play()
# Main function
def main():
st.set_page_config(page_title="اختبار بسيط", page_icon=":pencil2:")
st.markdown(
"""
""",
unsafe_allow_html=True
)
st.title(":pencil2: جامعة بشار نادي اينيفاك")
st.text("هذا التطبيق يقدم لك اختبارًا بسيطًا مكون من عدة أسئلة.")
st.write("بعد الإجابة على كل سؤال، انقر على الزر 'التالي' لعرض السؤال التالي.")
# Shuffle the questions
#shuffle_questions()
# Display each question one at a time
current_question_index = st.session_state.get("current_question_index", 0)
total_score = st.session_state.get("total_score", 0)
answers = st.session_state.get("answers", [])
if current_question_index < len(questions):
st.header(f"السؤال {current_question_index + 1} من {len(questions)}:")
display_question(questions[current_question_index])
# Convert question to speech and play the audio
txtToAudio=questions[current_question_index]["question"]+"الخيارات"+","+questions[current_question_index]["choices"][0]+","+questions[current_question_index]["choices"][1]+","+questions[current_question_index]["choices"][2]+ ","+questions[current_question_index]["choices"][3]
st.audio(text_to_speech(txtToAudio), format="audio/mp3")
user_answer = st.radio("إجابتك:", options=questions[current_question_index]["choices"])
answers.append(user_answer)
st.session_state["answers"] = answers
if st.button("التالي", key="next-btn", help=""):
if user_answer == questions[current_question_index]["correct_answer"]:
total_score += 1
st.session_state["total_score"] = total_score
st.session_state["current_question_index"] = current_question_index + 1
st.experimental_rerun()
else:
# Display total score
st.subheader("النتيجة الكلية:")
st.write(f"لقد حصلت على {total_score} من أصل {len(questions)}.")
if total_score == len(questions):
play_sound("success.mp3") # Play success sound
if st.button("إعادة الاختبار", key="reset-btn", help=""):
st.session_state["current_question_index"] = 0
st.session_state["total_score"] = 0
st.session_state["answers"] = []
st.experimental_rerun()
# Run the main function
if __name__ == "__main__":
main()
#pip install pygame