Skip to content

Commit 069f57d

Browse files
committed
first commit v1.1
0 parents  commit 069f57d

31 files changed

+861
-0
lines changed

.DS_Store

8 KB
Binary file not shown.

README.md

Lines changed: 291 additions & 0 deletions
Large diffs are not rendered by default.

__pycache__/app.cpython-39.pyc

2.52 KB
Binary file not shown.

app.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
from core import generate_questions, generate_pdf
4+
import pandas as pd
5+
6+
df = pd.read_csv('sample_questions/Questions100_Abstractions.csv')
7+
df['question'] = df['question'].replace('\n','<br />', regex=True)
8+
9+
class QuestionGenerator:
10+
def __init__(self):
11+
self.root = tk.Tk()
12+
self.root.title("Question Paper Generator Abstraction")
13+
self.radio_var = tk.StringVar()
14+
self.radio_var.set("Easy") # Set the default value
15+
16+
self.create_widgets()
17+
18+
def generate_pdf(self):
19+
pdf_name = self.output_name_entry.get().strip()
20+
21+
22+
23+
if pdf_name:
24+
selected_option = self.radio_var.get()
25+
ques = df[df['level'] == selected_option]
26+
# Add code here to generate the PDF with the selected option and the pdf_name
27+
number_of_papers = int()
28+
if self.num_papers_entry.get().strip():
29+
if self.num_papers_entry.get().strip().isdigit():
30+
number_of_papers = int(self.num_papers_entry.get().strip())
31+
print("number of")
32+
else:
33+
messagebox.showerror("Error", "Please enter a valid number of papers.")
34+
else:
35+
number_of_papers = 1
36+
print(number_of_papers)
37+
for i in range(0, number_of_papers):
38+
questions = generate_questions.generate_question(input_ques=ques)
39+
40+
generate_pdf.generate_pdf(questions, pdf_name=f'{pdf_name}_{i+1}')
41+
else:
42+
messagebox.showerror("Error", "Please enter a valid PDF name.")
43+
44+
def create_widgets(self):
45+
# Radio buttons
46+
option1_radio = tk.Radiobutton(self.root, text="Easy", variable=self.radio_var, value="easy")
47+
option2_radio = tk.Radiobutton(self.root, text="Medium", variable=self.radio_var, value="medium")
48+
option3_radio = tk.Radiobutton(self.root, text="Hard", variable=self.radio_var, value="hard")
49+
50+
51+
num_papers_label = tk.Label(self.root, text="Number of papers you want to generate:")
52+
self.num_papers_entry = tk.Entry(self.root)
53+
54+
# Output PDF name entry field
55+
output_name_label = tk.Label(self.root, text="Output PDF Name:")
56+
self.output_name_entry = tk.Entry(self.root)
57+
58+
# Generate Button
59+
generate_button = tk.Button(self.root, text="Generate PDF", command=self.generate_pdf)
60+
61+
# Layout using grid
62+
option1_radio.grid(row=0, column=0, padx=10, pady=5)
63+
option2_radio.grid(row=1, column=0, padx=10, pady=5)
64+
option3_radio.grid(row=2, column=0, padx=10, pady=5)
65+
66+
num_papers_label.grid(row=3, column=0, padx=10, pady=5)
67+
self.num_papers_entry.grid(row=3, column=1, padx=10, pady=5)
68+
69+
output_name_label.grid(row=4, column=0, padx=10, pady=5)
70+
self.output_name_entry.grid(row=4, column=1, padx=10, pady=5)
71+
72+
generate_button.grid(row=5, column=0, columnspan=2, padx=10, pady=10)
73+
74+
def run(self):
75+
# Start the main event loop
76+
self.root.mainloop()
77+
78+

core/__init__.py

Whitespace-only changes.
147 Bytes
Binary file not shown.
1.06 KB
Binary file not shown.
2.83 KB
Binary file not shown.

core/generate_pdf.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import jinja2
2+
import pdfkit
3+
import os
4+
import sys
5+
6+
def get_wkhtmltopdf_path() -> str:
7+
if sys.platform.startswith('linux'):
8+
# Linux
9+
return '/usr/local/bin/wkhtmltopdf'
10+
elif sys.platform.startswith('darwin'):
11+
# macOS
12+
return '/usr/local/bin/wkhtmltopdf'
13+
elif sys.platform.startswith('win'):
14+
# Windows
15+
return r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
16+
else:
17+
raise OSError("Unsupported operating system. Please install wkhtmltopdf and set the path manually.")
18+
19+
20+
template = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'template')))
21+
template = template.get_template('index.html')
22+
23+
wkhtmltopdf_path = get_wkhtmltopdf_path()
24+
conf = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)
25+
26+
def generate_pdf(questions, pdf_name):
27+
html = template.render(questions=questions)
28+
pdfkit.from_string(html, f'{pdf_name}.pdf')
29+
return True

core/generate_questions.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import pandas as pd
2+
from pandas import DataFrame
3+
import re
4+
import random
5+
import string
6+
from parrot import Parrot
7+
8+
9+
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5", use_gpu=False)
10+
11+
12+
13+
def modify_question(ques: str) -> str:
14+
# Regular expressions to match variable, class, and function names
15+
variable_pattern:str = r'\b(int|double|float|char|bool)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*;'
16+
class_pattern:str = r'\bclass\s+([a-zA-Z_][a-zA-Z0-9_]*)\b'
17+
function_pattern:str = r'\b(int|double|float|char|bool)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*{'
18+
inherit_pattern:str = r'\bclass\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*public\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*{'
19+
# new object created pattern eg :- B b;
20+
new_object_pattern:str = r'\b([a-zA-Z_][a-zA-Z0-9_]*)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*;'
21+
# Objects calling functions pattern eg :- b.func();
22+
object_function_pattern:str = r'\b([a-zA-Z_][a-zA-Z0-9_]*)\.([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*;'
23+
24+
function_declaration_pattern:str = r'\b(int|double|float|char|bool|void)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)\s*;'
25+
pattern = r"(?i)(?:what|how|when|why|which|who|whom|where|can|do|is)\s.*[?].*"
26+
27+
28+
# Function to generate random word
29+
def generate_random_word() -> str:
30+
prefix:str = random.choice(['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta', 'Eta', 'Theta'])
31+
suffix = ''.join(random.choices(string.ascii_uppercase, k=random.randint(1, 3)))
32+
return f'{prefix}{suffix}'
33+
34+
def random_letter() -> str:
35+
return random.choice(string.ascii_lowercase)
36+
37+
38+
# Replace variable, class, and function names with random words
39+
# Does code have classes
40+
replaced_code = ques
41+
42+
43+
if re.search(pattern, ques):
44+
# Replace class names with random words
45+
questions = re.findall(pattern, ques)
46+
print(questions[0])
47+
new_questions = parrot.augment(input_phrase=questions[0], use_gpu=False)
48+
# choose any random from the new_questions that is a list of tuples
49+
if new_questions:
50+
new_question = random.choice(new_questions)
51+
new_question = new_question[0]
52+
replaced_code = re.sub(pattern, new_question, ques)
53+
54+
55+
if re.search(class_pattern, ques):
56+
# Replace class names with random words
57+
new_class_name = generate_random_word()
58+
replaced_code = re.sub(class_pattern, 'class ' + new_class_name, ques)
59+
60+
# Does code have inheritance
61+
if re.search(inherit_pattern, replaced_code):
62+
# Replace inheritance names with random words
63+
replaced_code = re.sub(inherit_pattern, 'class ' + generate_random_word() + ' : public ' + new_class_name, replaced_code)
64+
# Doe
65+
# Does code have functions
66+
if re.search(function_pattern, replaced_code):
67+
# Replace function names with random words
68+
replaced_code = re.sub(function_pattern, 'int ' + generate_random_word() + '()', replaced_code)
69+
70+
# Does code have function declarations
71+
if re.search(function_declaration_pattern, replaced_code):
72+
# Replace function names with random words
73+
replaced_code = re.sub(function_declaration_pattern, 'int ' + generate_random_word() + '();', replaced_code)
74+
75+
# Does code have variables
76+
if re.search(variable_pattern, replaced_code):
77+
# Replace variable names with random words
78+
replaced_code = re.sub(variable_pattern, 'int ' + random_letter() + ';', replaced_code)
79+
80+
# Does code have new objects
81+
if re.search(new_object_pattern, replaced_code):
82+
# Replace new object names with random words with new class names
83+
object_name = random_letter()
84+
replaced_code = re.sub(new_object_pattern, new_class_name + ' ' + object_name + ';', replaced_code)
85+
86+
# Does code have objects calling functions
87+
if re.search(object_function_pattern, replaced_code):
88+
# Replace object names with random words
89+
replaced_code = re.sub(object_function_pattern, object_name + '.' + generate_random_word() + '();', replaced_code)
90+
return replaced_code
91+
92+
93+
94+
def generate_question(input_ques: DataFrame) -> str:
95+
print("Hello")
96+
ques:str = ""
97+
for i in range(len(input_ques)):
98+
ques += f"Q.{i+1} " + modify_question(input_ques.iloc[i]['question']) + "<br/><br/>" + "A." + str(input_ques.iloc[i]['a']) + "<br/>" + "B." + str(input_ques.iloc[i]['b']) + "<br />" + "C." + str(input_ques.iloc[i]['c']) + "<br/>" + "D." + str(input_ques.iloc[i]['d']) + "<br /><br/>"
99+
100+
return ques

0 commit comments

Comments
 (0)