gyanbardhan123 commited on
Commit
66f22c4
1 Parent(s): 6a2f0ce

Update MCQ_Gen.py

Browse files
Files changed (1) hide show
  1. MCQ_Gen.py +155 -155
MCQ_Gen.py CHANGED
@@ -1,156 +1,156 @@
1
- from fpdf import FPDF
2
- class PDF(FPDF):
3
- def header(self):
4
- self.set_font('Arial', 'B', 12)
5
- self.cell(0, 10, 'MCQ Quiz', 0, 1, 'C')
6
-
7
- def chapter_title(self, num, label):
8
- self.set_font('Arial', '', 12)
9
- self.cell(0, 10, 'Question %d: %s' % (num, label), 0, 1, 'L')
10
- self.ln(5)
11
-
12
- def chapter_body(self, body):
13
- self.set_font('Arial', '', 12)
14
- self.multi_cell(0, 10, body)
15
- self.ln()
16
-
17
- def add_question(self, num, question, options):
18
- self.chapter_title(num, question)
19
- for key, option in options.items():
20
- self.chapter_body(f"{key}. {option}")
21
- self.ln()
22
-
23
- def add_answers_section(self, answers):
24
- self.add_page()
25
- self.set_font('Arial', 'B', 12)
26
- self.cell(0, 10, 'Answers', 0, 1, 'C')
27
- self.ln(10)
28
- self.set_font('Arial', '', 12)
29
- for num, answer in answers.items():
30
- self.cell(0, 10, f"Question {num}: {answer}", 0, 1, 'L')
31
-
32
-
33
-
34
- import streamlit as st
35
- from dotenv import load_dotenv
36
- load_dotenv()
37
- import os
38
- import json
39
- import base64
40
- from langchain_google_genai import ChatGoogleGenerativeAI
41
- os.getenv("GOOGLE_API_KEY")
42
- RESPONSE_JSON = {
43
- "1": {
44
- "mcq": "multiple choice question",
45
- "options": {
46
- "a": "choice here",
47
- "b": "choice here",
48
- "c": "choice here",
49
- "d": "choice here",
50
- },
51
- "correct": "correct answer",
52
- },
53
- "2": {
54
- "mcq": "multiple choice question",
55
- "options": {
56
- "a": "choice here",
57
- "b": "choice here",
58
- "c": "choice here",
59
- "d": "choice here",
60
- },
61
- "correct": "correct answer",
62
- },
63
- "3": {
64
- "mcq": "multiple choice question",
65
- "options": {
66
- "a": "choice here",
67
- "b": "choice here",
68
- "c": "choice here",
69
- "d": "choice here",
70
- },
71
- "correct": "correct answer",
72
- },
73
- }
74
- TEMPLATE="""
75
- Text:{text}
76
- You are an expert MCQ maker. Given the above text, it is your job to \
77
- create a quiz of {number} multiple choice questions for {subject} students in {tone} tone.
78
- Make sure the questions are not repeated and check all the questions to be conforming the text as well.
79
- Make sure to format your response like RESPONSE_JSON below and use it as a guide. \
80
- Ensure to make {number} MCQs
81
- ### RESPONSE_JSON
82
- {response_json}
83
-
84
- """
85
-
86
- TEMPLATE2="""
87
- You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\
88
- You need to evaluate the complexity of the question and give a complete analysis of the quiz. Only use at max 50 words for complexity analysis.
89
- if the quiz is not at per with the cognitive and analytical abilities of the students,\
90
- update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities
91
- Quiz_MCQs:
92
- {quiz}
93
-
94
- Check from an expert English Writer of the above quiz:
95
- """
96
- def show():
97
- st.header("MCQ_Generator")
98
- TEXT=st.text_input("Input Prompt: ",key="input1")
99
- NUMBER=st.text_input("Number of MCQs ",key="input2")
100
- SUBJECT=st.text_input("Topic of MCQs ",key="input3")
101
- TONE=st.text_input("Difficulty Level ",key="input4")
102
-
103
- submit=st.button("Submit")
104
-
105
- if submit and TEXT:
106
- llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.9)
107
- from langchain.prompts import PromptTemplate
108
- from langchain.chains import LLMChain
109
- from langchain.chains import SequentialChain
110
- quiz_generation_prompt = PromptTemplate(
111
- input_variables=["text", "number", "subject", "tone", "response_json"],
112
- template=TEMPLATE
113
- )
114
- quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key="quiz", verbose=True)
115
- quiz_evaluation_prompt=PromptTemplate(input_variables=["subject", "quiz"], template=TEMPLATE)
116
- review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True)
117
- generate_evaluate_chain=SequentialChain(chains=[quiz_chain, review_chain], input_variables=["text", "number", "subject", "tone", "response_json"],
118
- output_variables=["quiz", "review"], verbose=True,)
119
- response=generate_evaluate_chain(
120
- {
121
- "text": TEXT,
122
- "number": NUMBER,
123
- "subject":SUBJECT,
124
- "tone": TONE,
125
- "response_json": json.dumps(RESPONSE_JSON)
126
- }
127
- )
128
- quiz=response.get("quiz")
129
- if '### RESPONSE_JSON\n' in quiz:
130
- quiz = quiz.split('### RESPONSE_JSON\n')[1]
131
- quiz = json.loads(quiz)
132
- else:
133
- quiz=json.loads(quiz)
134
- pdf = PDF()
135
- pdf.add_page()
136
- pdf.set_title(SUBJECT+" Quiz")
137
- answers = {}
138
- for key, value in quiz.items():
139
- question_num = int(key)
140
- pdf.add_question(question_num, value["mcq"], value["options"])
141
- answers[question_num] = value["correct"]
142
- pdf.add_answers_section(answers)
143
-
144
- pdf_file_path =SUBJECT+"_mcq.pdf"
145
- pdf.output(pdf_file_path)
146
-
147
- with open(pdf_file_path, "rb") as pdf_file:
148
- st.download_button(
149
- label="Download "+SUBJECT+" Quiz PDF",
150
- data=pdf_file,
151
- file_name=SUBJECT+"_quiz.pdf",
152
- mime="application/pdf",
153
- )
154
-
155
- pdf_display = f'<iframe src="data:application/pdf;base64,{base64.b64encode(open(pdf_file_path, "rb").read()).decode()}" width="700" height="1000" type="application/pdf"></iframe>'
156
  st.markdown(pdf_display, unsafe_allow_html=True)
 
1
+ from fpdf import FPDF
2
+ class PDF(FPDF):
3
+ def header(self):
4
+ self.set_font('Arial', 'B', 12)
5
+ self.cell(0, 10, 'MCQ Quiz', 0, 1, 'C')
6
+
7
+ def chapter_title(self, num, label):
8
+ self.set_font('Arial', '', 12)
9
+ self.cell(0, 10, 'Question %d: %s' % (num, label), 0, 1, 'L')
10
+ self.ln(5)
11
+
12
+ def chapter_body(self, body):
13
+ self.set_font('Arial', '', 12)
14
+ self.multi_cell(0, 10, body)
15
+ self.ln()
16
+
17
+ def add_question(self, num, question, options):
18
+ self.chapter_title(num, question)
19
+ for key, option in options.items():
20
+ self.chapter_body(f"{key}. {option}")
21
+ self.ln()
22
+
23
+ def add_answers_section(self, answers):
24
+ self.add_page()
25
+ self.set_font('Arial', 'B', 12)
26
+ self.cell(0, 10, 'Answers', 0, 1, 'C')
27
+ self.ln(10)
28
+ self.set_font('Arial', '', 12)
29
+ for num, answer in answers.items():
30
+ self.cell(0, 10, f"Question {num}: {answer}", 0, 1, 'L')
31
+
32
+
33
+
34
+ import streamlit as st
35
+ from dotenv import load_dotenv
36
+ load_dotenv()
37
+ import os
38
+ import json
39
+ import base64
40
+ from langchain_google_genai import ChatGoogleGenerativeAI
41
+ os.getenv("GOOGLE_API_KEY")
42
+ RESPONSE_JSON = {
43
+ "1": {
44
+ "mcq": "multiple choice question",
45
+ "options": {
46
+ "a": "choice here",
47
+ "b": "choice here",
48
+ "c": "choice here",
49
+ "d": "choice here",
50
+ },
51
+ "correct": "correct answer",
52
+ },
53
+ "2": {
54
+ "mcq": "multiple choice question",
55
+ "options": {
56
+ "a": "choice here",
57
+ "b": "choice here",
58
+ "c": "choice here",
59
+ "d": "choice here",
60
+ },
61
+ "correct": "correct answer",
62
+ },
63
+ "3": {
64
+ "mcq": "multiple choice question",
65
+ "options": {
66
+ "a": "choice here",
67
+ "b": "choice here",
68
+ "c": "choice here",
69
+ "d": "choice here",
70
+ },
71
+ "correct": "correct answer",
72
+ },
73
+ }
74
+ TEMPLATE="""
75
+ Text:{text}
76
+ You are an expert MCQ maker. Given the above text, it is your job to \
77
+ create a quiz of {number} multiple choice questions for {subject} students in {tone} tone.
78
+ Make sure the questions are not repeated and check all the questions to be conforming the text as well.
79
+ Make sure to format your response like RESPONSE_JSON below and use it as a guide. \
80
+ Ensure to make {number} MCQs
81
+ ### RESPONSE_JSON
82
+ {response_json}
83
+
84
+ """
85
+
86
+ TEMPLATE2="""
87
+ You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\
88
+ You need to evaluate the complexity of the question and give a complete analysis of the quiz. Only use at max 50 words for complexity analysis.
89
+ if the quiz is not at per with the cognitive and analytical abilities of the students,\
90
+ update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities
91
+ Quiz_MCQs:
92
+ {quiz}
93
+
94
+ Check from an expert English Writer of the above quiz:
95
+ """
96
+ def show():
97
+ st.header("MCQ_Generator")
98
+ TEXT=st.text_input("Input Prompt: ",key="input1")
99
+ NUMBER=st.text_input("Number of MCQs ",key="input2")
100
+ SUBJECT=st.text_input("Topic of MCQs ",key="input3")
101
+ TONE=st.text_input("Difficulty Level ",key="input4")
102
+
103
+ submit=st.button("Submit")
104
+
105
+ if submit and TEXT:
106
+ llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.9)
107
+ from langchain.prompts import PromptTemplate
108
+ from langchain.chains import LLMChain
109
+ from langchain.chains import SequentialChain
110
+ quiz_generation_prompt = PromptTemplate(
111
+ input_variables=["text", "number", "subject", "tone", "response_json"],
112
+ template=TEMPLATE
113
+ )
114
+ quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key="quiz", verbose=True)
115
+ quiz_evaluation_prompt=PromptTemplate(input_variables=["subject", "quiz"], template=TEMPLATE2)
116
+ review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True)
117
+ generate_evaluate_chain=SequentialChain(chains=[quiz_chain, review_chain], input_variables=["text", "number", "subject", "tone", "response_json"],
118
+ output_variables=["quiz", "review"], verbose=True,)
119
+ response=generate_evaluate_chain(
120
+ {
121
+ "text": TEXT,
122
+ "number": NUMBER,
123
+ "subject":SUBJECT,
124
+ "tone": TONE,
125
+ "response_json": json.dumps(RESPONSE_JSON)
126
+ }
127
+ )
128
+ quiz=response.get("quiz")
129
+ if '### RESPONSE_JSON\n' in quiz:
130
+ quiz = quiz.split('### RESPONSE_JSON\n')[1]
131
+ quiz = json.loads(quiz)
132
+ else:
133
+ quiz=json.loads(quiz)
134
+ pdf = PDF()
135
+ pdf.add_page()
136
+ pdf.set_title(SUBJECT+" Quiz")
137
+ answers = {}
138
+ for key, value in quiz.items():
139
+ question_num = int(key)
140
+ pdf.add_question(question_num, value["mcq"], value["options"])
141
+ answers[question_num] = value["correct"]
142
+ pdf.add_answers_section(answers)
143
+
144
+ pdf_file_path =SUBJECT+"_mcq.pdf"
145
+ pdf.output(pdf_file_path)
146
+
147
+ with open(pdf_file_path, "rb") as pdf_file:
148
+ st.download_button(
149
+ label="Download "+SUBJECT+" Quiz PDF",
150
+ data=pdf_file,
151
+ file_name=SUBJECT+"_quiz.pdf",
152
+ mime="application/pdf",
153
+ )
154
+
155
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64.b64encode(open(pdf_file_path, "rb").read()).decode()}" width="700" height="1000" type="application/pdf"></iframe>'
156
  st.markdown(pdf_display, unsafe_allow_html=True)