Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,149 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import random
|
3 |
+
from gtts import gTTS
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
import pygame
|
7 |
|
8 |
+
# Initialize pygame mixer
|
9 |
+
pygame.mixer.init()
|
10 |
+
|
11 |
+
# Define a list of questions and their corresponding correct answers
|
12 |
+
questions = [
|
13 |
+
{
|
14 |
+
"question": "ما هي عاصمة فرنسا؟",
|
15 |
+
"choices": ["لندن", "باريس", "برلين", "روما"],
|
16 |
+
"correct_answer": "باريس"
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"question": "ما هو أكبر كوكب في النظام الشمسي؟",
|
20 |
+
"choices": ["المريخ", "المشتري", "الأرض", "زحل"],
|
21 |
+
"correct_answer": "المشتري"
|
22 |
+
},
|
23 |
+
# Add more questions here
|
24 |
+
]
|
25 |
+
|
26 |
+
# Function to shuffle the questions
|
27 |
+
def shuffle_questions():
|
28 |
+
random.shuffle(questions)
|
29 |
+
|
30 |
+
# Function to display a question
|
31 |
+
def display_question(question):
|
32 |
+
st.markdown(f"<div class='question'>{question['question']}</div>", unsafe_allow_html=True)
|
33 |
+
|
34 |
+
# Function to convert text to speech and return audio
|
35 |
+
def text_to_speech(text):
|
36 |
+
tts = gTTS(text=text, lang='ar')
|
37 |
+
speech = io.BytesIO()
|
38 |
+
tts.write_to_fp(speech)
|
39 |
+
return speech.getvalue()
|
40 |
+
|
41 |
+
# Function to calculate the score
|
42 |
+
def calculate_score(answers):
|
43 |
+
score = 0
|
44 |
+
for i, answer in enumerate(answers):
|
45 |
+
if answer == questions[i]["correct_answer"]:
|
46 |
+
score += 1
|
47 |
+
return score
|
48 |
+
|
49 |
+
# Function to play sound effect
|
50 |
+
def play_sound(sound_path):
|
51 |
+
pygame.mixer.music.load(sound_path)
|
52 |
+
pygame.mixer.music.play()
|
53 |
+
|
54 |
+
# Main function
|
55 |
+
def main():
|
56 |
+
st.set_page_config(page_title="اختبار بسيط", page_icon=":pencil2:")
|
57 |
+
st.markdown(
|
58 |
+
"""
|
59 |
+
<style>
|
60 |
+
body {
|
61 |
+
direction: rtl;
|
62 |
+
text-align: right;
|
63 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
64 |
+
}
|
65 |
+
.question {
|
66 |
+
background-color: #f0f0f0;
|
67 |
+
padding: 10px;
|
68 |
+
border-radius: 10px;
|
69 |
+
margin-bottom: 20px;
|
70 |
+
font-size: 18px;
|
71 |
+
}
|
72 |
+
.btn-next {
|
73 |
+
background-color: #4CAF50;
|
74 |
+
color: white;
|
75 |
+
padding: 10px 20px;
|
76 |
+
border: none;
|
77 |
+
border-radius: 5px;
|
78 |
+
cursor: pointer;
|
79 |
+
font-size: 16px;
|
80 |
+
transition: background-color 0.3s;
|
81 |
+
}
|
82 |
+
.btn-next:hover {
|
83 |
+
background-color: #45a049;
|
84 |
+
}
|
85 |
+
.btn-reset {
|
86 |
+
background-color: #ff6347;
|
87 |
+
color: white;
|
88 |
+
padding: 10px 20px;
|
89 |
+
border: none;
|
90 |
+
border-radius: 5px;
|
91 |
+
cursor: pointer;
|
92 |
+
font-size: 16px;
|
93 |
+
transition: background-color 0.3s;
|
94 |
+
margin-top: 20px;
|
95 |
+
}
|
96 |
+
.btn-reset:hover {
|
97 |
+
background-color: #e74c3c;
|
98 |
+
}
|
99 |
+
</style>
|
100 |
+
""",
|
101 |
+
unsafe_allow_html=True
|
102 |
+
)
|
103 |
+
st.title(":pencil2: جامعة بشار نادي اينيفاك")
|
104 |
+
|
105 |
+
st.text("هذا التطبيق يقدم لك اختبارًا بسيطًا مكون من عدة أسئلة.")
|
106 |
+
st.write("بعد الإجابة على كل سؤال، انقر على الزر 'التالي' لعرض السؤال التالي.")
|
107 |
+
|
108 |
+
# Shuffle the questions
|
109 |
+
#shuffle_questions()
|
110 |
+
|
111 |
+
# Display each question one at a time
|
112 |
+
current_question_index = st.session_state.get("current_question_index", 0)
|
113 |
+
total_score = st.session_state.get("total_score", 0)
|
114 |
+
answers = st.session_state.get("answers", [])
|
115 |
+
if current_question_index < len(questions):
|
116 |
+
st.header(f"السؤال {current_question_index + 1} من {len(questions)}:")
|
117 |
+
display_question(questions[current_question_index])
|
118 |
+
# Convert question to speech and play the audio
|
119 |
+
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]
|
120 |
+
st.audio(text_to_speech(txtToAudio), format="audio/mp3")
|
121 |
+
user_answer = st.radio("إجابتك:", options=questions[current_question_index]["choices"])
|
122 |
+
answers.append(user_answer)
|
123 |
+
st.session_state["answers"] = answers
|
124 |
+
if st.button("التالي", key="next-btn", help=""):
|
125 |
+
if user_answer == questions[current_question_index]["correct_answer"]:
|
126 |
+
total_score += 1
|
127 |
+
st.session_state["total_score"] = total_score
|
128 |
+
st.session_state["current_question_index"] = current_question_index + 1
|
129 |
+
st.experimental_rerun()
|
130 |
+
|
131 |
+
else:
|
132 |
+
# Display total score
|
133 |
+
st.subheader("النتيجة الكلية:")
|
134 |
+
st.write(f"لقد حصلت على {total_score} من أصل {len(questions)}.")
|
135 |
+
if total_score == len(questions):
|
136 |
+
play_sound("success.mp3") # Play success sound
|
137 |
+
if st.button("إعادة الاختبار", key="reset-btn", help=""):
|
138 |
+
st.session_state["current_question_index"] = 0
|
139 |
+
st.session_state["total_score"] = 0
|
140 |
+
st.session_state["answers"] = []
|
141 |
+
st.experimental_rerun()
|
142 |
+
|
143 |
+
# Run the main function
|
144 |
+
if __name__ == "__main__":
|
145 |
+
main()
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
#pip install pygame
|