Sina Media Lab commited on
Commit
bad6a07
Β·
1 Parent(s): 32f2e18

Add application file 5

Browse files
Files changed (2) hide show
  1. app.py +73 -61
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,71 +1,83 @@
1
  import streamlit as st
2
- import importlib
3
 
4
- # List of available modules with shorter names and icons
5
- module_names = {
6
- "Bases": "presentation_bases",
7
- "Validity": "valid_invalid_numbers",
8
- "Conversion": "conversion_bases",
9
- "Grouping": "grouping_techniques",
10
- "Addition": "addition_bases",
11
- "2's Complement": "twos_complement",
12
- "Negative Numbers": "negative_binary",
13
- "Subtraction": "subtraction_bases",
14
- }
15
 
16
- # Initialize session state for tracking answers and progress
17
- if 'correct_count' not in st.session_state:
18
- st.session_state.correct_count = 0
19
- if 'question_count' not in st.session_state:
20
- st.session_state.question_count = 0
21
- if 'current_module' not in st.session_state:
22
- st.session_state.current_module = None
23
 
24
- # Streamlit interface
25
- st.sidebar.title("Quiz Modules")
26
- module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- if module_name:
29
- module_file = module_names[module_name]
30
- try:
31
- module = importlib.import_module(f'modules.{module_file}')
32
- generate_question = module.generate_question
33
- title = getattr(module, 'title', f"{module_name} Module")
34
- description = getattr(module, 'description', "Description is not available")
35
-
36
- # Show module title and description
37
- st.title(title)
38
- st.write(description)
39
 
40
- # Add spacing to ensure question visibility on Hugging Face
41
- st.markdown("<br><br>", unsafe_allow_html=True)
 
 
 
 
42
 
43
- # Generate and display the question
44
- question, options, correct_answer, explanation = generate_question()
45
- st.write(f"**Question:** {question}")
46
- answer = st.radio("Choose an answer:", options)
 
47
 
48
- # Track user's response
49
- if st.button("Submit"):
50
- st.session_state.question_count += 1
51
- if answer == correct_answer:
52
- st.session_state.correct_count += 1
53
- st.success("Correct!", icon="βœ…")
54
- else:
55
- st.error("Incorrect.", icon="❌")
56
- st.write(f"**Explanation:** {explanation}")
57
 
58
- # Option to go to the next or previous question
59
- col1, col2 = st.columns(2)
60
- if col1.button("Previous Question"):
61
- st.experimental_rerun() # To simulate going back (if history is managed)
62
- if col2.button("Next Question"):
63
- st.experimental_rerun() # To simulate moving forward
64
 
65
- # Show percentage of correct answers
66
- if st.session_state.question_count > 0:
67
- correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
68
- st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
69
-
70
- except ModuleNotFoundError:
71
- st.error(f"The module '{module_name}' was not found. Please select another module.")
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from fpdf import FPDF
3
 
4
+ # Initialize session state to keep track of questions
5
+ if 'questions' not in st.session_state:
6
+ st.session_state.questions = []
7
+ if 'current_question' not in st.session_state:
8
+ st.session_state.current_question = 0
9
+ if 'pdf_report' not in st.session_state:
10
+ st.session_state.pdf_report = None
 
 
 
 
11
 
12
+ # Emojis for correct/incorrect and PDF
13
+ correct_emoji = "βœ…"
14
+ incorrect_emoji = "❌"
15
+ pdf_emoji = "πŸ“„"
 
 
 
16
 
17
+ # Example questions
18
+ questions = [
19
+ {
20
+ "question": "What is the binary equivalent of the decimal number 13?",
21
+ "options": ["1100", "1101", "1011", "1111", "1001"],
22
+ "answer": "1101"
23
+ },
24
+ {
25
+ "question": "Which of the following is a valid hexadecimal number?",
26
+ "options": ["1G4", "FACE", "XYZ", "1.5", "0x23G5"],
27
+ "answer": "FACE"
28
+ }
29
+ # Add more questions here
30
+ ]
31
 
32
+ # Function to display a question
33
+ def display_question():
34
+ current_q = questions[st.session_state.current_question]
35
+ st.write(f"**Question {st.session_state.current_question + 1}:** {current_q['question']}")
36
+ user_answer = st.radio("Choose an answer:", current_q['options'])
37
+
38
+ if st.button("Submit Answer"):
39
+ correct_answer = current_q['answer']
40
+ status = "Correct" if user_answer == correct_answer else "Incorrect"
41
+ emoji = correct_emoji if status == "Correct" else incorrect_emoji
 
42
 
43
+ st.session_state.questions.append({
44
+ 'question': current_q['question'],
45
+ 'user_answer': user_answer,
46
+ 'correct_answer': correct_answer,
47
+ 'status': status
48
+ })
49
 
50
+ st.markdown(f"**You answered:** {user_answer} {emoji}")
51
+ if status == "Correct":
52
+ st.success(f"Correct! {correct_emoji}")
53
+ else:
54
+ st.error(f"Incorrect. The correct answer is {correct_answer} {incorrect_emoji}")
55
 
56
+ if len(st.session_state.questions) > 0 and st.session_state.current_question < len(questions) - 1:
57
+ if st.button("Next Question"):
58
+ st.session_state.current_question += 1
59
+ st.experimental_rerun()
 
 
 
 
 
60
 
61
+ if len(st.session_state.questions) > 0:
62
+ if st.button(f"{pdf_emoji} Download PDF Report"):
63
+ generate_pdf_report(st.session_state.questions)
64
+ st.download_button(label=f"{pdf_emoji} Download PDF", data=st.session_state.pdf_report, file_name="quiz_report.pdf")
 
 
65
 
66
+ # Function to generate a PDF report
67
+ def generate_pdf_report(questions):
68
+ pdf = FPDF()
69
+ pdf.add_page()
70
+ pdf.set_font("Arial", size=12)
71
+
72
+ for i, q in enumerate(questions, 1):
73
+ pdf.cell(200, 10, txt=f"Question {i}: {q['question']}", ln=True)
74
+ pdf.cell(200, 10, txt=f"Your Answer: {q['user_answer']}", ln=True)
75
+ pdf.cell(200, 10, txt=f"Correct Answer: {q['correct_answer']}", ln=True)
76
+ pdf.cell(200, 10, txt=f"Status: {q['status']}", ln=True)
77
+ pdf.cell(200, 10, txt="---------------------------------", ln=True)
78
+
79
+ st.session_state.pdf_report = pdf.output(dest='S').encode('latin1')
80
+
81
+ # Main app logic
82
+ st.title("Multiple Choice Quiz")
83
+ display_question()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  streamlit
2
- huggingface_hub
 
 
1
  streamlit
2
+ huggingface_hub
3
+ fpdf