Spaces:
Sleeping
Sleeping
File size: 5,185 Bytes
2962d5f 495a32b 2962d5f 495a32b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
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"<div class='question'>{question['question']}</div>", 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(
"""
<style>
body {
direction: rtl;
text-align: right;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.question {
background-color: #f0f0f0;
padding: 10px;
border-radius: 10px;
margin-bottom: 20px;
font-size: 18px;
}
.btn-next {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.btn-next:hover {
background-color: #45a049;
}
.btn-reset {
background-color: #ff6347;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
margin-top: 20px;
}
.btn-reset:hover {
background-color: #e74c3c;
}
</style>
""",
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 |