Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,6 @@ genai.configure(api_key=API_KEY)
|
|
| 10 |
# Initialize the Generative Model
|
| 11 |
model = genai.GenerativeModel('gemini-pro')
|
| 12 |
|
| 13 |
-
# Global variable to store the generated quiz
|
| 14 |
-
generated_quiz = ""
|
| 15 |
-
|
| 16 |
def extract_text_from_pdf(pdf_file):
|
| 17 |
"""Extract text from the given PDF file."""
|
| 18 |
try:
|
|
@@ -61,7 +58,7 @@ def answer_question_from_quiz(question):
|
|
| 61 |
try:
|
| 62 |
prompt = (f"Based on the following quiz, answer the user's question. The quiz is formatted with "
|
| 63 |
"questions followed by answers:\n\n"
|
| 64 |
-
f"{generated_quiz}\n\n"
|
| 65 |
f"User's question: {question}\n\n"
|
| 66 |
"Please provide a detailed answer based on the quiz content.")
|
| 67 |
response = model.generate_content(prompt)
|
|
@@ -72,15 +69,13 @@ def answer_question_from_quiz(question):
|
|
| 72 |
def process_pdf(pdf_file, difficulty):
|
| 73 |
"""Process the uploaded PDF to generate and explain quiz questions."""
|
| 74 |
try:
|
| 75 |
-
global generated_quiz
|
| 76 |
-
|
| 77 |
# Extract text from the PDF
|
| 78 |
extracted_text = extract_text_from_pdf(pdf_file)
|
| 79 |
|
| 80 |
# Generate a new quiz based on the extracted text
|
| 81 |
-
generated_quiz = generate_quiz(extracted_text, difficulty)
|
| 82 |
|
| 83 |
-
return generated_quiz
|
| 84 |
except Exception as e:
|
| 85 |
return f"Error processing PDF: {e}"
|
| 86 |
|
|
@@ -88,20 +83,22 @@ def main():
|
|
| 88 |
st.title("AI Quiz Generator")
|
| 89 |
st.write("Upload an exam or quiz PDF to generate a new quiz with the same style and complexity. You can also ask questions based on the generated quiz.")
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 92 |
difficulty = st.selectbox("Difficulty Level", ['easy', 'medium', 'hard'])
|
| 93 |
|
| 94 |
if st.button("Generate Quiz"):
|
| 95 |
if uploaded_file is not None:
|
| 96 |
-
|
| 97 |
-
generated_quiz = process_pdf(uploaded_file, difficulty)
|
| 98 |
|
| 99 |
st.subheader("Generated Quiz")
|
| 100 |
-
st.text(generated_quiz)
|
| 101 |
else:
|
| 102 |
st.error("Please upload a PDF file.")
|
| 103 |
|
| 104 |
-
if generated_quiz:
|
| 105 |
question = st.text_input("Ask a question about the quiz", "")
|
| 106 |
if st.button("Ask"):
|
| 107 |
if question:
|
|
|
|
| 10 |
# Initialize the Generative Model
|
| 11 |
model = genai.GenerativeModel('gemini-pro')
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def extract_text_from_pdf(pdf_file):
|
| 14 |
"""Extract text from the given PDF file."""
|
| 15 |
try:
|
|
|
|
| 58 |
try:
|
| 59 |
prompt = (f"Based on the following quiz, answer the user's question. The quiz is formatted with "
|
| 60 |
"questions followed by answers:\n\n"
|
| 61 |
+
f"{st.session_state.generated_quiz}\n\n"
|
| 62 |
f"User's question: {question}\n\n"
|
| 63 |
"Please provide a detailed answer based on the quiz content.")
|
| 64 |
response = model.generate_content(prompt)
|
|
|
|
| 69 |
def process_pdf(pdf_file, difficulty):
|
| 70 |
"""Process the uploaded PDF to generate and explain quiz questions."""
|
| 71 |
try:
|
|
|
|
|
|
|
| 72 |
# Extract text from the PDF
|
| 73 |
extracted_text = extract_text_from_pdf(pdf_file)
|
| 74 |
|
| 75 |
# Generate a new quiz based on the extracted text
|
| 76 |
+
st.session_state.generated_quiz = generate_quiz(extracted_text, difficulty)
|
| 77 |
|
| 78 |
+
return st.session_state.generated_quiz
|
| 79 |
except Exception as e:
|
| 80 |
return f"Error processing PDF: {e}"
|
| 81 |
|
|
|
|
| 83 |
st.title("AI Quiz Generator")
|
| 84 |
st.write("Upload an exam or quiz PDF to generate a new quiz with the same style and complexity. You can also ask questions based on the generated quiz.")
|
| 85 |
|
| 86 |
+
if 'generated_quiz' not in st.session_state:
|
| 87 |
+
st.session_state.generated_quiz = ""
|
| 88 |
+
|
| 89 |
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 90 |
difficulty = st.selectbox("Difficulty Level", ['easy', 'medium', 'hard'])
|
| 91 |
|
| 92 |
if st.button("Generate Quiz"):
|
| 93 |
if uploaded_file is not None:
|
| 94 |
+
st.session_state.generated_quiz = process_pdf(uploaded_file, difficulty)
|
|
|
|
| 95 |
|
| 96 |
st.subheader("Generated Quiz")
|
| 97 |
+
st.text(st.session_state.generated_quiz)
|
| 98 |
else:
|
| 99 |
st.error("Please upload a PDF file.")
|
| 100 |
|
| 101 |
+
if st.session_state.generated_quiz:
|
| 102 |
question = st.text_input("Ask a question about the quiz", "")
|
| 103 |
if st.button("Ask"):
|
| 104 |
if question:
|