pratikshahp commited on
Commit
ece53ba
·
verified ·
1 Parent(s): 8934212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -16,30 +16,43 @@ def extract_text_from_pdf(pdf_file):
16
  text += page.get_text()
17
  return text
18
 
 
 
 
 
 
 
19
  # Function to generate MCQs using the model
20
- def generate_mcqs(text, num_questions=5):
21
- if not text.strip():
22
  return ["No text extracted from the PDF. Unable to generate MCQs."]
23
 
24
- max_input_length = 512 - 100 # Reserve space for generated tokens
25
- inputs = tokenizer(text, return_tensors="pt", max_length=max_input_length, truncation=True)
26
-
27
  # Create the question generation pipeline
28
  generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
29
  mcqs = []
30
- for _ in range(num_questions):
31
- # Generate a single MCQ at a time
32
- input_text = f"Based on the following text, generate a multiple choice question:\n\n{text}\n\nQuestion:"
33
  generated = generator(input_text, max_length=400, num_return_sequences=1)
34
  question_text = generated[0]["generated_text"].split("Question:")[1].strip()
35
 
36
- # Format the MCQ
37
- options = ["Option A: Placeholder A", "Option B: Placeholder B", "Option C: Placeholder C", "Option D: Placeholder D"]
38
- correct_answer = "Option A: Placeholder A" # Placeholder correct answer for now
 
 
 
 
 
 
 
39
 
40
  mcq_formatted = f"Q: {question_text}\n{options[0]}\n{options[1]}\n{options[2]}\n{options[3]}\nCorrect Answer: {correct_answer}"
41
  mcqs.append(mcq_formatted)
42
 
 
 
 
43
  return mcqs
44
 
45
  # Streamlit app interface
@@ -55,7 +68,8 @@ if uploaded_file is not None:
55
 
56
  st.write("Generating MCQs...")
57
  num_questions = st.number_input("Number of MCQs to generate", min_value=1, max_value=20, value=5, step=1, format="%d")
58
- mcqs = generate_mcqs(text, num_questions)
 
59
 
60
  st.write("Generated MCQs:")
61
  for idx, mcq in enumerate(mcqs):
 
16
  text += page.get_text()
17
  return text
18
 
19
+ # Function to split text into chunks
20
+ def split_text(text, chunk_size=500):
21
+ words = text.split()
22
+ chunks = [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
23
+ return chunks
24
+
25
  # Function to generate MCQs using the model
26
+ def generate_mcqs(text_chunks, num_questions=5):
27
+ if not text_chunks:
28
  return ["No text extracted from the PDF. Unable to generate MCQs."]
29
 
 
 
 
30
  # Create the question generation pipeline
31
  generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
32
  mcqs = []
33
+
34
+ for chunk in text_chunks:
35
+ input_text = f"Generate a multiple-choice question from the following text:\n\n{chunk}\n\nQuestion:"
36
  generated = generator(input_text, max_length=400, num_return_sequences=1)
37
  question_text = generated[0]["generated_text"].split("Question:")[1].strip()
38
 
39
+ # Generate options for the question
40
+ options_text = f"Generate four plausible multiple-choice options for the following question:\n\n{question_text}\nOptions:"
41
+ options_generated = generator(options_text, max_length=200, num_return_sequences=1)
42
+ options_list = options_generated[0]["generated_text"].split("Options:")[1].strip().split("\n")
43
+ options = [f"Option {chr(65 + i)}: {option.strip()}" for i, option in enumerate(options_list[:4])]
44
+
45
+ if len(options) < 4:
46
+ continue
47
+
48
+ correct_answer = options[0] # Placeholder for correct answer identification logic
49
 
50
  mcq_formatted = f"Q: {question_text}\n{options[0]}\n{options[1]}\n{options[2]}\n{options[3]}\nCorrect Answer: {correct_answer}"
51
  mcqs.append(mcq_formatted)
52
 
53
+ if len(mcqs) >= num_questions:
54
+ break
55
+
56
  return mcqs
57
 
58
  # Streamlit app interface
 
68
 
69
  st.write("Generating MCQs...")
70
  num_questions = st.number_input("Number of MCQs to generate", min_value=1, max_value=20, value=5, step=1, format="%d")
71
+ text_chunks = split_text(text)
72
+ mcqs = generate_mcqs(text_chunks, num_questions)
73
 
74
  st.write("Generated MCQs:")
75
  for idx, mcq in enumerate(mcqs):