import os import streamlit as st from dotenv import load_dotenv from docx import Document import fitz # PyMuPDF import google.generativeai as genai def extract_text_from_pdf(file): """Extract text from PDF.""" text = "" doc = fitz.open(stream=file.read(), filetype="pdf") for page in doc: text += page.get_text() return text def create_multiple_choice_prompt(num_questions, quiz_context, expertise): """Create the prompt template for multiple-choice quiz.""" template = f""" You are an expert in {expertise}. Generate a quiz with {num_questions} multiple-choice questions that are relevant to {expertise} based on the following content: {quiz_context}. The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field. also add a blooms taxonomy skills with each question that is which question is generated on which blooms taxonomy. The format of the quiz is as follows: - Multiple-choice: - Questions: 1. : a. Answer 1 b. Answer 2 c. Answer 3 d. Answer 4 2. : a. Answer 1 b. Answer 2 c. Answer 3 d. Answer 4 .... - Answers: 1. 2. .... Example: - Questions: 1. What is the time complexity of a binary search tree? a. O(n) b. O(log n) c. O(n^2) d. O(1) - Answers: 1. b """ return template def create_true_false_prompt(num_questions, quiz_context, expertise): """Create the prompt template for true-false quiz.""" template = f""" You are an expert in {expertise}. Generate a quiz with {num_questions} true-false questions that are relevant to {expertise} based on the following content: {quiz_context}. The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field. also add a blooms taxonomy skills with each question that is which question is generated on which blooms taxonomy. The format of the quiz is as follows: - True-false: - Questions: 1. : 2. : ..... - Answers: 1. 2. ..... Example: - Questions: 1. A binary search tree is a type of data structure. 2. Binary search trees are typically used for sorting and searching operations. - Answers: 1. True 2. True """ return template def create_open_ended_prompt(num_questions, quiz_context, expertise): """Create the prompt template for open-ended quiz.""" template = f""" You are an expert in {expertise}. Generate a quiz with {num_questions} open-ended questions that are relevant to {expertise} based on the following content: {quiz_context}. The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field. For each question, also specify the Bloom's Taxonomy level it corresponds to, choosing from the following levels: 1. Remember 2. Understand 3. Apply 4. Analyze 5. Evaluate 6. Create The format of the quiz is as follows: - Open-ended: - Questions: 1. 2. .... Example: - Questions: 1. What is a binary search tree? 2. How are binary search trees implemented? """ return template def create_fill_in_the_blank_prompt(num_questions, quiz_context, expertise): """Create the prompt template for fill-in-the-blank quiz.""" template = f""" You are an expert in {expertise}. Generate a quiz with {num_questions} fill-in-the-blank questions that are relevant to {expertise} based on the following content: {quiz_context}. The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field. also add a blooms taxonomy skills with each question that is which question is generated on which blooms taxonomy. The format of the quiz is as follows: - Fill-in-the-blank: - Questions: 1. : 2. : .... Example: - Questions: 1. A binary search tree is a ________ data structure. 2. Binary search trees are implemented using ________. - Answers: 1. hierarchical 2. linked lists """ return template def create_mixed_questions_prompt(num_questions, quiz_context, expertise): """Create the prompt template for a mix of all question types.""" template = f""" You are an expert in {expertise}. Generate a quiz with exactly {num_questions} questions that include a random mix of multiple-choice, true-false, open-ended, and fill-in-the-blank questions relevant to {expertise} based on the following content: {quiz_context}. The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field. Ensure that the questions are randomly mixed among the different types. also add a blooms taxonomy skills with each question that is which question is generated on which blooms taxonomy. The format of the quiz is as follows: - Mixed Questions: - Questions: 1. (Question type): 2. (Question type): 3. (Question type): ... {num_questions}. (Question type): Example: - Questions: 1. What is the time complexity of a binary search tree? (Multiple-choice) a. O(n) b. O(log n) c. O(n^2) d. O(1) 2. A binary search tree is a type of data structure. (True/False) 3. What is a binary search tree? (Open-ended) 4. A binary search tree is a ________ data structure. (Fill-in-the-blank) 5. Another sample question. (Multiple-choice) a. Sample 1 b. Sample 2 c. Sample 3 d. Sample 4 - Answers: 1. b 2. True 3. A binary search tree is a data structure used to store data in a sorted manner. 4. hierarchical 5. b Note:Ensure there are exactly {num_questions} questions in total with a random mix of question types.Here only template is given if there are more than mentioned example questions generate questions by your own. """ return template def get_gemini_response(question, prompt): """Function to load Google Gemini model and provide queries as response.""" model = genai.GenerativeModel('gemini-1.5-flash') response = model.generate_content([prompt, question]) return response.text def split_questions_answers(quiz_response): """Function that splits the questions and answers from the quiz response.""" if "Answers:" in quiz_response: questions = quiz_response.split("Answers:")[0] answers = quiz_response.split("Answers:")[1] else: questions = quiz_response answers = "Answers section not found in the response." return questions, answers def main(): st.title("Question Generation Application") st.write("This app generates questions based on the uploaded document.") load_dotenv() genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) uploaded_file = st.file_uploader("Upload a PDF document", type=["pdf"]) if uploaded_file is not None: text = extract_text_from_pdf(uploaded_file) num_questions = st.number_input("Enter the number of questions", min_value=1, max_value=10, value=3) quiz_type = st.selectbox("Select the type of Question", ["multiple-choice", "true-false", "open-ended", "fill-in-the-blank", "mixed"]) expertise = st.text_input("Enter the domain of the questions to be generated.") if st.button("Generate Questions"): if quiz_type == "multiple-choice": prompt_template = create_multiple_choice_prompt(num_questions, text, expertise) elif quiz_type == "true-false": prompt_template = create_true_false_prompt(num_questions, text, expertise) elif quiz_type == "open-ended": prompt_template = create_open_ended_prompt(num_questions, text, expertise) elif quiz_type == "fill-in-the-blank": prompt_template = create_fill_in_the_blank_prompt(num_questions, text, expertise) else: # mixed prompt_template = create_mixed_questions_prompt(num_questions, text, expertise) quiz_response = get_gemini_response(text, prompt_template) questions, answers = split_questions_answers(quiz_response) st.session_state.answers = answers st.session_state.questions = questions st.write(questions) if st.button("Show Answers"): st.markdown(st.session_state.questions) st.write("----") st.markdown(st.session_state.answers) if __name__ == "__main__": main()