Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,35 +47,17 @@ def chunk_text(text, chunk_size=800):
|
|
| 47 |
|
| 48 |
return chunks
|
| 49 |
|
| 50 |
-
def format_quiz_output(raw_output):
|
| 51 |
-
"""Format the quiz output for better readability"""
|
| 52 |
-
# Split by question patterns
|
| 53 |
-
questions = re.split(r'\n(?=\d+\.|\d+\))', raw_output)
|
| 54 |
-
formatted_quiz = ""
|
| 55 |
-
|
| 56 |
-
for i, question in enumerate(questions, 1):
|
| 57 |
-
if question.strip():
|
| 58 |
-
# Clean up the question
|
| 59 |
-
question = question.strip()
|
| 60 |
-
if not question.startswith(f"{i}."):
|
| 61 |
-
question = f"{i}. {question}"
|
| 62 |
-
|
| 63 |
-
formatted_quiz += f"\n{question}\n"
|
| 64 |
-
formatted_quiz += "-" * 50 + "\n"
|
| 65 |
-
|
| 66 |
-
return formatted_quiz
|
| 67 |
-
|
| 68 |
def generate_quiz(pdf_bytes, num_questions=10):
|
| 69 |
"""Generate quiz from PDF"""
|
| 70 |
if pdf_bytes is None:
|
| 71 |
-
return "
|
| 72 |
|
| 73 |
try:
|
| 74 |
# Extract text from PDF
|
| 75 |
text = extract_text_from_pdf(pdf_bytes)
|
| 76 |
|
| 77 |
if not text.strip():
|
| 78 |
-
return "
|
| 79 |
|
| 80 |
# Clean the text
|
| 81 |
text = re.sub(r'\s+', ' ', text).strip()
|
|
@@ -122,18 +104,79 @@ Text: {chunk}"""
|
|
| 122 |
continue
|
| 123 |
|
| 124 |
if not all_questions:
|
| 125 |
-
return "
|
| 126 |
|
| 127 |
# Combine all questions
|
| 128 |
-
final_quiz = "\n".join(all_questions)
|
| 129 |
|
| 130 |
# Format the output
|
| 131 |
-
formatted_quiz = f"""
|
| 132 |
-
|
| 133 |
-
{'='*60}
|
| 134 |
|
| 135 |
{final_quiz}
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
return chunks
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def generate_quiz(pdf_bytes, num_questions=10):
|
| 51 |
"""Generate quiz from PDF"""
|
| 52 |
if pdf_bytes is None:
|
| 53 |
+
return "Error: Please upload a PDF file first."
|
| 54 |
|
| 55 |
try:
|
| 56 |
# Extract text from PDF
|
| 57 |
text = extract_text_from_pdf(pdf_bytes)
|
| 58 |
|
| 59 |
if not text.strip():
|
| 60 |
+
return "Error: No text found in the PDF. Please check if the PDF contains readable text."
|
| 61 |
|
| 62 |
# Clean the text
|
| 63 |
text = re.sub(r'\s+', ' ', text).strip()
|
|
|
|
| 104 |
continue
|
| 105 |
|
| 106 |
if not all_questions:
|
| 107 |
+
return "Error: Failed to generate questions. The text might be too complex or the model encountered an error."
|
| 108 |
|
| 109 |
# Combine all questions
|
| 110 |
+
final_quiz = "\n\n".join(all_questions)
|
| 111 |
|
| 112 |
# Format the output
|
| 113 |
+
formatted_quiz = f"""QUIZ GENERATED FROM YOUR PDF
|
| 114 |
+
============================================================
|
|
|
|
| 115 |
|
| 116 |
{final_quiz}
|
| 117 |
|
| 118 |
+
============================================================
|
| 119 |
+
Total Questions Generated: {len(all_questions)} sections
|
| 120 |
+
Note: Review each question carefully and verify the answers.
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
return formatted_quiz
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return f"Error processing PDF: {str(e)}\n\nPlease make sure you uploaded a valid PDF file."
|
| 127 |
+
|
| 128 |
+
# Create Gradio interface
|
| 129 |
+
def create_interface():
|
| 130 |
+
with gr.Blocks(title="PDF Quiz Generator") as interface:
|
| 131 |
+
gr.Markdown("""
|
| 132 |
+
# PDF to Quiz Generator
|
| 133 |
+
Upload a PDF chapter and generate multiple choice questions with answers and explanations.
|
| 134 |
+
""")
|
| 135 |
+
|
| 136 |
+
with gr.Row():
|
| 137 |
+
with gr.Column():
|
| 138 |
+
pdf_input = gr.File(
|
| 139 |
+
label="Upload PDF Chapter",
|
| 140 |
+
file_types=[".pdf"],
|
| 141 |
+
type="binary"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
num_questions = gr.Slider(
|
| 145 |
+
minimum=5,
|
| 146 |
+
maximum=15,
|
| 147 |
+
value=10,
|
| 148 |
+
step=1,
|
| 149 |
+
label="Number of Questions to Generate"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
generate_btn = gr.Button("Generate Quiz", variant="primary")
|
| 153 |
+
|
| 154 |
+
with gr.Column():
|
| 155 |
+
output = gr.Textbox(
|
| 156 |
+
label="Generated Quiz",
|
| 157 |
+
lines=20,
|
| 158 |
+
max_lines=30,
|
| 159 |
+
show_copy_button=True
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
generate_btn.click(
|
| 163 |
+
fn=generate_quiz,
|
| 164 |
+
inputs=[pdf_input, num_questions],
|
| 165 |
+
outputs=output
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
gr.Markdown("""
|
| 169 |
+
### Instructions:
|
| 170 |
+
1. Upload a PDF file containing the chapter or text you want to create a quiz from
|
| 171 |
+
2. Select the number of questions you want (5-15)
|
| 172 |
+
3. Click "Generate Quiz" and wait for the results
|
| 173 |
+
4. Copy the generated quiz for your use
|
| 174 |
+
|
| 175 |
+
**Note:** The quality of questions depends on the clarity and content of your PDF text.
|
| 176 |
+
""")
|
| 177 |
+
|
| 178 |
+
return interface
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
interface = create_interface()
|
| 182 |
+
interface.launch(debug=True, share=False)
|