JasonData/MathGenerator
3
1import openai2import gradio as gr3import os4 5STARTING_PROMPT = [{"role": "user", "content": """You are a math question generator. For each question, I will provide you with 4 things: 6 1. the main topic to be tested, 2. the types of question type, 3. the difficulty level, and 4. the required skillsets to solve the question. 7 You will then reply with appropriate math question as well as the step by step solution for the question. Reply in Four parts. 8 1. Question Information: 9 Topic(s) Tested: ...10 Question Type: ...11 Difficulty Level: ...12 Skills required: ...13 Case Study: True/False14 15 2. Question: .... 16 17 3. Step by Step Solution: ... 18 19 4. Final answer(s): ..."""},20 {"role": "assistant", "content": f"OK"}]21 22openai.api_key = os.environ['OPENAI']23 24 25def predict(input, msg_history=STARTING_PROMPT):26 msg_history.append({"role": "user", "content": f"{input}"})27 print(msg_history)28 29 completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=msg_history, temperature=0.8)30 response = completion.choices[0].message.content 31 msg_history.append({"role": "assistant", "content": f"{response}"}) 32 33 return [response, msg_history]34 35 36def prompt_builder_predict(questionType=None, difficulty=0, topic=None, prerequisites=None, caseStudy=False, additionalPrompt=None, msg_history=STARTING_PROMPT, latex=False):37 38 level = ['Very Easy', 'Easy', 'Medium', 'Difficult', 'Extremely Difficult']39 prompt = 'randomly generatate a math question '40 if topic:41 prompt = prompt + f'on the topic of {topic}. '42 if difficulty:43 prompt = prompt + f'The difficulty level of the question should be: {level[difficulty-1]}, which means that it must require at least {difficulty} steps to solve. '44 if questionType:45 prompt = prompt + f'The question type should be in {questionType} format. '46 if prerequisites:47 prompt = prompt + f"This question will require to use the following methods to solve: {' and '.join(prerequisites)}. "48 if caseStudy:49 prompt = prompt + 'This question must be in the form of case study where it tries to test the application of the topic in the real life scenario. '50 if latex:51 prompt = prompt + 'Display all mathematical equation parts of the question to LaTeX format. ' 52 if additionalPrompt:53 prompt = prompt + f"In addition, {additionalPrompt}."54 55 return predict(prompt, msg_history)56 57 58with gr.Blocks() as demo:59 60 msg_history = gr.State(STARTING_PROMPT)61 62 gr.Markdown(63 """64 # Math Question Generator65 This webapp demostrates an API plugin that can be used with LearningANTs to generate questions. The response will contain three parts: [Question, Step by Step Solution, Final answer].66 """)67 68 with gr.Row():69 questionType = gr.Radio(["MCQ", "True or False", "Short Response"], value='Short Response', label="Question Type")70 difficulty = gr.Slider(1, 5, value=3, step=1, label="Difficult Level", info="Choose between 1 and 5")71 with gr.Row():72 topic = gr.Dropdown(["Simultaneous Equation", "Linear Equation", "Derivatives", "Integrals", "Optimization"], value='Simultaneous Equation', label="Main Testing Topic")73 prerequisites = gr.Dropdown(["Elimination", "Subsitution", "Linear Equation", "Algebra", "Geometry", "Trigonometry", "Logarithms", "Power Rule", "Sum Rule", 'Difference Rule', "Product Rule", "Quotient Rule", 'Reciprocal Rule', "Chain Rule", "Implicit Differentiation", "Logarithmic Differentiation"], multiselect=True, interactive=True, label="Prerequisite Topics")74 75 with gr.Row():76 caseStudy = gr.Checkbox(label="Case Study", info="Does this question test the application of theory in real life scenarios?")77 latex = gr.Checkbox(label="LaTeX", value=True, info="Display all equations in LaTeX format?")78 79 additionalInfo = gr.Textbox(label="Additional information (prompt)", placeholder="Give a scenario where Jim and John are working in a garden....") 80 81 gen_btn = gr.Button("Generate A New Question")82 83 with gr.Row():84 question = gr.TextArea(label="Generated Question")85 86 gen_btn.click(fn=prompt_builder_predict, inputs = [questionType, difficulty, topic, prerequisites, caseStudy, additionalInfo, msg_history, latex], outputs= [question, msg_history])87 88 with gr.Row():89 prompt = gr.Textbox(label='Additional Prompt', info='Not satified with the result? Enter instructions to modify the question.', placeholder='Include the case study of....', visible=False) 90 91 with gr.Row():92 modify_btn = gr.Button('Modify Question', visible=False)93 modify_btn.click(fn=predict, inputs = [prompt, msg_history], outputs= [question, msg_history])94 95 96 # restart_btn = gr.Button("Generate Another Question", visible=False)97 98 99 def show_display():100 return gr.update(visible=True) 101 def hide_display():102 return gr.update(visible=False) 103 def clear_value():104 return gr.update(value='')105 106 question.change(fn=show_display, outputs=prompt)107 question.change(fn=show_display, outputs=modify_btn)108 109demo.launch( share=False)