Spaces:
Runtime error
Runtime error
Download mcq_func.py from QL2023/Question_general: direct link, hf CLI and curl.
- Browser
- Download file 12.9 kB
-
https://huggingface.co/spaces/QL2023/Question_general/resolve/main/mcq_func.py
- Command line
-
hf download hf://spaces/QL2023/Question_general/mcq_func.py
-
curl -L -o mcq_func.py https://huggingface.co/spaces/QL2023/Question_general/resolve/main/mcq_func.py
12.9 kB
| import openai | |
| import datetime as dt | |
| import numpy as np | |
| from openpyxl import Workbook | |
| function_description = [ | |
| { | |
| "name": "generate_moodle_mcq", | |
| "description": "Generate a MCQ question to be used in online quiz. Content to be generated including question name, question text, (by defaut) four options including the correct option, feedback for each option, general feedback if question is answered correctly, and general feedback if question is answered incorrectly. Do not generated content that you do not know, if you do not have relevant information on certain topic, put 'I do not understand this topic' in question name and 'N/A' for question text and skip remaining properties.", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "question name": { | |
| "type": "string", | |
| "description": "name of the question" | |
| }, | |
| "question text": { | |
| "type": "string", | |
| "description": "question text or main body of the question" | |
| }, | |
| "general feedback correct": { | |
| "type": "string", | |
| "description": "general feedback if question is answered correctly" | |
| }, | |
| "general feedback wrong": { | |
| "type": "string", | |
| "description": "general feedback if question is answered incorrectly" | |
| }, | |
| "correct option": { | |
| "type": "string", | |
| "description": "the correct option" | |
| }, | |
| "correct option feedback": { | |
| "type": "string", | |
| "description": "feedback for the correct option" | |
| }, | |
| "wrong option 1": { | |
| "type": "string", | |
| "description": "the wrong option" | |
| }, | |
| "wrong option 1 feedback": { | |
| "type": "string", | |
| "description": "feedback for the wrong option" | |
| }, | |
| "wrong option 2": { | |
| "type": "string", | |
| "description": "the wrong option" | |
| }, | |
| "wrong option 2 feedback": { | |
| "type": "string", | |
| "description": "feedback for the wrong option" | |
| }, | |
| "wrong option 3": { | |
| "type": "string", | |
| "description": "the wrong option" | |
| }, | |
| "wrong option 3 feedback": { | |
| "type": "string", | |
| "description": "feedback for the wrong option" | |
| }, | |
| }, | |
| "required":["question name","question text"] | |
| } | |
| } | |
| ] | |
| def get_question_content(response): | |
| function_call = response["choices"][0]["message"]["function_call"] | |
| function_name = function_call["name"] | |
| arguments = function_call["arguments"] | |
| qtext = eval(arguments).get("question text") | |
| if qtext == "N/A": | |
| qname = eval(arguments).get("question name") | |
| qcontent = "Name: " + qname + "\n" + "Text: " + qtext | |
| return qcontent, [qname,qtext,"N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"] | |
| else: | |
| qname = eval(arguments).get("question name") | |
| qfdbkc = eval(arguments).get("general feedback correct") | |
| qfdbkw = eval(arguments).get("general feedback wrong") | |
| qoptc = eval(arguments).get("correct option") | |
| qoptfdbkc = eval(arguments).get("correct option feedback") | |
| qopt1 = eval(arguments).get("wrong option 1") | |
| qoptfdbk1 = eval(arguments).get("wrong option 1 feedback") | |
| qopt2 = eval(arguments).get("wrong option 2") | |
| qoptfdbk2 = eval(arguments).get("wrong option 2 feedback") | |
| qopt3 = eval(arguments).get("wrong option 3") | |
| qoptfdbk3 = eval(arguments).get("wrong option 3 feedback") | |
| qcontent = "Name: " + qname + "\n" + "Text: " + qtext + "\n" + "Correct option: " + qoptc + "\n" + "Correct option feedback: " + qoptfdbkc + "\n" + "Wrong option 1: " + qopt1 + "\n" + "Wrong option feedback: " + qoptfdbk1 + "\n" + "Wrong option 2: " + qopt2 + "\n" + "Wrong option feedback: " + qoptfdbk2 + "\n" + "Wrong option 3: " + qopt3 + "\n" + "Wrong option feedback: " + qoptfdbk3 + "\n" + "General feedback correct: " + qfdbkc + "\n" + "General feedback wrong: " + qfdbkw | |
| return qcontent, [qname,qtext,qfdbkc,qfdbkw,qoptc,qoptfdbkc,qopt1,qoptfdbk1,qopt2,qoptfdbk2,qopt3,qoptfdbk3] | |
| def write_to_xml(question_variable): | |
| question_name = question_variable[0] | |
| question_text = question_variable[1] | |
| question_feedback_correct = question_variable[2] | |
| question_feedback_wrong = question_variable[3] | |
| question_correct_option = question_variable[4] | |
| question_correct_option_feedback = question_variable[5] | |
| question_wrong_option1 = question_variable[6] | |
| question_wrong_option_feedback1 = question_variable[7] | |
| question_wrong_option2 = question_variable[8] | |
| question_wrong_option_feedback2 = question_variable[9] | |
| question_wrong_option3 = question_variable[10] | |
| question_wrong_option_feedback3 = question_variable[11] | |
| xml_export_question = f""" | |
| <question type="multichoice"> | |
| <name> | |
| <text>{question_name}</text> | |
| </name> | |
| <questiontext format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_text}</p>]]></text> | |
| </questiontext> | |
| <generalfeedback format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;"> </p>]]></text> | |
| </generalfeedback> | |
| <defaultgrade>1</defaultgrade> | |
| <penalty>0.3333333</penalty> | |
| <hidden>0</hidden> | |
| <idnumber></idnumber> | |
| <single>true</single> | |
| <shuffleanswers>true</shuffleanswers> | |
| <answernumbering>none</answernumbering> | |
| <showstandardinstruction>0</showstandardinstruction> | |
| <correctfeedback format="html"> | |
| <text>{question_feedback_correct}</text> | |
| </correctfeedback> | |
| <partiallycorrectfeedback format="html"> | |
| <text> </text> | |
| </partiallycorrectfeedback> | |
| <incorrectfeedback format="html"> | |
| <text>{question_feedback_wrong}</text> | |
| </incorrectfeedback> | |
| <shownumcorrect/> | |
| <answer fraction="100" format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_correct_option}</p>]]></text> | |
| <feedback format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_correct_option_feedback}</p>]]></text> | |
| </feedback> | |
| </answer> | |
| <answer fraction="0" format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option1}</p>]]></text> | |
| <feedback format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback1}</p>]]></text> | |
| </feedback> | |
| </answer> | |
| <answer fraction="0" format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option2}</p>]]></text> | |
| <feedback format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback2}<br></p>]]></text> | |
| </feedback> | |
| </answer> | |
| <answer fraction="0" format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option3}</p>]]></text> | |
| <feedback format="html"> | |
| <text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback3}<br></p>]]></text> | |
| </feedback> | |
| </answer> | |
| </question>""" | |
| return xml_export_question | |
| def multi_mcq(query,number): | |
| prompt = "If you do not have relevant information on certain topic, put 'I do not understand this topic' in question name and 'N/A' for question text and skip remaining properties." | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": query | |
| }, | |
| ] | |
| content = "" | |
| xml = "" | |
| for x in range(number): | |
| response = openai.ChatCompletion.create( | |
| model = "gpt-4o", | |
| messages = messages, | |
| functions = function_description, | |
| function_call = {"name": "generate_moodle_mcq"}, | |
| temperature=0.5, | |
| max_tokens=4000 | |
| ) | |
| y = str(x+1) | |
| question_content = get_question_content(response) | |
| question_preview = question_content[0] + "\n\n" | |
| question_text = question_content[1][1] | |
| question_variable = question_content[1] | |
| content += "Question" + y + ":\n" + question_preview + "\n" | |
| prompt += f"\n This topic has already been generated '{question_text}', write a different topic. \n" | |
| xml += write_to_xml(question_variable) + "\n\n" | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": query | |
| }, | |
| ] | |
| prefix = """<?xml version="1.0" encoding="UTF-8"?> | |
| <quiz>""" | |
| suffix = """ | |
| </quiz>""" | |
| xml_export_quiz = prefix + xml + suffix | |
| now = dt.datetime.now() | |
| filename = f"MCQ export-{now:%Y%m%d-%H%M%S}.xml" | |
| path = "./MCQ/" + filename | |
| with open(path, "w", encoding="utf-8") as file: | |
| file.write(xml_export_quiz) | |
| return content, path | |
| def get_question_content2(response): | |
| function_call = response["choices"][0]["message"]["function_call"] | |
| function_name = function_call["name"] | |
| arguments = function_call["arguments"] | |
| qtext = eval(arguments).get("question text") | |
| if qtext == "N/A": | |
| qname = eval(arguments).get("question name") | |
| qcontent = "Name: " + qname + "\n" + "Text: " + qtext | |
| return qcontent, [qname,qtext,"N/A","N/A","N/A","N/A"] | |
| else: | |
| qname = eval(arguments).get("question name") | |
| qfdbkc = eval(arguments).get("general feedback correct") | |
| qfdbkw = eval(arguments).get("general feedback wrong") | |
| qoptc = eval(arguments).get("correct option") | |
| qoptfdbkc = eval(arguments).get("correct option feedback") | |
| qopt1 = eval(arguments).get("wrong option 1") | |
| qoptfdbk1 = eval(arguments).get("wrong option 1 feedback") | |
| qopt2 = eval(arguments).get("wrong option 2") | |
| qoptfdbk2 = eval(arguments).get("wrong option 2 feedback") | |
| qopt3 = eval(arguments).get("wrong option 3") | |
| qoptfdbk3 = eval(arguments).get("wrong option 3 feedback") | |
| qcontent = "Name: " + qname + "\n" + "Text: " + qtext + "\n" + "Correct option: " + qoptc + "\n" + "Wrong option 1: " + qopt1 + "\n" + "Wrong option 2: " + qopt2 + "\n" + "Wrong option 3: " + qopt3 | |
| return qcontent, [qname,qtext,qoptc,qopt1,qopt2,qopt3] | |
| def inspera_multi_mcq(query,number): | |
| prompt = "If you do not have relevant information on certain topic, put 'I don't know this topic' in question name and 'N/A' for question text and skip remaining properties." | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": query | |
| }, | |
| ] | |
| Column_header = ['QuestionType','QuestionName','QuestionText','Randomize','OptionText','IsCorrect','OptionText','IsCorrect','OptionText','IsCorrect','OptionText','IsCorrect'] | |
| question_list = np.array([Column_header]) | |
| content = "" | |
| for x in range(number): | |
| response = openai.ChatCompletion.create( | |
| model = "gpt-4o", | |
| messages = messages, | |
| functions = function_description, | |
| function_call = {"name": "generate_moodle_mcq"}, | |
| temperature=0.5, | |
| max_tokens=4000 | |
| ) | |
| y = str(x+1) | |
| question_content = get_question_content2(response) | |
| question_preview = question_content[0] + "\n\n" | |
| question_text = question_content[1][1] | |
| question_variable = question_content[1] | |
| content += "Question" + y + ":\n" + question_preview + "\n" | |
| prompt += f"\n This topic has already been generated '{question_text}', write a different topic. \n" | |
| refined_question_variable = ['multiple_choice',question_variable[0],question_variable[1],'1','<p>'+question_variable[2]+'</p>','1','<p>'+question_variable[3]+'</p>', '','<p>'+question_variable[4]+'</p>', '','<p>'+question_variable[5]+'</p>', ''] | |
| question_list = np.vstack([question_list,refined_question_variable]) | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": query | |
| }, | |
| ] | |
| question_list = question_list.tolist() | |
| now = dt.datetime.now() | |
| filename = f"Inspera MCQ export-{now:%Y%m%d-%H%M%S}.xlsx" | |
| path = "./Inspera/" + filename | |
| wb = Workbook() | |
| ws = wb.active | |
| ws.title = "questions" | |
| for row in question_list: | |
| ws.append(row) | |
| wb.save(filename=path) | |
| return content, path |