modify the below code completely by this below-given project codes should use LLMs to perform. I have my own API key so modify it accordingly:
import PyPDF2
import random
import os
def get_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
code
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
questions = get_mca_questions(context)
for i, question in enumerate(questions):
print(f"Question {i+1}: {question}")
import PyPDF2
import random
import os
def get_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
code
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
To modify the code to use LLMs, you can replace the get_text_from_pdf function with a function that uses an LLM to extract text from the PDF file. Here's an example of how you can modify the code:
code
import PyPDF2
import random
import os
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments
def get_text_from_pdf(file_path):
tokenizer = AutoTokenizer.from_pretrained('github-ubert-base-uncased-whole-word-masking-finetuned-squad')
model = AutoModelForSequenceClassification.from_pretrained('github-ubert-base-uncased-whole-word-masking-finetuned-squad')
text = ''
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
for page in reader.pages:
text += tokenizer.encode(page.extract_text(), return_tensors='pt')
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
file_path = 'chapter-2.pdf'
context = get_text_from_pdf(file_path)
questions = get_mca_questions(context)
for i, question in enumerate(questions):
print
User
modify the below code completely by this below-given project codes should use LLMs to perform. I have my own API key so modify it accordingly:
import PyPDF2
import random
import os
def get_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
code
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
questions = get_mca_questions(context)
for i, question in enumerate(questions):
print(f"Question {i+1}: {question}")
import PyPDF2
import random
import os
def get_text_from_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
code
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
Assistant
To modify the code to use LLMs, you can replace the get_text_from_pdf function with a function that uses an LLM to extract text from the PDF file. Here's an example of how you can modify the code:
code
import PyPDF2
import random
import os
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments
def get_text_from_pdf(file_path):
tokenizer = AutoTokenizer.from_pretrained('github-ubert-base-uncased-whole-word-masking-finetuned-squad')
model = AutoModelForSequenceClassification.from_pretrained('github-ubert-base-uncased-whole-word-masking-finetuned-squad')
text = ''
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
for page in reader.pages:
text += tokenizer.encode(page.extract_text(), return_tensors='pt')
return text
def get_mca_questions(context):
# Split the context into sentences
sentences = context.strip().split('. ')
# Generate multiple-choice questions
mca_questions = []
for sentence in sentences:
question = "Which of the following is true about " + sentence + "?"
choices = random.sample(sentences, k=3) # Randomly select 3 choices
choices.append(sentence) # Add the correct answer to the choices
random.shuffle(choices) # Shuffle the choices
mca_question = question + "\n"
for i, choice in enumerate(choices):
mca_question += chr(ord('a') + i) + ") " + choice + "\n"
mca_questions.append(mca_question)
return mca_questions
file_path = 'chapter-2.pdf'
context = get_text_from_pdf(file_path)
questions = get_mca_questions(context)
for i, question in enumerate(questions):
print(f"Question {i+1}: {question}")
In this modified code, we first import the necessary LLM models and tokenizer. Then, we define a new get_text_from_pdf function that uses the tokenizer to extract text from the PDF file. We open the PDF file, read it page by page, and encode each page's text using the tokenizer. We then return the encoded text.
We then call the get_mca_questions function with the encoded text as input, and iterate over the resulting questions, printing each one.
Note that you'll need to replace the github-ubert-base-uncased-whole-word-masking-finetuned-squad model and tokenizer with the appropriate ones for your specific use case. Also, make sure to install the necessary packages and libraries to use LLMs in your code.