File size: 8,864 Bytes
907735b 04af361 907735b 18ed441 907735b 18ed441 907735b b0cfc76 907735b b0cfc76 907735b 3376ec5 907735b b0cfc76 907735b b0cfc76 907735b 18ed441 907735b 18ed441 b0cfc76 907735b 18ed441 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
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. <Question1>:
a. Answer 1
b. Answer 2
c. Answer 3
d. Answer 4
2. <Question2>:
a. Answer 1
b. Answer 2
c. Answer 3
d. Answer 4
....
- Answers:
1. <a|b|c|d>
2. <a|b|c|d>
....
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. <Question1>: <True|False>
2. <Question2>: <True|False>
.....
- Answers:
1. <True|False>
2. <True|False>
.....
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. <Question1>
2. <Question2>
....
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. <Question1>: <Fill-in-the-blank>
2. <Question2>: <Fill-in-the-blank>
....
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. <Question1> (Question type):
<Answers if applicable>
2. <Question2> (Question type):
<Answers if applicable>
3. <Question3> (Question type):
<Answers if applicable>
...
{num_questions}. <Question{num_questions}> (Question type):
<Answers if applicable>
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() |