fixed auth
Browse files- TA.py +2 -11
- app.py +5 -14
- mathprompter.py +9 -10
- pal.py +2 -7
- utils.py +3 -2
TA.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
from prompt import TA_prompt
|
4 |
import re
|
5 |
from utils import generate_response, run_code
|
@@ -22,13 +20,13 @@ def post_process_code(code, question):
|
|
22 |
return code
|
23 |
|
24 |
|
25 |
-
def solve_ta(question
|
26 |
question = question.strip()
|
27 |
question = "Human: " + question
|
28 |
query = TA_prompt + question
|
29 |
query = query.strip()
|
30 |
query += "\n"
|
31 |
-
code = generate_response(query, 0.9
|
32 |
n = len(TA_prompt.strip())
|
33 |
code = code[n:].strip().split("-----")[0]
|
34 |
# print(code)
|
@@ -51,10 +49,3 @@ def solve_ta(question, token):
|
|
51 |
res = re.findall(r"Assistant:(.*)", code, re.DOTALL)[0].split("Human:")[0]
|
52 |
return res.strip(), ""
|
53 |
|
54 |
-
|
55 |
-
if __name__ == "__main__":
|
56 |
-
|
57 |
-
q = "What is the smallest even prime number?"
|
58 |
-
# q = "Write the code to find the 1st even prime number"
|
59 |
-
print(solve_ta(q, os.environ.get("HF_TOKEN")))
|
60 |
-
|
|
|
|
|
|
|
1 |
from prompt import TA_prompt
|
2 |
import re
|
3 |
from utils import generate_response, run_code
|
|
|
20 |
return code
|
21 |
|
22 |
|
23 |
+
def solve_ta(question):
|
24 |
question = question.strip()
|
25 |
question = "Human: " + question
|
26 |
query = TA_prompt + question
|
27 |
query = query.strip()
|
28 |
query += "\n"
|
29 |
+
code = generate_response(query, 0.9)
|
30 |
n = len(TA_prompt.strip())
|
31 |
code = code[n:].strip().split("-----")[0]
|
32 |
# print(code)
|
|
|
49 |
res = re.findall(r"Assistant:(.*)", code, re.DOTALL)[0].split("Human:")[0]
|
50 |
return res.strip(), ""
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -5,11 +5,9 @@ from TA import solve_ta
|
|
5 |
from utils import run_code
|
6 |
|
7 |
|
8 |
-
def run(
|
9 |
-
if len(token) == 0:
|
10 |
-
raise gr.Error("Please provide a valid Hugging Face access token")
|
11 |
if method == "PAL":
|
12 |
-
code_op, generated_code = solve_pal(question
|
13 |
if code_op is not None:
|
14 |
return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)
|
15 |
else:
|
@@ -18,7 +16,7 @@ def run(token, question, method):
|
|
18 |
gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))
|
19 |
|
20 |
elif method == "TA":
|
21 |
-
code_op, generated_code = solve_ta(question
|
22 |
if code_op is not None:
|
23 |
return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)
|
24 |
else:
|
@@ -27,7 +25,7 @@ def run(token, question, method):
|
|
27 |
gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))
|
28 |
|
29 |
elif method == "MathPrompter":
|
30 |
-
exp_op, code_op, generated_code, generated_exp = solve_mp(question
|
31 |
display_value = generated_exp + "\n\n" + generated_code
|
32 |
if code_op is not None:
|
33 |
ans = f"Answer from Expression execution: {exp_op} \nAnswer from Code execution: {code_op} "
|
@@ -104,7 +102,6 @@ In addition, the space enables users to edit the generated code and re-run it, p
|
|
104 |
|
105 |
with demo:
|
106 |
gr.Markdown(description, interactive=False)
|
107 |
-
access_token = gr.Textbox(type="password", label="Hugging Face Access Token")
|
108 |
with gr.Row():
|
109 |
methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], value="PAL",interactive=True, label="Evaluation Strategies")
|
110 |
question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")
|
@@ -117,7 +114,7 @@ with demo:
|
|
117 |
submit_btn = gr.Button("Submit")
|
118 |
edit_btn = gr.Button("Run the edited code", visible=False)
|
119 |
|
120 |
-
submit_btn.click(run, inputs=[
|
121 |
edit_btn.click(run_edits, inputs=code, outputs=[question_output, code])
|
122 |
gr.Examples(
|
123 |
examples=[
|
@@ -140,9 +137,3 @@ with demo:
|
|
140 |
)
|
141 |
|
142 |
demo.launch()
|
143 |
-
|
144 |
-
'''
|
145 |
-
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?
|
146 |
-
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?
|
147 |
-
I had 5 apples, I gave 3 to my brother and 4 to my uncle. How many apples I am left with?
|
148 |
-
'''
|
|
|
5 |
from utils import run_code
|
6 |
|
7 |
|
8 |
+
def run(question, method):
|
|
|
|
|
9 |
if method == "PAL":
|
10 |
+
code_op, generated_code = solve_pal(question)
|
11 |
if code_op is not None:
|
12 |
return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)
|
13 |
else:
|
|
|
16 |
gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))
|
17 |
|
18 |
elif method == "TA":
|
19 |
+
code_op, generated_code = solve_ta(question)
|
20 |
if code_op is not None:
|
21 |
return code_op, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)
|
22 |
else:
|
|
|
25 |
gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))
|
26 |
|
27 |
elif method == "MathPrompter":
|
28 |
+
exp_op, code_op, generated_code, generated_exp = solve_mp(question)
|
29 |
display_value = generated_exp + "\n\n" + generated_code
|
30 |
if code_op is not None:
|
31 |
ans = f"Answer from Expression execution: {exp_op} \nAnswer from Code execution: {code_op} "
|
|
|
102 |
|
103 |
with demo:
|
104 |
gr.Markdown(description, interactive=False)
|
|
|
105 |
with gr.Row():
|
106 |
methods = gr.Dropdown(choices=['PAL', 'TA', 'MathPrompter'], value="PAL",interactive=True, label="Evaluation Strategies")
|
107 |
question_input = gr.Textbox(label="Question", lines=1, placeholder="Enter your question here...")
|
|
|
114 |
submit_btn = gr.Button("Submit")
|
115 |
edit_btn = gr.Button("Run the edited code", visible=False)
|
116 |
|
117 |
+
submit_btn.click(run, inputs=[question_input, methods], outputs=[question_output, code, edit_btn])
|
118 |
edit_btn.click(run_edits, inputs=code, outputs=[question_output, code])
|
119 |
gr.Examples(
|
120 |
examples=[
|
|
|
137 |
)
|
138 |
|
139 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
mathprompter.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import re
|
@@ -19,22 +18,22 @@ def generate_algebric_template(question):
|
|
19 |
return question, var_map
|
20 |
|
21 |
|
22 |
-
def generate_algebric_expression(question, param
|
23 |
question = question.strip()
|
24 |
query = algebric_prompt.format(question=question).strip() + "\n"
|
25 |
-
response = generate_response(query, param
|
26 |
expression = response.split(f"#Ques: {question}")[-1].strip()
|
27 |
return expression.split("Answer = ")[-1]
|
28 |
|
29 |
|
30 |
-
def generate_python_code(question, equation, param
|
31 |
query = python_prompt.format(question=question.strip(), expression=equation.strip()).strip() + "\n"
|
32 |
-
response = generate_response(query, param
|
33 |
function_code = response.split("# Function for above expression is:")[-1].strip()
|
34 |
return function_code
|
35 |
|
36 |
|
37 |
-
def run(question, random_candidates, hps
|
38 |
question, var_map = generate_algebric_template(question)
|
39 |
|
40 |
# generating the random candidates for arguments
|
@@ -49,8 +48,8 @@ def run(question, random_candidates, hps, token):
|
|
49 |
N = len(hps)
|
50 |
for i in range(N):
|
51 |
|
52 |
-
expression = generate_algebric_expression(question, hps[i]
|
53 |
-
code = generate_python_code(question, expression, hps[i]
|
54 |
candidates.append((expression, code))
|
55 |
current_acc = 0
|
56 |
|
@@ -99,9 +98,9 @@ def run(question, random_candidates, hps, token):
|
|
99 |
return top_candidate, var_map
|
100 |
|
101 |
|
102 |
-
def solve_mp(question
|
103 |
hps = [0.9, 0.95]
|
104 |
-
(expression, code), var_map = run(question, 5, hps
|
105 |
exp_op = None
|
106 |
code_op = None
|
107 |
try:
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
import re
|
|
|
18 |
return question, var_map
|
19 |
|
20 |
|
21 |
+
def generate_algebric_expression(question, param):
|
22 |
question = question.strip()
|
23 |
query = algebric_prompt.format(question=question).strip() + "\n"
|
24 |
+
response = generate_response(query, param)
|
25 |
expression = response.split(f"#Ques: {question}")[-1].strip()
|
26 |
return expression.split("Answer = ")[-1]
|
27 |
|
28 |
|
29 |
+
def generate_python_code(question, equation, param):
|
30 |
query = python_prompt.format(question=question.strip(), expression=equation.strip()).strip() + "\n"
|
31 |
+
response = generate_response(query, param)
|
32 |
function_code = response.split("# Function for above expression is:")[-1].strip()
|
33 |
return function_code
|
34 |
|
35 |
|
36 |
+
def run(question, random_candidates, hps):
|
37 |
question, var_map = generate_algebric_template(question)
|
38 |
|
39 |
# generating the random candidates for arguments
|
|
|
48 |
N = len(hps)
|
49 |
for i in range(N):
|
50 |
|
51 |
+
expression = generate_algebric_expression(question, hps[i])
|
52 |
+
code = generate_python_code(question, expression, hps[i])
|
53 |
candidates.append((expression, code))
|
54 |
current_acc = 0
|
55 |
|
|
|
98 |
return top_candidate, var_map
|
99 |
|
100 |
|
101 |
+
def solve_mp(question):
|
102 |
hps = [0.9, 0.95]
|
103 |
+
(expression, code), var_map = run(question, 5, hps)
|
104 |
exp_op = None
|
105 |
code_op = None
|
106 |
try:
|
pal.py
CHANGED
@@ -12,10 +12,10 @@ def post_process_code(code, question):
|
|
12 |
return list(zip(parameters, values))
|
13 |
|
14 |
|
15 |
-
def solve_pal(question
|
16 |
question = question.strip()
|
17 |
query = prompt.format(question=question).strip()
|
18 |
-
code = generate_response(query, 0.9
|
19 |
code = code.split("def solution():")[-1].strip()
|
20 |
code = "def solution():\n" + code
|
21 |
# code preprocessing
|
@@ -38,8 +38,3 @@ def solve_pal(question, token):
|
|
38 |
return pred, code
|
39 |
|
40 |
|
41 |
-
if __name__ == "__main__":
|
42 |
-
|
43 |
-
q = "What is the 7th Fibonacci number?"
|
44 |
-
print(solve_pal(q))
|
45 |
-
|
|
|
12 |
return list(zip(parameters, values))
|
13 |
|
14 |
|
15 |
+
def solve_pal(question):
|
16 |
question = question.strip()
|
17 |
query = prompt.format(question=question).strip()
|
18 |
+
code = generate_response(query, 0.9)
|
19 |
code = code.split("def solution():")[-1].strip()
|
20 |
code = "def solution():\n" + code
|
21 |
# code preprocessing
|
|
|
38 |
return pred, code
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
utils.py
CHANGED
@@ -6,8 +6,9 @@ import gradio
|
|
6 |
import os
|
7 |
|
8 |
|
9 |
-
def generate_response(query, top_p
|
10 |
-
endpoint =
|
|
|
11 |
params = {
|
12 |
'accept': 'application/json',
|
13 |
'Content-Type': 'application/json',
|
|
|
6 |
import os
|
7 |
|
8 |
|
9 |
+
def generate_response(query, top_p):
|
10 |
+
endpoint = os.environ.get("ENDPOINT", None)
|
11 |
+
token = os.environ.get("TOKEN", None)
|
12 |
params = {
|
13 |
'accept': 'application/json',
|
14 |
'Content-Type': 'application/json',
|