yukti-kash commited on
Commit
df36d99
1 Parent(s): d311129

Upload 2 files

Browse files
Files changed (2) hide show
  1. MCQGenerator.py +72 -0
  2. util.py +71 -0
MCQGenerator.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import pandas as pd
4
+ import traceback
5
+ import PyPDF2
6
+
7
+
8
+ from dotenv import load_dotenv
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain import PromptTemplate, LLMChain, OpenAI
11
+ from langchain.chains import SequentialChain
12
+
13
+
14
+ # load environment variables as mentioned in .env
15
+
16
+ load_dotenv()
17
+
18
+ # geting envrionment variable like => os.environ
19
+ key = os.getenv('OPENAI_API_KEY')
20
+
21
+ # making openai object
22
+ llm = ChatOpenAI(openai_api_key = key,model_name = 'gpt-3.5-turbo',temperature = 0.6)
23
+
24
+ # creating Tempalate for PromptTemplate
25
+
26
+ Template = '''
27
+
28
+ You are a helpful MCQ Generater and u have been given a text : {text},
29
+
30
+ You have to generate {number} mcq questions on {subject} and
31
+ level of difficulty should be {level} .
32
+
33
+ The response should be kept the way it is given in RESPONSE_JSON and use it as guide
34
+
35
+ ### RESPONSE_JSON
36
+
37
+ {response_json}'''
38
+
39
+ quiz_prompt = PromptTemplate(
40
+
41
+ input_variables = ['text','number','subject','level','response_json'],
42
+ template = Template
43
+ )
44
+
45
+
46
+ quiz_chain = LLMChain(llm = llm,prompt = quiz_prompt,output_key = 'quiz')
47
+
48
+
49
+ Template2 = '''
50
+
51
+ Quiz : {quiz} ,
52
+
53
+ U are an english grammerian and writer
54
+ Task : Ur task is to anyalyse or review the quiz questions in the given quiz
55
+ quiz for the given subject : {subject},
56
+
57
+ Just give a review about the quiz like how it is and
58
+ If the quiz doesn't fit with the cognitve abiltiy of the student then just change the
59
+ questions in the quiz and difficulty level of the quiz to make it fit
60
+ '''
61
+
62
+ # to check the complexity and review the quiz
63
+
64
+ complex_prompt = PromptTemplate(
65
+
66
+ input_variables = ['quiz','subject'],
67
+ template = Template2
68
+ )
69
+
70
+ complex_chain = LLMChain(llm = llm, prompt = complex_prompt, output_key = 'complexity')
71
+
72
+ final_chain = SequentialChain(chains = [quiz_chain, complex_chain], input_variables = ['text','number','subject','level','response_json'],output_variables = ['quiz','complexity'])
util.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import json
4
+ import pandas as pd
5
+ import PyPDF2
6
+ import traceback
7
+
8
+
9
+ def read_flie(file):
10
+
11
+ if file.name.endswith('.pdf'):
12
+ try :
13
+
14
+ pdf_content = PyPDF2.PdfFileReader(file)
15
+
16
+ text = ''
17
+
18
+ for page in pdf_content.pages:
19
+ text+=page
20
+
21
+ return text
22
+
23
+ except Exception as e :
24
+ raise Exception( 'error in reading the file >> Please try reuploading')
25
+
26
+ elif file.name.endswith('.txt'):
27
+
28
+ return file.read().decode('utf-8')
29
+
30
+ else :
31
+
32
+ raise Exception(
33
+
34
+ 'Not File desired format : only PDF and txt files are supported '
35
+ )
36
+
37
+
38
+ def get_table_data(quiz_str):
39
+
40
+ try :
41
+
42
+ quiz_dict = json.loads(quiz_str)
43
+
44
+ table = []
45
+
46
+ for key,value in quiz_dict.items():
47
+
48
+ print(key," | ",value)
49
+
50
+ ques = value['question']
51
+ options = ' | '.join(
52
+ [
53
+ f"{option} : {options}"
54
+ for option, options in value['options'].items()
55
+
56
+ ]
57
+ )
58
+ correct = value['correct']
59
+ table.append({'ques':ques,'options' : options, 'correct' : correct})
60
+
61
+ return table
62
+
63
+ except Exception as e :
64
+
65
+ # traceback.print(e)
66
+
67
+ return False
68
+
69
+
70
+
71
+