rootacess commited on
Commit
076bdf6
1 Parent(s): 4a519e2

Performance improvement for TA + mathprompter

Browse files
Files changed (4) hide show
  1. TA.py +23 -18
  2. app.py +10 -2
  3. mathprompter.py +3 -3
  4. prompt.py +4 -18
TA.py CHANGED
@@ -27,27 +27,32 @@ def solve_ta(question, token):
27
  query = query.strip()
28
  query += "\n"
29
  code = generate_response(query, 0.9, token)
30
-
 
 
31
  splitting_string = "```" if "```python" not in code else "```python"
32
- code = code.split(splitting_string)[-2].split("```")[0].strip()
33
- print(code)
34
- # code preprocessing
35
-
36
- code = post_process_code(code, question)
37
- print(code)
38
- # code running
39
- if "input(" in code:
40
- return None, code
41
- pred = None
42
- try:
43
- pred = run_code(code)
44
- except Exception as ex:
45
- return None, code
46
- return pred, code
 
 
47
 
48
 
49
  if __name__ == "__main__":
50
 
51
- q = "What is the 7th Fibonacci number? Write the Python code"
52
- print(solve_ta(q))
 
53
 
27
  query = query.strip()
28
  query += "\n"
29
  code = generate_response(query, 0.9, token)
30
+ n = len(TA_prompt.strip())
31
+ code = code[n:].strip().split("-----")[0]
32
+ # print(code)
33
  splitting_string = "```" if "```python" not in code else "```python"
34
+ if "```" in code:
35
+ code = code.split(splitting_string)[1].split("```")[0].strip()
36
+ # code preprocessing
37
+ code = post_process_code(code, question)
38
+ print(code)
39
+ # code running
40
+ if "input(" in code:
41
+ return None, code
42
+ pred = None
43
+ try:
44
+ pred = run_code(code)
45
+ except Exception as ex:
46
+ return None, code
47
+ return pred, code
48
+ else:
49
+ res = re.findall(r"Assistant:(.*)", code)[0]
50
+ return res, ""
51
 
52
 
53
  if __name__ == "__main__":
54
 
55
+ q = "What is the smallest even prime number?"
56
+ # q = "Write the code to find the 1st even prime number"
57
+ print(solve_ta(q, "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"))
58
 
app.py CHANGED
@@ -42,6 +42,8 @@ def run(token, question, method):
42
 
43
 
44
  def run_edits(code):
 
 
45
  try:
46
  code_op = run_code(code)
47
  return code_op, code
@@ -90,10 +92,10 @@ with demo:
90
 
91
  access_token = gr.Textbox(type="password", label="Hugging Face Access Token")
92
  with gr.Row():
93
- methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], interactive=True, label="Evaluation Strategies")
94
  question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")
95
 
96
- instruction = gr.Textbox(label="Instructions", visible=False, interactive=False)
97
  methods.change(fn=render_instruction, inputs=methods, outputs=instruction)
98
 
99
  question_output = gr.Textbox(label="Answer", interactive=False)
@@ -123,3 +125,9 @@ with demo:
123
  )
124
 
125
  demo.launch()
 
 
 
 
 
 
42
 
43
 
44
  def run_edits(code):
45
+ if "input(" in code:
46
+ return "Code execution failed, Please remove any input statement or bugs", code
47
  try:
48
  code_op = run_code(code)
49
  return code_op, code
92
 
93
  access_token = gr.Textbox(type="password", label="Hugging Face Access Token")
94
  with gr.Row():
95
+ methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], value="PAL",interactive=True, label="Evaluation Strategies")
96
  question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")
97
 
98
+ instruction = gr.Textbox(label="Instructions", visible=True, interactive=False, value=render_instruction("PAL")['value'])
99
  methods.change(fn=render_instruction, inputs=methods, outputs=instruction)
100
 
101
  question_output = gr.Textbox(label="Answer", interactive=False)
125
  )
126
 
127
  demo.launch()
128
+
129
+ '''
130
+ Carol was playing a trivia game. In the first round she scored 17 points and in the second round she scored 6 points. In the last round she lost 16 points. How many points did she have at the end of the game?
131
+ While on vacation, Debby took 24 pictures at the zoo and 12 at the museum. If she later deleted 14 of the pictures, how many pictures from her vacation did she still have?
132
+ I had 5 apples, I gave 3 to my brother and 4 to my uncle. How many apples I am left with?
133
+ '''
mathprompter.py CHANGED
@@ -19,7 +19,7 @@ def generate_algebric_template(question):
19
  return question, var_map
20
 
21
 
22
- def generate_algebric_expression(question, variables, param, token):
23
  question = question.strip()
24
  query = algebric_prompt.format(question=question).strip() + "\n"
25
  response = generate_response(query, param, token)
@@ -36,6 +36,7 @@ def generate_python_code(question, equation, param, token):
36
 
37
  def run(question, random_candidates, hps, token):
38
  question, var_map = generate_algebric_template(question)
 
39
 
40
  # generating the random candidates for arguments
41
  random_mapping = pd.DataFrame(columns=list(var_map.keys()))
@@ -49,8 +50,7 @@ def run(question, random_candidates, hps, token):
49
  N = len(hps)
50
  for i in range(N):
51
 
52
- variables = list(var_map.keys())
53
- expression = generate_algebric_expression(question, variables, hps[i], token)
54
  code = generate_python_code(question, expression, hps[i], token)
55
  candidates.append((expression, code))
56
  current_acc = 0
19
  return question, var_map
20
 
21
 
22
+ def generate_algebric_expression(question, param, token):
23
  question = question.strip()
24
  query = algebric_prompt.format(question=question).strip() + "\n"
25
  response = generate_response(query, param, token)
36
 
37
  def run(question, random_candidates, hps, token):
38
  question, var_map = generate_algebric_template(question)
39
+ print(question)
40
 
41
  # generating the random candidates for arguments
42
  random_mapping = pd.DataFrame(columns=list(var_map.keys()))
50
  N = len(hps)
51
  for i in range(N):
52
 
53
+ expression = generate_algebric_expression(question, hps[i], token)
 
54
  code = generate_python_code(question, expression, hps[i], token)
55
  candidates.append((expression, code))
56
  current_acc = 0
prompt.py CHANGED
@@ -1,5 +1,3 @@
1
- # todo: Improve the prompt of mathprompter too
2
-
3
  prompt = '''
4
  def solution():
5
  #Ques: For Halloween Debby and her sister combined the candy they received. Debby had 32 pieces of candy while her sister had 42. If they ate 35 pieces the first night, how many pieces do they have left?
@@ -58,10 +56,6 @@ algebric_prompt = '''
58
  Answer = (A + B) - C
59
 
60
 
61
- #Ques: Roger had A dollars. If he spent B bucks on a new game, how many C dollar toys could he buy with the money he had left?
62
- Answer = (A - B) // C
63
-
64
-
65
  #Ques: A waiter had A customers in his section. If B of them left and the rest of his tables had C people at each table, how many tables did he have?
66
  Answer = (A - B) / C
67
 
@@ -75,15 +69,15 @@ Answer = (A - B) * C
75
 
76
 
77
  python_prompt = '''
78
- #Ques: For Halloween Debby and her sister combined the candy they received. Debby had A pieces of candy while her sister had B. If they ate C pieces the first night, how many pieces do they have left?
79
  # Expression = (A + B) - C
80
 
81
- # Function for above Expression is:
82
  def solution(A, B, C):
83
  return (A + B) - C
84
 
85
 
86
- #Ques: A waiter had A customers in his section. If B of them left and the rest of his tables had C people at each table, how many tables did he have?
87
  # Expression = (A - B) / C
88
 
89
  # Function for above expression is:
@@ -91,15 +85,7 @@ def solution(A, B, C):
91
  return (A - B) / C
92
 
93
 
94
- #Ques: Roger had A dollars. If he spent B bucks on a new game, how many C dollar toys could he buy with the money he had left?
95
- # Expression = (A - B) // C
96
-
97
- # Function for above expression is:
98
- def solution(A, B, C):
99
- return (A - B) // C
100
-
101
-
102
- #Ques: There were A friends playing a video game online when B players quit. If each player left had C lives, how many lives did they have total? "
103
  # Expression = (A - B) * C
104
 
105
  # Function for above expression is:
 
 
1
  prompt = '''
2
  def solution():
3
  #Ques: For Halloween Debby and her sister combined the candy they received. Debby had 32 pieces of candy while her sister had 42. If they ate 35 pieces the first night, how many pieces do they have left?
56
  Answer = (A + B) - C
57
 
58
 
 
 
 
 
59
  #Ques: A waiter had A customers in his section. If B of them left and the rest of his tables had C people at each table, how many tables did he have?
60
  Answer = (A - B) / C
61
 
69
 
70
 
71
  python_prompt = '''
72
+ # Ques: For Halloween Debby and her sister combined the candy they received. Debby had A pieces of candy while her sister had B . If they ate C pieces the first night, how many pieces do they have left?
73
  # Expression = (A + B) - C
74
 
75
+ # Function for above Expression and Ques is:
76
  def solution(A, B, C):
77
  return (A + B) - C
78
 
79
 
80
+ # Ques: A waiter had A customers in his section. If B of them left and the rest of his tables had C people at each table, how many tables did he have?
81
  # Expression = (A - B) / C
82
 
83
  # Function for above expression is:
85
  return (A - B) / C
86
 
87
 
88
+ # Ques: There were A friends playing a video game online when B more players joined the game. If each player had C lives, how many lives did they have total?
 
 
 
 
 
 
 
 
89
  # Expression = (A - B) * C
90
 
91
  # Function for above expression is: