Spaces:
Sleeping
Sleeping
File size: 8,403 Bytes
e16a738 |
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
import os
import streamlit as st
from dotenv import load_dotenv
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain.schema import StrOutputParser
from docx import Document
import fitz # PyMuPDF
def extract_text_from_pdf_or_docx(file):
"""Extract text from PDF or Word document."""
filename = file.name
text = ""
if filename.endswith('.pdf'):
# Extract text from PDF
with fitz.open(file) as doc:
for page in doc:
text += page.get_text()
elif filename.endswith('.docx'):
# Extract text from Word document
doc = Document(file)
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
else:
text = "Unsupported file format. Please upload a PDF or Word document."
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.
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.
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.
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.
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 {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}.
The questions should be at the level of {expertise} and should challenge the knowledge of someone proficient in this field.
The format of the quiz is as follows:
- Mixed Questions:
- Questions:
1. <Question1> (Multiple-choice):
a. Answer 1
b. Answer 2
c. Answer 3
d. Answer 4
2. <Question2> (True/False):
<Question2>: <True|False>
3. <Question3> (Open-ended):
<Question3>
4. <Question4> (Fill-in-the-blank):
<Question4>: <Fill-in-the-blank>
....
- Answers:
1. <a|b|c|d>
2. <True|False>
3. <Open-ended Answer>
4. <Fill-in-the-blank Answer>
....
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)
True
3. What is a binary search tree? (Open-ended)
4. A binary search tree is a ________ data structure. (Fill-in-the-blank)
- Answers:
1. b
2. True
3. A binary search tree is a data structure used to store data in a sorted manner.
4. hierarchical
"""
return template
def create_quiz_chain(openai_api_key):
"""Creates the chain for the quiz app."""
llm = ChatOpenAI(temperature=0.0, openai_api_key=openai_api_key)
return llm | StrOutputParser()
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("QuesPro")
st.write("This app generates questions based on the uploaded document.")
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
uploaded_file = st.file_uploader("Upload a PDF or Word document")
if uploaded_file is not None:
text = extract_text_from_pdf_or_docx(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 quiz type", ["multiple-choice", "true-false", "open-ended", "fill-in-the-blank", "mixed"])
expertise = st.text_input("Enter the field of expertise for the quiz")
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)
chain = create_quiz_chain(openai_api_key)
quiz_response = chain.invoke(prompt_template)
st.write("Quiz Generated!")
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()
|