Samay42 commited on
Commit
907735b
·
verified ·
1 Parent(s): 124bad2

Upload quizgemini.py

Browse files
Files changed (1) hide show
  1. quizgemini.py +238 -0
quizgemini.py ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from docx import Document
5
+ import fitz # PyMuPDF
6
+ import google.generativeai as genai
7
+
8
+ def extract_text_from_pdf_or_docx(file):
9
+ """Extract text from PDF or Word document."""
10
+ filename = file.name
11
+ text = ""
12
+ if filename.endswith('.pdf'):
13
+ # Extract text from PDF
14
+ with fitz.open(file) as doc:
15
+ for page in doc:
16
+ text += page.get_text()
17
+ elif filename.endswith('.docx'):
18
+ # Extract text from Word document
19
+ doc = Document(file)
20
+ for paragraph in doc.paragraphs:
21
+ text += paragraph.text + "\n"
22
+ else:
23
+ text = "Unsupported file format. Please upload a PDF or Word document."
24
+ return text
25
+
26
+ def create_multiple_choice_prompt(num_questions, quiz_context, expertise):
27
+ """Create the prompt template for multiple-choice quiz."""
28
+ template = f"""
29
+ 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}.
30
+
31
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
32
+
33
+ The format of the quiz is as follows:
34
+ - Multiple-choice:
35
+ - Questions:
36
+ 1. <Question1>:
37
+ a. Answer 1
38
+ b. Answer 2
39
+ c. Answer 3
40
+ d. Answer 4
41
+
42
+ 2. <Question2>:
43
+ a. Answer 1
44
+ b. Answer 2
45
+ c. Answer 3
46
+ d. Answer 4
47
+ ....
48
+ - Answers:
49
+ 1. <a|b|c|d>
50
+ 2. <a|b|c|d>
51
+ ....
52
+ Example:
53
+ - Questions:
54
+ 1. What is the time complexity of a binary search tree?
55
+ a. O(n)
56
+ b. O(log n)
57
+ c. O(n^2)
58
+ d. O(1)
59
+ - Answers:
60
+ 1. b
61
+ """
62
+ return template
63
+
64
+ def create_true_false_prompt(num_questions, quiz_context, expertise):
65
+ """Create the prompt template for true-false quiz."""
66
+ template = f"""
67
+ 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}.
68
+
69
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
70
+
71
+ The format of the quiz is as follows:
72
+ - True-false:
73
+ - Questions:
74
+ 1. <Question1>: <True|False>
75
+ 2. <Question2>: <True|False>
76
+ .....
77
+ - Answers:
78
+ 1. <True|False>
79
+ 2. <True|False>
80
+ .....
81
+ Example:
82
+ - Questions:
83
+ 1. A binary search tree is a type of data structure.
84
+ 2. Binary search trees are typically used for sorting and searching operations.
85
+ - Answers:
86
+ 1. True
87
+ 2. True
88
+ """
89
+ return template
90
+
91
+ def create_open_ended_prompt(num_questions, quiz_context, expertise):
92
+ """Create the prompt template for open-ended quiz."""
93
+ template = f"""
94
+ 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}.
95
+
96
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
97
+
98
+ The format of the quiz is as follows:
99
+ - Open-ended:
100
+ - Questions:
101
+ 1. <Question1>
102
+ 2. <Question2>
103
+ ....
104
+ Example:
105
+ - Questions:
106
+ 1. What is a binary search tree?
107
+ 2. How are binary search trees implemented?
108
+ """
109
+ return template
110
+
111
+ def create_fill_in_the_blank_prompt(num_questions, quiz_context, expertise):
112
+ """Create the prompt template for fill-in-the-blank quiz."""
113
+ template = f"""
114
+ 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}.
115
+
116
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
117
+
118
+ The format of the quiz is as follows:
119
+ - Fill-in-the-blank:
120
+ - Questions:
121
+ 1. <Question1>: <Fill-in-the-blank>
122
+ 2. <Question2>: <Fill-in-the-blank>
123
+ ....
124
+ Example:
125
+ - Questions:
126
+ 1. A binary search tree is a ________ data structure.
127
+ 2. Binary search trees are implemented using ________.
128
+ - Answers:
129
+ 1. hierarchical
130
+ 2. linked lists
131
+ """
132
+ return template
133
+
134
+ def create_mixed_questions_prompt(num_questions, quiz_context, expertise):
135
+ """Create the prompt template for a mix of all question types."""
136
+ template = f"""
137
+ 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}.
138
+
139
+ 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.
140
+
141
+ The format of the quiz is as follows:
142
+ - Mixed Questions:
143
+ - Questions:
144
+ 1. <Question1> (Question type):
145
+ <Answers if applicable>
146
+
147
+ 2. <Question2> (Question type):
148
+ <Answers if applicable>
149
+
150
+ 3. <Question3> (Question type):
151
+ <Answers if applicable>
152
+
153
+ ...
154
+ {num_questions}. <Question{num_questions}> (Question type):
155
+ <Answers if applicable>
156
+
157
+ Example:
158
+ - Questions:
159
+ 1. What is the time complexity of a binary search tree? (Multiple-choice)
160
+ a. O(n)
161
+ b. O(log n)
162
+ c. O(n^2)
163
+ d. O(1)
164
+ 2. A binary search tree is a type of data structure. (True/False)
165
+ 3. What is a binary search tree? (Open-ended)
166
+ 4. A binary search tree is a ________ data structure. (Fill-in-the-blank)
167
+ 5. Another sample question. (Multiple-choice)
168
+ a. Sample 1
169
+ b. Sample 2
170
+ c. Sample 3
171
+ d. Sample 4
172
+
173
+ - Answers:
174
+ 1. b
175
+ 2. True
176
+ 3. A binary search tree is a data structure used to store data in a sorted manner.
177
+ 4. hierarchical
178
+ 5. b
179
+
180
+ 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.
181
+ """
182
+ return template
183
+
184
+
185
+ def get_gemini_response(question, prompt):
186
+ """Function to load Google Gemini model and provide queries as response."""
187
+ model = genai.GenerativeModel('gemini-1.5-flash')
188
+ response = model.generate_content([prompt, question])
189
+ return response.text
190
+
191
+ def split_questions_answers(quiz_response):
192
+ """Function that splits the questions and answers from the quiz response."""
193
+ if "Answers:" in quiz_response:
194
+ questions = quiz_response.split("Answers:")[0]
195
+ answers = quiz_response.split("Answers:")[1]
196
+ else:
197
+ questions = quiz_response
198
+ answers = "Answers section not found in the response."
199
+ return questions, answers
200
+
201
+ def main():
202
+ st.title("Question Generation Application")
203
+ st.write("This app generates questions based on the uploaded document.")
204
+ load_dotenv()
205
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
206
+
207
+ uploaded_file = st.file_uploader("Upload a PDF or Word document")
208
+ if uploaded_file is not None:
209
+ text = extract_text_from_pdf_or_docx(uploaded_file)
210
+ num_questions = st.number_input("Enter the number of questions", min_value=1, max_value=10, value=3)
211
+ quiz_type = st.selectbox("Select the type of Question", ["multiple-choice", "true-false", "open-ended", "fill-in-the-blank", "mixed"])
212
+ expertise = st.text_input("Enter the domain of the questions to be generated.")
213
+
214
+ if st.button("Generate Questions"):
215
+ if quiz_type == "multiple-choice":
216
+ prompt_template = create_multiple_choice_prompt(num_questions, text, expertise)
217
+ elif quiz_type == "true-false":
218
+ prompt_template = create_true_false_prompt(num_questions, text, expertise)
219
+ elif quiz_type == "open-ended":
220
+ prompt_template = create_open_ended_prompt(num_questions, text, expertise)
221
+ elif quiz_type == "fill-in-the-blank":
222
+ prompt_template = create_fill_in_the_blank_prompt(num_questions, text, expertise)
223
+ else: # mixed
224
+ prompt_template = create_mixed_questions_prompt(num_questions, text, expertise)
225
+
226
+ quiz_response = get_gemini_response(text, prompt_template)
227
+ questions, answers = split_questions_answers(quiz_response)
228
+ st.session_state.answers = answers
229
+ st.session_state.questions = questions
230
+ st.write(questions)
231
+
232
+ if st.button("Show Answers"):
233
+ st.markdown(st.session_state.questions)
234
+ st.write("----")
235
+ st.markdown(st.session_state.answers)
236
+
237
+ if __name__ == "__main__":
238
+ main()