Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
STARTING_PROMPT = [{"role": "user", "content": """You are a math question generator. For each question, I will provide you with 4 things:
|
| 5 |
+
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.
|
| 6 |
+
You will then reply with appropriate math question as well as the step by step solution for the question. Reply in Four parts.
|
| 7 |
+
1. Question Information:
|
| 8 |
+
Topic(s) Tested: ...
|
| 9 |
+
Question Type: ...
|
| 10 |
+
Difficulty Level: ...
|
| 11 |
+
Skills required: ...
|
| 12 |
+
Case Study: True/False
|
| 13 |
+
|
| 14 |
+
2. Question: ....
|
| 15 |
+
|
| 16 |
+
3. Step by Step Solution: ...
|
| 17 |
+
|
| 18 |
+
4. Final answer(s): ..."""},
|
| 19 |
+
{"role": "assistant", "content": f"OK"}]
|
| 20 |
+
|
| 21 |
+
API_KEY = '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'
|
| 22 |
+
openai.api_key = API_KEY
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def 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 |
+
|
| 36 |
+
def prompt_builder_predict(questionType=None, difficulty=0, topic=None, prerequisites=None, caseStudy=False, additionalPrompt=None, msg_history=STARTING_PROMPT):
|
| 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 additionalPrompt:
|
| 51 |
+
prompt = prompt + f"In addition, {additionalPrompt}."
|
| 52 |
+
|
| 53 |
+
return predict(prompt, msg_history)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
|
| 58 |
+
msg_history = gr.State(STARTING_PROMPT)
|
| 59 |
+
|
| 60 |
+
gr.Markdown(
|
| 61 |
+
"""
|
| 62 |
+
# Math Question Generator
|
| 63 |
+
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].
|
| 64 |
+
""")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
questionType = gr.Radio(["MCQ", "True or False", "Short Response"], value='Short Response', label="Question Type")
|
| 68 |
+
difficulty = gr.Slider(1, 5, value=3, step=1, label="Difficult Level", info="Choose between 1 and 5")
|
| 69 |
+
with gr.Row():
|
| 70 |
+
topic = gr.Dropdown(["Simultaneous Equation", "Linear Equation", "Derivatives", "Integrals", "Optimization"], value='Simultaneous Equation', label="Main Testing Topic")
|
| 71 |
+
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")
|
| 72 |
+
|
| 73 |
+
caseStudy = gr.Checkbox(label="Case Study", info="Does this question test the application of theory in real life scenarios?")
|
| 74 |
+
additionalInfo = gr.Textbox(label="Additional information (prompt)", placeholder="Give a scenario where Jim and John are working in a garden....")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
gen_btn = gr.Button("Generate A New Question")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
question = gr.TextArea(label="Generated Question")
|
| 81 |
+
|
| 82 |
+
gen_btn.click(fn=prompt_builder_predict, inputs = [questionType, difficulty, topic, prerequisites, caseStudy, additionalInfo, msg_history], outputs= [question, msg_history])
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
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)
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
modify_btn = gr.Button('Modify Question', visible=False)
|
| 89 |
+
modify_btn.click(fn=predict, inputs = [prompt, msg_history], outputs= [question, msg_history])
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# restart_btn = gr.Button("Generate Another Question", visible=False)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def show_display():
|
| 96 |
+
return gr.update(visible=True)
|
| 97 |
+
def hide_display():
|
| 98 |
+
return gr.update(visible=False)
|
| 99 |
+
def clear_value():
|
| 100 |
+
return gr.update(value='')
|
| 101 |
+
|
| 102 |
+
question.change(fn=show_display, outputs=prompt)
|
| 103 |
+
question.change(fn=show_display, outputs=modify_btn)
|
| 104 |
+
|
| 105 |
+
demo.launch( share=False)
|