abhiixxhek commited on
Commit
ddc83da
·
verified ·
1 Parent(s): 5de878d

Update QuizMaster.py

Browse files
Files changed (1) hide show
  1. QuizMaster.py +154 -57
QuizMaster.py CHANGED
@@ -1,57 +1,154 @@
1
- import streamlit as st
2
- from dotenv import load_dotenv
3
- import os
4
- import json
5
- import base64
6
- from langchain_google_genai import ChatGoogleGenerativeAI
7
-
8
- # Load environment variables
9
- load_dotenv()
10
- os.getenv("GOOGLE_API_KEY")
11
-
12
- # Constants for templates
13
- RESPONSE_JSON = {
14
- # Example JSON response structure...
15
- }
16
-
17
- TEMPLATE = """
18
- Text:{text}
19
- You are an expert MCQ maker...
20
- """
21
-
22
- TEMPLATE2 = """
23
- You are an expert English grammarian and writer...
24
- """
25
-
26
- def show():
27
- st.header("Quiz Master :game_die:")
28
- st.subheader("Generate your MCQ quiz instantly!")
29
-
30
- TEXT = st.text_input("Quiz Topic:", key="input1")
31
- NUMBER = st.text_input("Number of Questions:", key="input2")
32
- SUBJECT = st.text_input("Subject Area:", key="input3")
33
- TONE = st.text_input("Difficulty Level:", key="input4")
34
-
35
- submit = st.button("Generate Quiz")
36
-
37
- if submit and TEXT:
38
- llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.9)
39
- # Chain setup...
40
- response = generate_evaluate_chain(
41
- # Passing inputs...
42
- )
43
- # Process and display the generated quiz...
44
- pdf = PDF()
45
- # PDF generation code...
46
-
47
- with open(pdf_file_path, "rb") as pdf_file:
48
- st.download_button(
49
- label=f"Download {SUBJECT} Quiz PDF",
50
- data=pdf_file,
51
- file_name=f"{SUBJECT}_quiz.pdf",
52
- mime="application/pdf",
53
- )
54
-
55
- # Display the PDF in the app
56
- 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>'
57
- 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
+ TEMPLATE2="""
86
+ You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\
87
+ 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.
88
+ if the quiz is not at per with the cognitive and analytical abilities of the students,\
89
+ update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities
90
+ Quiz_MCQs:
91
+ {quiz}
92
+ Check from an expert English Writer of the above quiz:
93
+ """
94
+ def show():
95
+ st.header("MCQ_Generator")
96
+ TEXT=st.text_input("Input Prompt: ",key="input1")
97
+ NUMBER=st.text_input("Number of MCQs ",key="input2")
98
+ SUBJECT=st.text_input("Topic of MCQs ",key="input3")
99
+ TONE=st.text_input("Difficulty Level ",key="input4")
100
+
101
+ submit=st.button("Submit")
102
+
103
+ if submit and TEXT:
104
+ llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.9)
105
+ from langchain.prompts import PromptTemplate
106
+ from langchain.chains import LLMChain
107
+ from langchain.chains import SequentialChain
108
+ quiz_generation_prompt = PromptTemplate(
109
+ input_variables=["text", "number", "subject", "tone", "response_json"],
110
+ template=TEMPLATE
111
+ )
112
+ quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key="quiz", verbose=True)
113
+ quiz_evaluation_prompt=PromptTemplate(input_variables=["subject", "quiz"], template=TEMPLATE2)
114
+ review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True)
115
+ generate_evaluate_chain=SequentialChain(chains=[quiz_chain, review_chain], input_variables=["text", "number", "subject", "tone", "response_json"],
116
+ output_variables=["quiz", "review"], verbose=True,)
117
+ response=generate_evaluate_chain(
118
+ {
119
+ "text": TEXT,
120
+ "number": NUMBER,
121
+ "subject":SUBJECT,
122
+ "tone": TONE,
123
+ "response_json": json.dumps(RESPONSE_JSON)
124
+ }
125
+ )
126
+ quiz=response.get("quiz")
127
+ if '### RESPONSE_JSON\n' in quiz:
128
+ quiz = quiz.split('### RESPONSE_JSON\n')[1]
129
+ quiz = json.loads(quiz)
130
+ else:
131
+ quiz=json.loads(quiz)
132
+ pdf = PDF()
133
+ pdf.add_page()
134
+ pdf.set_title(SUBJECT+" Quiz")
135
+ answers = {}
136
+ for key, value in quiz.items():
137
+ question_num = int(key)
138
+ pdf.add_question(question_num, value["mcq"], value["options"])
139
+ answers[question_num] = value["correct"]
140
+ pdf.add_answers_section(answers)
141
+
142
+ pdf_file_path =SUBJECT+"_mcq.pdf"
143
+ pdf.output(pdf_file_path)
144
+
145
+ with open(pdf_file_path, "rb") as pdf_file:
146
+ st.download_button(
147
+ label="Download "+SUBJECT+" Quiz PDF",
148
+ data=pdf_file,
149
+ file_name=SUBJECT+"_quiz.pdf",
150
+ mime="application/pdf",
151
+ )
152
+
153
+ 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>'
154
+ st.markdown(pdf_display, unsafe_allow_html=True)