Spaces:
Sleeping
Sleeping
File size: 17,248 Bytes
7936e2f b69329e 7936e2f |
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
import os
import re
import PyPDF2
import docx
import googleapiclient.discovery
import nltk
from nltk.tokenize import sent_tokenize
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
from youtube_transcript_api import YouTubeTranscriptApi
import streamlit as st
import pandas as pd
import random
from io import StringIO
import logging
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Download necessary NLTK resources
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
class QuizGenerator:
def __init__(self):
# Initialize the summarizer and question generator models
self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Load question generation model
self.qg_model = AutoModelForSeq2SeqLM.from_pretrained("valhalla/t5-base-qg-hl")
self.qg_tokenizer = AutoTokenizer.from_pretrained("valhalla/t5-base-qg-hl")
# Initialize MCQ generation components
self.qa_model = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
def extract_text_from_pdf(self, pdf_file):
"""Extract text from a PDF file."""
try:
text = ""
pdf_reader = PyPDF2.PdfReader(pdf_file)
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
logger.error(f"Error extracting text from PDF: {e}")
return ""
def extract_text_from_docx(self, docx_file):
"""Extract text from a DOCX file."""
try:
doc = docx.Document(docx_file)
text = ""
for para in doc.paragraphs:
text += para.text + "\n"
return text
except Exception as e:
logger.error(f"Error extracting text from DOCX: {e}")
return ""
def extract_text_from_txt(self, txt_file):
"""Extract text from a TXT file."""
try:
return txt_file.read().decode('utf-8')
except Exception as e:
logger.error(f"Error extracting text from TXT: {e}")
return ""
def get_youtube_transcript(self, video_id):
"""Extract transcript from a YouTube video."""
try:
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
transcript = ' '.join([item['text'] for item in transcript_list])
return transcript
except Exception as e:
logger.error(f"Error getting YouTube transcript: {e}")
return ""
def summarize_text(self, text, max_length=1000):
"""Summarize long text to make processing more efficient."""
if len(text) <= max_length:
return text
chunks = self._split_text_into_chunks(text, max_length=3000)
summaries = []
for chunk in chunks:
if len(chunk) < 100: # Skip chunks that are too small
continue
summary = self.summarizer(chunk, max_length=300, min_length=100, do_sample=False)
summaries.append(summary[0]['summary_text'])
return " ".join(summaries)
def _split_text_into_chunks(self, text, max_length=3000):
"""Split text into chunks of max_length characters."""
sentences = sent_tokenize(text)
chunks = []
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) <= max_length:
current_chunk += " " + sentence
else:
chunks.append(current_chunk.strip())
current_chunk = sentence
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def generate_questions(self, text, num_questions=5):
"""Generate questions based on the input text."""
try:
# Summarize text if it's too long
processed_text = self.summarize_text(text)
# Split into sentences
sentences = sent_tokenize(processed_text)
questions = []
random.shuffle(sentences) # Randomize to get different questions each time
for sentence in sentences[:min(num_questions * 3, len(sentences))]: # Process more sentences than needed
if len(sentence.split()) < 5: # Skip short sentences
continue
# Format for the question generation model
input_text = f"generate question: {sentence}"
# Generate question
inputs = self.qg_tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = self.qg_model.generate(inputs, max_length=64, num_beams=4, early_stopping=True)
question = self.qg_tokenizer.decode(outputs[0], skip_special_tokens=True)
# Use QA model to get answer
qa_input = {
'question': question,
'context': processed_text
}
answer = self.qa_model(qa_input)
if answer['score'] > 0.1: # Only keep questions with reasonable confidence
questions.append({
'question': question,
'answer': answer['answer'],
'context': sentence
})
if len(questions) >= num_questions:
break
return questions
except Exception as e:
logger.error(f"Error generating questions: {e}")
return []
def generate_mcq(self, questions, num_options=4):
"""Convert open-ended questions to multiple-choice questions."""
mcqs = []
for q in questions:
correct_answer = q['answer']
# Generate distractors (incorrect options)
distractors = self._generate_distractors(q['context'], correct_answer, num_options-1)
# Create options list with correct answer
options = distractors + [correct_answer]
random.shuffle(options)
# Find position of correct answer
correct_index = options.index(correct_answer)
mcqs.append({
'question': q['question'],
'options': options,
'correct_answer': correct_answer,
'correct_index': correct_index
})
return mcqs
def _generate_distractors(self, context, correct_answer, num_distractors=3):
"""Generate plausible but incorrect answers."""
# Simple approach - extract other nouns from the text
words = nltk.word_tokenize(context)
pos_tags = nltk.pos_tag(words)
# Extract nouns and named entities
nouns = [word for word, pos in pos_tags if pos in ('NN', 'NNS', 'NNP', 'NNPS') and word.lower() != correct_answer.lower()]
# Deduplicate and filter
unique_nouns = list(set(nouns))
distractors = [noun for noun in unique_nouns if len(noun) > 2]
# If we don't have enough distractors, add some generic ones
generic_distractors = ["None of the above", "Cannot be determined", "All of the above"]
# Combine and return required number
combined = list(distractors) + generic_distractors
random.shuffle(combined)
return combined[:num_distractors]
def generate_true_false(self, text, num_questions=5):
"""Generate true/false questions from text."""
try:
# Generate factual statements first
questions = self.generate_questions(text, num_questions)
true_false = []
for q in questions:
# Original statement is true
true_statement = {
'statement': q['context'],
'is_true': True
}
# Create a false version by negating or changing key parts
words = q['context'].split()
if len(words) > 4:
# Simple modification: replace a word or add a negation
change_idx = random.randint(0, len(words)-1)
words[change_idx] = random.choice(["not", "never", "rarely", "incorrectly"]) + " " + words[change_idx]
false_statement = {
'statement': " ".join(words),
'is_true': False
}
true_false.extend([true_statement, false_statement])
# Shuffle and return required number
random.shuffle(true_false)
return true_false[:num_questions]
except Exception as e:
logger.error(f"Error generating true/false questions: {e}")
return []
def create_streamlit_app():
st.set_page_config(page_title="QuizWhiz", page_icon="📚", layout="wide")
st.title("QuizWhiz - Comprehensive Quiz Generator")
st.subheader("Generate quizzes from various sources: text, documents, and YouTube videos")
quiz_gen = QuizGenerator()
# Sidebar for options
st.sidebar.header("Quiz Options")
quiz_type = st.sidebar.selectbox(
"Question Type",
["Multiple Choice", "True/False", "Open-Ended"]
)
num_questions = st.sidebar.slider("Number of Questions", 3, 20, 5)
# Source selection
st.header("Select Your Content Source")
source_type = st.radio(
"Content Source",
["Text Input", "Document Upload", "YouTube Video", "Topic/Subject"]
)
text_content = ""
# Handle different source types
if source_type == "Text Input":
text_content = st.text_area("Enter your text content here:", height=250)
elif source_type == "Document Upload":
uploaded_file = st.file_uploader("Upload your document", type=['pdf', 'docx', 'txt'])
if uploaded_file is not None:
st.success(f"File '{uploaded_file.name}' uploaded successfully!")
# Extract text based on file type
if uploaded_file.name.endswith('.pdf'):
text_content = quiz_gen.extract_text_from_pdf(uploaded_file)
elif uploaded_file.name.endswith('.docx'):
text_content = quiz_gen.extract_text_from_docx(uploaded_file)
elif uploaded_file.name.endswith('.txt'):
text_content = quiz_gen.extract_text_from_txt(uploaded_file)
# Show text preview
if text_content:
with st.expander("Preview Extracted Text"):
st.text(text_content[:500] + "..." if len(text_content) > 500 else text_content)
else:
st.error("Failed to extract text from the document.")
elif source_type == "YouTube Video":
youtube_url = st.text_input("Enter YouTube Video URL:")
if youtube_url:
# Extract video ID from URL
video_id_match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', youtube_url)
if video_id_match:
video_id = video_id_match.group(1)
# Show video embed
st.video(youtube_url)
# Extract transcript
with st.spinner("Extracting video transcript..."):
text_content = quiz_gen.get_youtube_transcript(video_id)
if text_content:
with st.expander("Preview Transcript"):
st.text(text_content[:500] + "..." if len(text_content) > 500 else text_content)
else:
st.error("Failed to extract transcript. This video might not have captions.")
else:
st.error("Invalid YouTube URL. Please enter a valid URL.")
elif source_type == "Topic/Subject":
topic = st.text_input("Enter a topic or subject:")
if topic:
# For this demo, we'll use a predefined text about the topic
# In a real app, you might use an API to fetch content about the topic
st.info(f"Generating quiz about: {topic}")
text_content = f"The topic of {topic} is a fascinating subject to explore. " \
f"There are many important concepts and facts related to {topic} " \
f"that make it an essential area of study. Understanding {topic} " \
f"requires careful consideration of its key principles."
# Placeholder for a real implementation that would gather information about the topic
st.warning("In a complete implementation, this would gather information about the topic from reliable sources.")
# Generate Quiz Button
if text_content:
if st.button("Generate Quiz"):
with st.spinner("Generating quiz questions..."):
if quiz_type == "Multiple Choice":
# Generate questions first
questions = quiz_gen.generate_questions(text_content, num_questions)
# Convert to MCQs
mcqs = quiz_gen.generate_mcq(questions)
if mcqs:
st.success(f"Generated {len(mcqs)} multiple choice questions!")
# Display questions
for i, q in enumerate(mcqs, 1):
st.subheader(f"Question {i}: {q['question']}")
# Display options
option_letters = ['A', 'B', 'C', 'D']
for j, option in enumerate(q['options']):
st.write(f"{option_letters[j]}. {option}")
# Reveal answer in expander
with st.expander("Reveal Answer"):
st.write(f"Correct Answer: {option_letters[q['correct_index']]}. {q['correct_answer']}")
st.divider()
else:
st.error("Failed to generate questions. Try with different content.")
elif quiz_type == "True/False":
tf_questions = quiz_gen.generate_true_false(text_content, num_questions)
if tf_questions:
st.success(f"Generated {len(tf_questions)} true/false questions!")
# Display questions
for i, q in enumerate(tf_questions, 1):
st.subheader(f"Question {i}: True or False?")
st.write(q['statement'])
# Reveal answer
with st.expander("Reveal Answer"):
st.write(f"Answer: {'True' if q['is_true'] else 'False'}")
st.divider()
else:
st.error("Failed to generate true/false questions. Try with different content.")
elif quiz_type == "Open-Ended":
questions = quiz_gen.generate_questions(text_content, num_questions)
if questions:
st.success(f"Generated {len(questions)} open-ended questions!")
# Display questions
for i, q in enumerate(questions, 1):
st.subheader(f"Question {i}: {q['question']}")
# Reveal answer
with st.expander("Reveal Answer"):
st.write(f"Suggested Answer: {q['answer']}")
st.write(f"Context: {q['context']}")
st.divider()
else:
st.error("Failed to generate questions. Try with different content.")
else:
st.info("Please provide content or select a source to generate a quiz.")
# Footer
st.sidebar.divider()
st.sidebar.caption("QuizWhiz - Powered by AI")
st.sidebar.caption("© 2025 QuizWhiz Technologies")
if __name__ == "__main__":
create_streamlit_app() |