Skip to content

Commit 83d39dd

Browse files
committed
flake8 linting
1 parent 8488e33 commit 83d39dd

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

llm_quiz_generator/main.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
"""
2-
___________________________________________________________________________________________________________________________________________________
3-
| |
4-
| To use this script, please check the README.md file in the directory. A quick start to get the project running is described here. |
5-
| |
6-
| 1. Create a Groq account and get your API key at https://console.groq.com/login. |
7-
| |
8-
| 2. Either: |
9-
| - Add your API key directly to line 38: API_KEY = "your_groq_api_key_here", or |
10-
| - Create a .env file in the same directory, and add GROQ_API_KEY=your_groq_api_key_here. |
11-
| |
12-
| 3. Place all your PDFs in a folder named 'Source' in the same directory as this script. |
13-
| |
14-
| 4. Run the script: |
15-
| python quiz_generator.py |
16-
| |
17-
| The generated MCQ quiz will be saved in a file called 'generated_mcq_quiz.txt'. |
18-
|_________________________________________________________________________________________________________________________________________________|
19-
"""
20-
21-
22-
# Change this if you want to set the number of MCQ's
23-
num_questions = 5
24-
25-
261
import os
272
from PyPDF2 import PdfReader
283
from datetime import datetime
@@ -33,20 +8,23 @@
338
from langchain_huggingface import HuggingFaceEmbeddings
349
from langchain.text_splitter import CharacterTextSplitter
3510

36-
3711
load_dotenv(find_dotenv())
3812
API_KEY = os.environ["GROQ_API_KEY"]
3913

14+
# Change this if you want to set the number of MCQs
15+
num_questions = 5
16+
4017

4118
def extract_text_from_pdfs():
42-
print(f"Extracting text from PDF files in the folder: 'Source'...")
19+
"""Extracts text from PDF files in the 'Source' folder."""
20+
print("Extracting text from PDF files in the folder: 'Source'...")
4321
all_text = []
44-
45-
if not os.path.exists('Source') or not os.listdir('Source'):
46-
print("Folder 'Source' is empty or not found!")
22+
23+
if len(os.listdir('Source')) == 0:
24+
print("Source Folder Empty!")
4725
print("Process exiting...")
4826
exit(0)
49-
27+
5028
for file_name in os.listdir('Source'):
5129
if file_name.endswith(".pdf"):
5230
file_path = os.path.join('Source', file_name)
@@ -57,8 +35,10 @@ def extract_text_from_pdfs():
5735
print("Text extraction completed.")
5836
return " ".join(all_text)
5937

38+
6039
def generate_unique_mcq(text, num_questions=5):
61-
print(f"Splitting text into chunks and creating embeddings for LLM processing...")
40+
"""Generates unique multiple choice questions from text."""
41+
print("LLM processing...")
6242
text_splitter = CharacterTextSplitter(
6343
chunk_size=1000,
6444
chunk_overlap=0
@@ -82,42 +62,48 @@ def generate_unique_mcq(text, num_questions=5):
8262
)
8363

8464
quiz = []
85-
query = f"Generate {num_questions} unique multiple choice questions from the following text: {text} " \
86-
f"Provide 4 answer options and also the correct answer in plaintext."
87-
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+
8871
response = retrieval_chain.invoke(query)
8972
question_and_options = response['result']
9073
quiz.append(question_and_options)
9174

9275
print("MCQ generation completed.")
9376
return quiz
9477

78+
9579
def save_mcq_to_file(quiz, file_name="generated_mcq_quiz.txt"):
80+
"""Saves generated MCQs to a text file."""
9681
output_folder = "Generated_Quizes"
97-
82+
9883
if not os.path.exists(output_folder):
9984
os.makedirs(output_folder)
10085
print(f"Folder '{output_folder}' created.")
101-
86+
10287
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
10388
file_name = f"generated_mcq_quiz_{current_time}.txt"
10489
file_path = os.path.join(output_folder, file_name)
105-
90+
10691
print(f"Saving the generated MCQs to file: '{file_path}'...")
10792
with open(file_path, "w") as f:
10893
for i, question in enumerate(quiz, 1):
10994
f.write(f"Question {i}:\n{question}\n\n")
110-
95+
11196
print(f"MCQ Quiz saved to {file_path}")
11297

98+
11399
if __name__ == "__main__":
114100
if not os.path.exists('Source'):
115-
print(f"Folder 'Source' not found.")
101+
print("Folder 'Source' not found.")
116102
else:
117-
print(f"Folder 'Source' found. Starting process...")
103+
print("Folder 'Source' found. Starting process...")
118104
text = extract_text_from_pdfs()
119105
print("Text extracted from PDFs.")
120-
106+
121107
mcq_quiz = generate_unique_mcq(text, num_questions=num_questions)
122108
save_mcq_to_file(mcq_quiz)
123109
print("Process completed successfully.")

0 commit comments

Comments
 (0)