Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
import pygame | |
from gtts import gTTS | |
import io | |
import json | |
# Initialize pygame | |
pygame.init() | |
# Set Streamlit direction to right-to-left (RTL) | |
st.markdown(""" | |
<style> | |
body { | |
direction: rtl; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Load questions from JSON file | |
def load_questions(file_path): | |
with open(file_path, "r", encoding="utf-8") as file: | |
questions = json.load(file) | |
return questions | |
# Define a list of Arabic questions and their corresponding correct answers | |
questions = load_questions("questions.json") | |
def text_to_speech(text): | |
tts = gTTS(text=text, lang='ar') | |
speech = io.BytesIO() | |
tts.write_to_fp(speech) | |
return speech.getvalue() | |
def play_correct_sound(): | |
pygame.mixer.init() | |
# Load the sound file | |
sound_path = "success.mp3" | |
pygame.mixer.music.load(sound_path) | |
# Play the sound file | |
pygame.mixer.music.play() | |
def quiz(): | |
# Convert question to speech and play the audio | |
selected_question_index = st.session_state.get('selected_question_index', None) | |
# If there is no selected question index, choose one randomly | |
if selected_question_index is None: | |
selected_question_index = random.randint(0, len(questions) - 1) | |
st.session_state.selected_question_index = selected_question_index | |
txtToAudio=questions[selected_question_index]["question"]+"الخيارات"+","+questions[selected_question_index]["choices"][0]+","+questions[selected_question_index]["choices"][1]+","+questions[selected_question_index]["choices"][2]+ "." | |
st.audio(text_to_speech(txtToAudio), format="audio/mp3") | |
selected_question = questions[selected_question_index] | |
question_text = selected_question["question"] | |
choices = selected_question["choices"] | |
correct_answer = selected_question["correct_answer"] | |
# Display the question | |
st.write(question_text) | |
# Get user's choice | |
user_choice = st.radio('اختر الإجابة الصحيحة:', options=choices, key='radio') | |
# Check if the user clicked "Check" | |
if st.button("التحقق"): | |
# Check if the user's choice is correct | |
if user_choice == correct_answer: | |
#play_correct_sound() | |
st.success("تهانينا! لقد فزت! 🎉😊👍") | |
else: | |
st.error("آسف، هذا غير صحيح. الإجابة الصحيحة هي: " + correct_answer + " 😔") | |
def main(): | |
st.title("المدرسة العليا للأساتذة نادي يونيفاك ✒️") | |
st.write("مرحبًا بك في تطبيق اختبار الثقافة العربية! انقر أدناه لبدء الاختبار.") | |
quiz() | |
if __name__ == "__main__": | |
main() | |