Spaces:
Sleeping
Sleeping
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 |