|
| 1 | +import os |
| 2 | +from PyPDF2 import PdfReader |
| 3 | +from datetime import datetime |
| 4 | +from langchain_groq import ChatGroq |
| 5 | +from langchain.chains import RetrievalQA |
| 6 | +from dotenv import load_dotenv, find_dotenv |
| 7 | +from langchain_community.vectorstores import FAISS |
| 8 | +from langchain_huggingface import HuggingFaceEmbeddings |
| 9 | +from langchain.text_splitter import CharacterTextSplitter |
| 10 | + |
| 11 | +load_dotenv(find_dotenv()) |
| 12 | +API_KEY = os.environ["GROQ_API_KEY"] |
| 13 | + |
| 14 | +# Change this if you want to set the number of MCQs |
| 15 | +num_questions = 5 |
| 16 | + |
| 17 | + |
| 18 | +def extract_text_from_pdfs(): |
| 19 | + """Extracts text from PDF files in the 'Source' folder.""" |
| 20 | + print("Extracting text from PDF files in the folder: 'Source'...") |
| 21 | + all_text = [] |
| 22 | + |
| 23 | + if len(os.listdir('Source')) == 0: |
| 24 | + print("Source Folder Empty!") |
| 25 | + print("Process exiting...") |
| 26 | + exit(0) |
| 27 | + |
| 28 | + for file_name in os.listdir('Source'): |
| 29 | + if file_name.endswith(".pdf"): |
| 30 | + file_path = os.path.join('Source', file_name) |
| 31 | + print(f"Processing file: {file_name}") |
| 32 | + reader = PdfReader(file_path) |
| 33 | + for page in reader.pages: |
| 34 | + all_text.append(page.extract_text()) |
| 35 | + print("Text extraction completed.") |
| 36 | + return " ".join(all_text) |
| 37 | + |
| 38 | + |
| 39 | +def generate_unique_mcq(text, num_questions=5): |
| 40 | + """Generates unique multiple choice questions from text.""" |
| 41 | + print("LLM processing...") |
| 42 | + text_splitter = CharacterTextSplitter( |
| 43 | + chunk_size=1000, |
| 44 | + chunk_overlap=0 |
| 45 | + ) |
| 46 | + docs = text_splitter.create_documents([text]) |
| 47 | + |
| 48 | + embeddings = HuggingFaceEmbeddings() |
| 49 | + store = FAISS.from_documents(docs, embeddings) |
| 50 | + |
| 51 | + print(f"Connecting to LLM to generate {num_questions} unique MCQs...") |
| 52 | + llm = ChatGroq( |
| 53 | + temperature=0.2, |
| 54 | + model="llama-3.1-70b-versatile", |
| 55 | + api_key=API_KEY |
| 56 | + ) |
| 57 | + |
| 58 | + retrieval_chain = RetrievalQA.from_chain_type( |
| 59 | + llm=llm, |
| 60 | + chain_type="stuff", |
| 61 | + retriever=store.as_retriever() |
| 62 | + ) |
| 63 | + |
| 64 | + quiz = [] |
| 65 | + query = ( |
| 66 | + f"Generate {num_questions} unique multiple choice questions" |
| 67 | + "from the text: {text}" |
| 68 | + "Provide 4 answer options and also the correct answer in plaintext." |
| 69 | + ) |
| 70 | + |
| 71 | + response = retrieval_chain.invoke(query) |
| 72 | + question_and_options = response['result'] |
| 73 | + quiz.append(question_and_options) |
| 74 | + |
| 75 | + print("MCQ generation completed.") |
| 76 | + return quiz |
| 77 | + |
| 78 | + |
| 79 | +def save_mcq_to_file(quiz, file_name="generated_mcq_quiz.txt"): |
| 80 | + """Saves generated MCQs to a text file.""" |
| 81 | + output_folder = "Generated_Quizes" |
| 82 | + |
| 83 | + if not os.path.exists(output_folder): |
| 84 | + os.makedirs(output_folder) |
| 85 | + print(f"Folder '{output_folder}' created.") |
| 86 | + |
| 87 | + current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 88 | + file_name = f"generated_mcq_quiz_{current_time}.txt" |
| 89 | + file_path = os.path.join(output_folder, file_name) |
| 90 | + |
| 91 | + print(f"Saving the generated MCQs to file: '{file_path}'...") |
| 92 | + with open(file_path, "w") as f: |
| 93 | + for i, question in enumerate(quiz, 1): |
| 94 | + f.write(f"Question {i}:\n{question}\n\n") |
| 95 | + |
| 96 | + print(f"MCQ Quiz saved to {file_path}") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + if not os.path.exists('Source'): |
| 101 | + print("Folder 'Source' not found.") |
| 102 | + else: |
| 103 | + print("Folder 'Source' found. Starting process...") |
| 104 | + text = extract_text_from_pdfs() |
| 105 | + print("Text extracted from PDFs.") |
| 106 | + |
| 107 | + mcq_quiz = generate_unique_mcq(text, num_questions=num_questions) |
| 108 | + save_mcq_to_file(mcq_quiz) |
| 109 | + print("Process completed successfully.") |
0 commit comments