File size: 7,652 Bytes
74885f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import re
import os
from apiclient import discovery
from oauth2client import client, file, tools
import bardapi
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
from transformers import T5ForConditionalGeneration, T5Tokenizer

SCOPES = "https://www.googleapis.com/auth/forms.body"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"

NEW_FORM = {
    "info": {
        "title": "Quiz"
    }
}

model_name = "t5-base"
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)

def generate_quiz_questions(prompt):
    # Set your Bard API key as an environment variable
    os.environ['_BARD_API_KEY'] = "WwgqSrcbBC71HsiWpTlqnbDC9TQ3-9N1YyY6CHxOEfFp_qeCe0laziZoOT_dkTEjhJmOcw."
    
    prompt_suffix = ". Each generated question has to begin with '๐Ÿ”น', each choice has to begin with '๐Ÿ”ธ', and each correct answer has to begin with 'โœ”๏ธ'."


    # Send API requests and get responses
    response = bardapi.core.Bard().get_answer(prompt + prompt_suffix)

    quiz = response["content"]

    return quiz

'''
def generate_quiz_url(prompt_text, form_service):
    # Generate quiz questions based on the transcribed text
    text = generate_quiz_questions(prompt_text)

    # Questions, choices, and correct answers
    questions = re.findall(r"๐Ÿ”น (.*?)\n", text)
    choices = re.findall(r"๐Ÿ”ธ (.*?)\n", text)
    answers = re.findall(r"โœ”๏ธ (.*?)\n", text)

    # Remove the '**' from the questions list (they are not part of the question), choices, and correct answers
    questions = [question.replace('**', '') for question in questions]
    answers = [answer.replace('**', '') for answer in answers]

    questions_list = []

    # Fill the questions_list variable
    for i, question in enumerate(questions):
        choices_for_question = choices[i * 4:(i + 1) * 4]
        correct_answer = answers[i] if i < len(answers) else ""

        question_dict = {
            "question": question,
            "choices": choices_for_question,
            "correct_answer": correct_answer
        }

        questions_list.append(question_dict)

    # Create the initial form
    result = form_service.forms().create(body=NEW_FORM).execute()

    # Add the questions to the form
    question_requests = []
    for index, question in enumerate(questions_list):
        new_question = {
            "createItem": {
                "item": {
                    "title": question["question"],
                    "questionItem": {
                        "question": {
                            "required": True,
                            "choiceQuestion": {
                                "type": "RADIO",
                                "options": [
                                    {"value": choice} for choice in question["choices"]
                                ],
                                "shuffle": True
                            }
                        }
                    }
                },
                "location": {
                    "index": index
                }
            }
        }
        question_requests.append(new_question)

    NEW_QUESTIONS = {
        "requests": question_requests
    }

    question_setting = form_service.forms().batchUpdate(formId=result["formId"], body=NEW_QUESTIONS).execute()

    # Retrieve the updated form result
    get_result = form_service.forms().get(formId=result["formId"]).execute()

    # Get the form ID
    form_id = get_result["formId"]

    # Construct the quiz link using the form ID
    form_url = result["responderUri"]

    return form_url
'''

def explain_quiz_answers(questions_list):
    explanations = []

    for question in questions_list:
        context = question["question"]
        choices = question["choices"]
        correct_answer = question["correct_answer"]

        explanation = f"Question: {context}\n"

        for choice in choices:
            # Construct a query with each choice as a question
            query = f"What is the reason for choosing '{choice}' in {context}?"

            # Tokenize the query and context
            inputs = tokenizer.encode_plus(query, context, return_tensors="pt", truncation=True, padding="max_length", max_length=512)

            # Generate the explanation using the T5 model
            outputs = model.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], max_length=256)

            # Decode the explanation
            explanation_text = tokenizer.decode(outputs[0], skip_special_tokens=True)

            # Add the explanation to the overall explanation
            explanation += f"\nChoice: {choice}\nExplanation: {explanation_text}"

            # Add an indicator if the choice is the correct answer
            if choice == correct_answer:
                explanation += " (Correct Answer)"

            explanation += "\n"

        explanations.append(explanation)

    return explanations


def generate_quiz_url(prompt_text, form_service):
    # Generate quiz questions based on the transcribed text
    text = generate_quiz_questions(prompt_text)

    # Questions, choices, and correct answers
    questions = re.findall(r"๐Ÿ”น (.*?)\n", text)
    choices = re.findall(r"๐Ÿ”ธ (.*?)\n", text)
    answers = re.findall(r"โœ”๏ธ (.*?)\n", text)

    # Remove the '**' from the questions list (they are not part of the question), choices, and correct answers
    questions = [question.replace('**', '') for question in questions]
    answers = [answer.replace('**', '') for answer in answers]

    questions_list = []

    # Fill the questions_list variable
    for i, question in enumerate(questions):
        choices_for_question = choices[i * 4:(i + 1) * 4]
        correct_answer = answers[i] if i < len(answers) else ""

        question_dict = {
            "question": question,
            "choices": choices_for_question,
            "correct_answer": correct_answer
        }

        questions_list.append(question_dict)

    # Create the initial form
    result = form_service.forms().create(body=NEW_FORM).execute()

    # Add the questions to the form
    question_requests = []
    for index, question in enumerate(questions_list):
        new_question = {
            "createItem": {
                "item": {
                    "title": question["question"],
                    "questionItem": {
                        "question": {
                            "required": True,
                            "choiceQuestion": {
                                "type": "RADIO",
                                "options": [
                                    {"value": choice} for choice in question["choices"]
                                ],
                                "shuffle": True
                            }
                        }
                    }
                },
                "location": {
                    "index": index
                }
            }
        }
        question_requests.append(new_question)

    NEW_QUESTIONS = {
        "requests": question_requests
    }

    question_setting = form_service.forms().batchUpdate(formId=result["formId"], body=NEW_QUESTIONS).execute()

    # Retrieve the updated form result
    get_result = form_service.forms().get(formId=result["formId"]).execute()

    # Get the form ID
    form_id = get_result["formId"]

    # Construct the quiz link using the form ID
    form_url = result["responderUri"]

    # Get the explanations for the quiz
    explanations = explain_quiz_answers(questions_list)

    return form_url, explanations