Samay42 commited on
Commit
e16a738
·
verified ·
1 Parent(s): 3d1b4ed

Upload quizapp.py

Browse files
Files changed (1) hide show
  1. quizapp.py +240 -0
quizapp.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from langchain.prompts import ChatPromptTemplate
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain.schema import StrOutputParser
7
+ from docx import Document
8
+ import fitz # PyMuPDF
9
+
10
+ def extract_text_from_pdf_or_docx(file):
11
+ """Extract text from PDF or Word document."""
12
+ filename = file.name
13
+ text = ""
14
+ if filename.endswith('.pdf'):
15
+ # Extract text from PDF
16
+ with fitz.open(file) as doc:
17
+ for page in doc:
18
+ text += page.get_text()
19
+ elif filename.endswith('.docx'):
20
+ # Extract text from Word document
21
+ doc = Document(file)
22
+ for paragraph in doc.paragraphs:
23
+ text += paragraph.text + "\n"
24
+ else:
25
+ text = "Unsupported file format. Please upload a PDF or Word document."
26
+ return text
27
+
28
+ def create_multiple_choice_prompt(num_questions, quiz_context, expertise):
29
+ """Create the prompt template for multiple-choice quiz."""
30
+ template = f"""
31
+ 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}.
32
+
33
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
34
+
35
+ The format of the quiz is as follows:
36
+ - Multiple-choice:
37
+ - Questions:
38
+ 1. <Question1>:
39
+ a. Answer 1
40
+ b. Answer 2
41
+ c. Answer 3
42
+ d. Answer 4
43
+
44
+ 2. <Question2>:
45
+ a. Answer 1
46
+ b. Answer 2
47
+ c. Answer 3
48
+ d. Answer 4
49
+ ....
50
+ - Answers:
51
+ 1. <a|b|c|d>
52
+ 2. <a|b|c|d>
53
+ ....
54
+ Example:
55
+ - Questions:
56
+ 1. What is the time complexity of a binary search tree?
57
+ a. O(n)
58
+ b. O(log n)
59
+ c. O(n^2)
60
+ d. O(1)
61
+ - Answers:
62
+ 1. b
63
+ """
64
+ return template
65
+
66
+ def create_true_false_prompt(num_questions, quiz_context, expertise):
67
+ """Create the prompt template for true-false quiz."""
68
+ template = f"""
69
+ 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}.
70
+
71
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
72
+
73
+ The format of the quiz is as follows:
74
+ - True-false:
75
+ - Questions:
76
+ 1. <Question1>: <True|False>
77
+ 2. <Question2>: <True|False>
78
+ .....
79
+ - Answers:
80
+ 1. <True|False>
81
+ 2. <True|False>
82
+ .....
83
+ Example:
84
+ - Questions:
85
+ 1. A binary search tree is a type of data structure.
86
+ 2. Binary search trees are typically used for sorting and searching operations.
87
+ - Answers:
88
+ 1. True
89
+ 2. True
90
+ """
91
+ return template
92
+
93
+ def create_open_ended_prompt(num_questions, quiz_context, expertise):
94
+ """Create the prompt template for open-ended quiz."""
95
+ template = f"""
96
+ 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}.
97
+
98
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
99
+
100
+ The format of the quiz is as follows:
101
+ - Open-ended:
102
+ - Questions:
103
+ 1. <Question1>
104
+ 2. <Question2>
105
+ ....
106
+ Example:
107
+ - Questions:
108
+ 1. What is a binary search tree?
109
+ 2. How are binary search trees implemented?
110
+ """
111
+ return template
112
+
113
+ def create_fill_in_the_blank_prompt(num_questions, quiz_context, expertise):
114
+ """Create the prompt template for fill-in-the-blank quiz."""
115
+ template = f"""
116
+ 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}.
117
+
118
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
119
+
120
+ The format of the quiz is as follows:
121
+ - Fill-in-the-blank:
122
+ - Questions:
123
+ 1. <Question1>: <Fill-in-the-blank>
124
+ 2. <Question2>: <Fill-in-the-blank>
125
+ ....
126
+ Example:
127
+ - Questions:
128
+ 1. A binary search tree is a ________ data structure.
129
+ 2. Binary search trees are implemented using ________.
130
+ - Answers:
131
+ 1. hierarchical
132
+ 2. linked lists
133
+ """
134
+ return template
135
+
136
+ def create_mixed_questions_prompt(num_questions, quiz_context, expertise):
137
+ """Create the prompt template for a mix of all question types."""
138
+ template = f"""
139
+ You are an expert in {expertise}. Generate a quiz with {num_questions} questions that include a mix of multiple-choice, true-false, open-ended, and fill-in-the-blank questions relevant to {expertise} based on the following content: {quiz_context}.
140
+
141
+ The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
142
+
143
+ The format of the quiz is as follows:
144
+ - Mixed Questions:
145
+ - Questions:
146
+ 1. <Question1> (Multiple-choice):
147
+ a. Answer 1
148
+ b. Answer 2
149
+ c. Answer 3
150
+ d. Answer 4
151
+
152
+ 2. <Question2> (True/False):
153
+ <Question2>: <True|False>
154
+
155
+ 3. <Question3> (Open-ended):
156
+ <Question3>
157
+
158
+ 4. <Question4> (Fill-in-the-blank):
159
+ <Question4>: <Fill-in-the-blank>
160
+ ....
161
+ - Answers:
162
+ 1. <a|b|c|d>
163
+ 2. <True|False>
164
+ 3. <Open-ended Answer>
165
+ 4. <Fill-in-the-blank Answer>
166
+ ....
167
+ Example:
168
+ - Questions:
169
+ 1. What is the time complexity of a binary search tree? (Multiple-choice)
170
+ a. O(n)
171
+ b. O(log n)
172
+ c. O(n^2)
173
+ d. O(1)
174
+ 2. A binary search tree is a type of data structure. (True/False)
175
+ True
176
+ 3. What is a binary search tree? (Open-ended)
177
+ 4. A binary search tree is a ________ data structure. (Fill-in-the-blank)
178
+ - Answers:
179
+ 1. b
180
+ 2. True
181
+ 3. A binary search tree is a data structure used to store data in a sorted manner.
182
+ 4. hierarchical
183
+ """
184
+ return template
185
+
186
+ def create_quiz_chain(openai_api_key):
187
+ """Creates the chain for the quiz app."""
188
+ llm = ChatOpenAI(temperature=0.0, openai_api_key=openai_api_key)
189
+ return llm | StrOutputParser()
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("QuesPro")
203
+ st.write("This app generates questions based on the uploaded document.")
204
+ load_dotenv()
205
+ openai_api_key = os.getenv("OPENAI_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 quiz type", ["multiple-choice", "true-false", "open-ended", "fill-in-the-blank", "mixed"])
212
+ expertise = st.text_input("Enter the field of expertise for the quiz")
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
+ chain = create_quiz_chain(openai_api_key)
227
+ quiz_response = chain.invoke(prompt_template)
228
+ st.write("Quiz Generated!")
229
+ questions, answers = split_questions_answers(quiz_response)
230
+ st.session_state.answers = answers
231
+ st.session_state.questions = questions
232
+ st.write(questions)
233
+
234
+ if st.button("Show Answers"):
235
+ st.markdown(st.session_state.questions)
236
+ st.write("----")
237
+ st.markdown(st.session_state.answers)
238
+
239
+ if __name__ == "__main__":
240
+ main()