Spaces:
Build error
Build error
Rohit Diwane
commited on
Commit
·
3945df2
1
Parent(s):
78cdb5a
Create mcqgenrator.py
Browse files- mcqgenrator.py +67 -0
mcqgenrator.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.llms import OpenAI
|
| 2 |
+
from langchain.chat_models import ChatOpenAI
|
| 3 |
+
from langchain.prompts import PromptTemplate
|
| 4 |
+
from langchain.chains import LLMChain
|
| 5 |
+
from langchain.chains import SequentialChain
|
| 6 |
+
from langchain.callbacks import get_openai_callback
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import traceback
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import PyPDF2
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
key=os.getenv("openai_key")
|
| 18 |
+
print(key)
|
| 19 |
+
|
| 20 |
+
llm=ChatOpenAI(openai_api_key=key,model_name="gpt-3.5-turbo",temperature=0.7)
|
| 21 |
+
|
| 22 |
+
with open(r"D:\NLP_Specialization\Gen_AI\LangChain_LLM\mcq_gen_project-main\mcq_gen_project-main\Response.json","r") as f:
|
| 23 |
+
RESPONSE_JSON=json.load(f)
|
| 24 |
+
|
| 25 |
+
print(RESPONSE_JSON)
|
| 26 |
+
|
| 27 |
+
TEMPLATE="""
|
| 28 |
+
Text:{text}
|
| 29 |
+
You are an expert MCQ maker. Given the above text, it is your job to \
|
| 30 |
+
create a quiz of {number} multiple choice questions for {subject} students in {tone} tone.
|
| 31 |
+
Make sure the questions are not repeated and check all the questions to be conforming the text as well.
|
| 32 |
+
Make sure to format your response like RESPONSE_JSON below and use it as a guide. \
|
| 33 |
+
Ensure to make {number} MCQs
|
| 34 |
+
### RESPONSE_JSON
|
| 35 |
+
{RESPONSE_JSON}
|
| 36 |
+
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
quiz_generation_prompt = PromptTemplate(
|
| 40 |
+
input_variables=["text", "number", "grade", "tone", "RESPONSE_JSON"],
|
| 41 |
+
template=TEMPLATE)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key="quiz", verbose=True)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
TEMPLATE2="""
|
| 48 |
+
You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\
|
| 49 |
+
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.
|
| 50 |
+
if the quiz is not at per with the cognitive and analytical abilities of the students,\
|
| 51 |
+
update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities
|
| 52 |
+
Quiz_MCQs:
|
| 53 |
+
{quiz}
|
| 54 |
+
|
| 55 |
+
Check from an expert English Writer of the above quiz:
|
| 56 |
+
"""
|
| 57 |
+
quiz_evaluation_prompt=PromptTemplate(input_variables=["subject", "quiz"], template=TEMPLATE2)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
generate_evaluate_chain=SequentialChain(chains=[quiz_chain,review_chain],
|
| 66 |
+
input_variables=["text", "number", "subject", "tone", "RESPONSE_JSON"],
|
| 67 |
+
output_variables=["quiz", "review"], verbose=True)
|