M-1: PAL, TA, MP
Browse files- TA.py +53 -0
- app.py +125 -0
- mathprompter.py +142 -0
- pal.py +45 -0
- prompt.py +343 -0
- requirements.txt +73 -0
- utils.py +43 -0
TA.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from prompt import TA_prompt
|
2 |
+
import re
|
3 |
+
from utils import generate_response, run_code
|
4 |
+
|
5 |
+
|
6 |
+
def post_process_code(code, question):
|
7 |
+
func_name = code.split("(")[0].split("def")[-1].strip()
|
8 |
+
parameters = code.split("\n")[0].split(f"def {func_name}")[-1][1:-2].split(",")
|
9 |
+
if '' in parameters:
|
10 |
+
parameters.remove('')
|
11 |
+
values = re.findall(r"[-+]?\d*\.\d+|\d+", question)[:len(parameters)]
|
12 |
+
values = [int(v) for v in values]
|
13 |
+
arguments = list(zip(parameters, values))
|
14 |
+
|
15 |
+
arg_string = ""
|
16 |
+
for param, val in arguments:
|
17 |
+
arg_string += f"{param}={val},"
|
18 |
+
func_call = f"\nprint({func_name}({arg_string[:-1]}))"
|
19 |
+
code += func_call
|
20 |
+
return code
|
21 |
+
|
22 |
+
|
23 |
+
def solve_ta(question, token):
|
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, 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 |
+
|
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pal import solve_pal
|
3 |
+
from mathprompter import solve_mp
|
4 |
+
from TA import solve_ta
|
5 |
+
from utils import run_code
|
6 |
+
|
7 |
+
|
8 |
+
def run(token, question, method):
|
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, token)
|
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:
|
16 |
+
return (
|
17 |
+
"Code execution failed, please review it from below and re-run it or try-asking again with more details",
|
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, token)
|
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:
|
25 |
+
return (
|
26 |
+
"Code execution failed, please review it from below and re-run it or try-asking again with more details",
|
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 = solve_mp(question, token)
|
31 |
+
|
32 |
+
if code_op is not None:
|
33 |
+
ans = f"Answer from Expression execution: {exp_op} \nAnswer from Code execution: {code_op} "
|
34 |
+
return ans, gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True)
|
35 |
+
else:
|
36 |
+
return (
|
37 |
+
"Code execution failed, please review it from below and re-run it or try-asking again with more details",
|
38 |
+
gr.Code.update(value=generated_code, interactive=True), gr.Button.update(visible=True))
|
39 |
+
|
40 |
+
else:
|
41 |
+
raise gr.Error("Please select the evaluating strategy from dropdown")
|
42 |
+
|
43 |
+
|
44 |
+
def run_edits(code):
|
45 |
+
try:
|
46 |
+
code_op = run_code(code)
|
47 |
+
return code_op, code
|
48 |
+
except:
|
49 |
+
return "Code execution failed, please review it from below and re-run it or try-asking again with more details", code
|
50 |
+
|
51 |
+
|
52 |
+
# User Interface Part
|
53 |
+
|
54 |
+
TITLE = "Reasoning with StarCoder 💫"
|
55 |
+
demo = gr.Blocks(title=TITLE, theme=gr.themes.Default())
|
56 |
+
|
57 |
+
|
58 |
+
def render_instruction(mtd):
|
59 |
+
if mtd == "PAL":
|
60 |
+
return gr.Textbox.update(
|
61 |
+
value='''
|
62 |
+
💫 Query can be an instruction or a direct question starting with What, Find, etc.
|
63 |
+
💫 Use numbers wherever possible, i.e use 2 instead of two
|
64 |
+
💫 Example: What is the value of sin(30)?
|
65 |
+
''',
|
66 |
+
visible=True
|
67 |
+
)
|
68 |
+
if mtd == "TA":
|
69 |
+
return gr.Textbox.update(
|
70 |
+
value='''
|
71 |
+
💫 Query should be a direct instruction starting with *write the code* and the main question.
|
72 |
+
💫 Use numbers wherever possible, i.e use 2 instead of two
|
73 |
+
💫 Example: Write the code to find 7th Fibonacci number.
|
74 |
+
''',
|
75 |
+
visible=True
|
76 |
+
)
|
77 |
+
if mtd == "MathPrompter":
|
78 |
+
return gr.Textbox.update(
|
79 |
+
value='''
|
80 |
+
💫 Query should be a direct question, can start by giving a context and then asking the intended segment.
|
81 |
+
💫 Use numbers wherever possible, i.e use 2 instead of two
|
82 |
+
💫 Example: The dimensions of the input image are 80x80, if the filter of 3x3 size is convoluted on the image, what are the dimensions of output image?
|
83 |
+
''',
|
84 |
+
visible=True
|
85 |
+
)
|
86 |
+
|
87 |
+
|
88 |
+
with demo:
|
89 |
+
gr.HTML(f"<center><h1>{TITLE}<h1></center>")
|
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)
|
100 |
+
code = gr.Code(language="python", interactive=False, label="Generated Code (Editable)")
|
101 |
+
submit_btn = gr.Button("Submit")
|
102 |
+
edit_btn = gr.Button("Run the edited code", visible=False)
|
103 |
+
|
104 |
+
submit_btn.click(run, inputs=[access_token, question_input, methods], outputs=[question_output, code, edit_btn])
|
105 |
+
edit_btn.click(run_edits, inputs=code, outputs=[question_output, code])
|
106 |
+
gr.Examples(
|
107 |
+
examples=[
|
108 |
+
[
|
109 |
+
"The dimensions of the input image are 80x80, if the filter of 3x3 size is convoluted on the image, what are the dimensions of output image?",
|
110 |
+
"PAL"],
|
111 |
+
[
|
112 |
+
"The dimensions of the input image are 80x80, if the filter of 3x3 size is convoluted on the image, what are the dimensions of output image?",
|
113 |
+
"MathPrompter"],
|
114 |
+
["What is the value of sin(30)?", "PAL"],
|
115 |
+
["Write the code to find 7th Fibonacci number.", "TA"],
|
116 |
+
["Write a program to filter all the odd numbers from a python list", "PAL"],
|
117 |
+
],
|
118 |
+
inputs=[question_input, methods],
|
119 |
+
outputs=[question_output, code, edit_btn],
|
120 |
+
fn=run,
|
121 |
+
cache_examples=False,
|
122 |
+
label="Sample Questions",
|
123 |
+
)
|
124 |
+
|
125 |
+
demo.launch()
|
mathprompter.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import re
|
5 |
+
from prompt import algebric_prompt, python_prompt
|
6 |
+
from utils import generate_response, run_code
|
7 |
+
|
8 |
+
|
9 |
+
def generate_algebric_template(question):
|
10 |
+
var_names = [chr(i) for i in range(ord('A'), ord('Z') + 1)]
|
11 |
+
pattern = re.compile(r"[-+]?\d*\.\d+|\d+")
|
12 |
+
var_map = {}
|
13 |
+
matches = re.findall(pattern, question)
|
14 |
+
|
15 |
+
for i, num in enumerate(matches):
|
16 |
+
var_name = var_names[i]
|
17 |
+
question = question.replace(num, var_name)
|
18 |
+
var_map[var_name] = float(num)
|
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)
|
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, token):
|
31 |
+
query = python_prompt.format(question=question.strip(), expression=equation.strip()).strip() + "\n"
|
32 |
+
response = generate_response(query, param, token)
|
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, 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()))
|
42 |
+
|
43 |
+
for _ in range(random_candidates):
|
44 |
+
random_mapping.loc[len(random_mapping)] = np.random.randint(1, 100, (len(random_mapping.columns),))
|
45 |
+
|
46 |
+
candidates = []
|
47 |
+
acc = []
|
48 |
+
# accumulating results
|
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
|
57 |
+
|
58 |
+
try:
|
59 |
+
for idx in range(5):
|
60 |
+
arguments = random_mapping.iloc[idx].to_list()
|
61 |
+
|
62 |
+
# running expression
|
63 |
+
exp = expression
|
64 |
+
temp_code = code
|
65 |
+
|
66 |
+
for k, v in zip(list(var_map.keys()), arguments):
|
67 |
+
exp = exp.replace(k, str(v))
|
68 |
+
exp = "print(" + exp + ")"
|
69 |
+
|
70 |
+
if "input(" in exp or "input(" in temp_code:
|
71 |
+
acc.append(0)
|
72 |
+
continue
|
73 |
+
|
74 |
+
exp_ans = run_code(exp)
|
75 |
+
|
76 |
+
# running code
|
77 |
+
parameters = temp_code.split("\n")[0].split("def solution")[-1][1:-2].split(",")
|
78 |
+
if '' in parameters:
|
79 |
+
parameters.remove('')
|
80 |
+
|
81 |
+
arguments = [(param.strip(), int(random_mapping.iloc[idx][param.strip()])) for param in parameters]
|
82 |
+
arg_string = ""
|
83 |
+
for param, val in arguments:
|
84 |
+
arg_string += f"{param}={val},"
|
85 |
+
func_call = f"\nprint(solution({arg_string[:-1]}))"
|
86 |
+
temp_code += func_call
|
87 |
+
code_ans = run_code(temp_code)
|
88 |
+
|
89 |
+
current_acc += int(exp_ans == code_ans)
|
90 |
+
|
91 |
+
# reverting the changes
|
92 |
+
exp = expression
|
93 |
+
temp_code = code
|
94 |
+
except Exception as ex:
|
95 |
+
pass
|
96 |
+
acc.append(current_acc)
|
97 |
+
|
98 |
+
candidate_index = np.argmax(acc)
|
99 |
+
top_candidate = candidates[candidate_index]
|
100 |
+
return top_candidate, var_map
|
101 |
+
|
102 |
+
|
103 |
+
def solve_mp(question, token):
|
104 |
+
hps = [0.9, 0.95]
|
105 |
+
(expression, code), var_map = run(question, 5, hps, token)
|
106 |
+
exp_op = None
|
107 |
+
code_op = None
|
108 |
+
|
109 |
+
try:
|
110 |
+
# expression output
|
111 |
+
for k, v in var_map.items():
|
112 |
+
expression = expression.replace(k, str(v))
|
113 |
+
expression = "print(" + expression + ")"
|
114 |
+
print(expression)
|
115 |
+
|
116 |
+
if "input(" in expression:
|
117 |
+
raise Exception
|
118 |
+
|
119 |
+
exp_op = run_code(expression)
|
120 |
+
except:
|
121 |
+
print("expression cannot be executed", expression)
|
122 |
+
try:
|
123 |
+
# code output
|
124 |
+
print(code)
|
125 |
+
parameters = code.split("\n")[0].split("def solution")[-1][1:-2].split(",")
|
126 |
+
if '' in parameters:
|
127 |
+
parameters.remove('')
|
128 |
+
|
129 |
+
arguments = [(param.strip(), int(var_map[param.strip()])) for param in parameters]
|
130 |
+
arg_string = ""
|
131 |
+
for param, val in arguments:
|
132 |
+
arg_string += f"{param}={val},"
|
133 |
+
func_call = f"\nprint(solution({arg_string[:-1]}))"
|
134 |
+
code += func_call
|
135 |
+
if "input(" in code:
|
136 |
+
print("code cannot be executed")
|
137 |
+
raise Exception
|
138 |
+
code_op = run_code(code)
|
139 |
+
except:
|
140 |
+
return None, None, code
|
141 |
+
|
142 |
+
return exp_op, code_op, code
|
pal.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from prompt import prompt
|
2 |
+
import re
|
3 |
+
from utils import generate_response, run_code
|
4 |
+
|
5 |
+
|
6 |
+
def post_process_code(code, question):
|
7 |
+
parameters = code.split("\n")[0].split("def solution")[-1][1:-2].split(",")
|
8 |
+
if '' in parameters:
|
9 |
+
parameters.remove('')
|
10 |
+
values = re.findall(r"[-+]?\d*\.\d+|\d+", question)[:len(parameters)]
|
11 |
+
values = [int(v) for v in values]
|
12 |
+
return list(zip(parameters, values))
|
13 |
+
|
14 |
+
|
15 |
+
def solve_pal(question, token):
|
16 |
+
question = question.strip()
|
17 |
+
query = prompt.format(question=question).strip()
|
18 |
+
code = generate_response(query, 0.9, token)
|
19 |
+
code = code.split("def solution():")[-1].strip()
|
20 |
+
code = "def solution():\n" + code
|
21 |
+
# code preprocessing
|
22 |
+
|
23 |
+
arguments = post_process_code(code, question)
|
24 |
+
|
25 |
+
arg_string = ""
|
26 |
+
for param, val in arguments:
|
27 |
+
arg_string += f"{param}={val},"
|
28 |
+
func_call = f"\nprint(solution({arg_string[:-1]}))"
|
29 |
+
code += func_call
|
30 |
+
# code running
|
31 |
+
if "input(" in code:
|
32 |
+
return None, code
|
33 |
+
pred = None
|
34 |
+
try:
|
35 |
+
pred = run_code(code)
|
36 |
+
except Exception as ex:
|
37 |
+
return None, code
|
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 |
+
|
prompt.py
ADDED
@@ -0,0 +1,343 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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?
|
6 |
+
Debby_candies = 32
|
7 |
+
sister_candies = 42
|
8 |
+
candies_ate = 35
|
9 |
+
return ((Debby_candies + sister_candies) - candies_ate)
|
10 |
+
|
11 |
+
|
12 |
+
def solution():
|
13 |
+
#Ques: What are roots of the equation x^2 - 2x + 1?
|
14 |
+
import math
|
15 |
+
a = 1
|
16 |
+
b = -2
|
17 |
+
c = 1
|
18 |
+
root1 = (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
|
19 |
+
root2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
|
20 |
+
return root1, root2
|
21 |
+
|
22 |
+
|
23 |
+
def solution():
|
24 |
+
#Ques: A waiter had 22 customers in his section. If 14 of them left and the rest of his tables had 4 people at each table, how many tables did he have?
|
25 |
+
customers = 32
|
26 |
+
customers_left = 14
|
27 |
+
each_table = 4
|
28 |
+
total_tables = (customers - customers_left) / each_table
|
29 |
+
return total_tables
|
30 |
+
|
31 |
+
|
32 |
+
def solution():
|
33 |
+
#Ques: What is the 5th number in Fibonacci sequence?
|
34 |
+
n = 5
|
35 |
+
a = 0
|
36 |
+
b = 1
|
37 |
+
if n < 0:
|
38 |
+
print("Incorrect input")
|
39 |
+
elif n == 0:
|
40 |
+
return a
|
41 |
+
elif n == 1:
|
42 |
+
return b
|
43 |
+
else:
|
44 |
+
for i in range(2, n):
|
45 |
+
c = a + b
|
46 |
+
a = b
|
47 |
+
b = c
|
48 |
+
return b
|
49 |
+
print(solution())
|
50 |
+
|
51 |
+
|
52 |
+
def solution():
|
53 |
+
#Ques: {question}
|
54 |
+
'''
|
55 |
+
|
56 |
+
algebric_prompt = '''
|
57 |
+
#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?
|
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 |
+
|
68 |
+
|
69 |
+
#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? "
|
70 |
+
Answer = (A - B) * C
|
71 |
+
|
72 |
+
|
73 |
+
#Ques: {question}
|
74 |
+
'''
|
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:
|
90 |
+
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:
|
106 |
+
def solution(A, B, C):
|
107 |
+
return (A - B) * C
|
108 |
+
|
109 |
+
|
110 |
+
#Ques: {question}
|
111 |
+
# Expression = {expression}
|
112 |
+
|
113 |
+
# Function for above expression is:
|
114 |
+
'''
|
115 |
+
|
116 |
+
TA_prompt = '''
|
117 |
+
Below are a series of dialogues between various people and an AI technical assistant. The assistant tries to be helpful, polite, honest, sophisticated, emotionally aware, and humble-but-knowledgeable. The assistant is happy to help with code questions, and will do its best to understand exactly what is needed. It also tries to avoid giving false or misleading information, and it caveats when it isn’t entirely sure about the right answer. That said, the assistant is practical and really does its best, and doesn’t let caution get too much in the way of being useful.
|
118 |
+
|
119 |
+
-----
|
120 |
+
|
121 |
+
Human: Write a function that takes two lists and returns a list that has alternating elements from each input list.
|
122 |
+
|
123 |
+
Assistant: Sure. Here is a function that does that.
|
124 |
+
|
125 |
+
```
|
126 |
+
def alternating(list1, list2):
|
127 |
+
results = []
|
128 |
+
for i in range(len(list1)):
|
129 |
+
results.append(list1[i])
|
130 |
+
results.append(list2[i])
|
131 |
+
return results
|
132 |
+
```
|
133 |
+
|
134 |
+
Human: Can you write some test cases for this function?
|
135 |
+
|
136 |
+
Assistant: Sure, here are some tests.
|
137 |
+
|
138 |
+
```
|
139 |
+
assert alternating([10, 20, 30], [1, 2, 3]) == [10, 1, 20, 2, 30, 3]
|
140 |
+
assert alternating([True, False], [4, 5]) == [True, 4, False, 5]
|
141 |
+
assert alternating([], []) == []
|
142 |
+
```
|
143 |
+
|
144 |
+
Human: Modify the function so that it returns all input elements when the lists have uneven length. The elements from the longer list should be at the end.
|
145 |
+
|
146 |
+
Assistant: Here is the modified function.
|
147 |
+
|
148 |
+
```
|
149 |
+
def alternating(list1, list2):
|
150 |
+
results = []
|
151 |
+
for i in range(min(len(list1), len(list2))):
|
152 |
+
results.append(list1[i])
|
153 |
+
results.append(list2[i])
|
154 |
+
if len(list1) > len(list2):
|
155 |
+
results.extend(list1[i+1:])
|
156 |
+
else:
|
157 |
+
results.extend(list2[i+1:])
|
158 |
+
return results
|
159 |
+
```
|
160 |
+
|
161 |
+
-----
|
162 |
+
|
163 |
+
Human: My code below is not working. Can you see what may be wrong?
|
164 |
+
|
165 |
+
```
|
166 |
+
def factorial(n):
|
167 |
+
r = 0
|
168 |
+
for i in range(n):
|
169 |
+
r *= i
|
170 |
+
return r
|
171 |
+
```
|
172 |
+
|
173 |
+
Assistant: In factorial, the initial value of `r` must be `1`. The following version should work:
|
174 |
+
|
175 |
+
```
|
176 |
+
def factorial(n):
|
177 |
+
r = 1 # Changed here
|
178 |
+
for i in range(n):
|
179 |
+
r *= i
|
180 |
+
return r
|
181 |
+
```
|
182 |
+
Human: Can you use this function to create a code to compute the number of k-combinations of a set of n elements?
|
183 |
+
|
184 |
+
Assistant: Yes, of course. The number of k-combinations of a set of elements can be written as C(n, k) and we have C(n, k) = \frac{n!}{(n-k)!k!} whenever k <= n. The code is as follows
|
185 |
+
|
186 |
+
def combination(n, k):
|
187 |
+
assert k <= n
|
188 |
+
return factorial(n)/(factorial(n-k)*factorial(k))
|
189 |
+
|
190 |
+
|
191 |
+
This should work pretty well. Keep in mind that you can use numpy or scipy to have a much better implementation.
|
192 |
+
|
193 |
+
|
194 |
+
Human: Thanks.
|
195 |
+
|
196 |
+
|
197 |
+
Assistant: You are welcome.
|
198 |
+
-----
|
199 |
+
|
200 |
+
Human: I have a CSV file that looks like this:
|
201 |
+
|
202 |
+
Name,Salary
|
203 |
+
Bob,12000
|
204 |
+
Avantika,35000
|
205 |
+
Alice,30000
|
206 |
+
|
207 |
+
Write a program that returns the name of the person with the highest salary.
|
208 |
+
|
209 |
+
Assistant: Here is a solution that assumes the filename is salaries.csv.
|
210 |
+
|
211 |
+
|
212 |
+
```
|
213 |
+
from csv import DictReader
|
214 |
+
from pathlib import Path
|
215 |
+
|
216 |
+
|
217 |
+
def max_salary(filename):
|
218 |
+
rows = DictReader(Path(filename).read_text().splitlines())
|
219 |
+
max_index = -1
|
220 |
+
for i, row in enumerate(rows):
|
221 |
+
if int(row['Salary']) > int(rows[max_index]['Salary']):
|
222 |
+
max_index = i
|
223 |
+
return rows[max_index]['Name']
|
224 |
+
|
225 |
+
|
226 |
+
print(max_salary('salaries.csv'))
|
227 |
+
```
|
228 |
+
-----
|
229 |
+
|
230 |
+
Human: What is the purpose of the code below ?
|
231 |
+
|
232 |
+
```
|
233 |
+
def f(n, p):
|
234 |
+
if p == 0:
|
235 |
+
return 1
|
236 |
+
else :
|
237 |
+
return n*f(n, p-1)
|
238 |
+
```
|
239 |
+
Assistant: This python function is designed to raise a base value n to a power p, that is, computing the value n^p where p is a positive integer.
|
240 |
+
|
241 |
+
-----
|
242 |
+
|
243 |
+
Human: Write a program that returns the prime numbers less than 1000 that are congruent to 1 modulo 6.
|
244 |
+
|
245 |
+
Assistant : The method I will adopt to solve this problem is to go through all the integers less than 1000 and check if they are prime and congruent to 1 modulo 6. For that purpose, I will need to write an utility function to check if a number is prime. Here it is:
|
246 |
+
|
247 |
+
```
|
248 |
+
def is_prime(p):
|
249 |
+
if p <= 1:
|
250 |
+
return False
|
251 |
+
else :
|
252 |
+
for i in range(2, int(p**0.5)+1):
|
253 |
+
if p%i == 0 :
|
254 |
+
return False
|
255 |
+
return True
|
256 |
+
```
|
257 |
+
Now, here is the final solution:
|
258 |
+
|
259 |
+
```
|
260 |
+
def primes_mod_6():
|
261 |
+
primes = []
|
262 |
+
for n in range(1000):
|
263 |
+
if is_prime(n) and n%6 == 1 :
|
264 |
+
primes.append(n)
|
265 |
+
return primes
|
266 |
+
```
|
267 |
+
|
268 |
+
-----
|
269 |
+
|
270 |
+
Human: Can you translate the code below in Java?
|
271 |
+
```
|
272 |
+
print(“Hello world”)
|
273 |
+
```
|
274 |
+
Assistant: Sure, the Java translation of your code is as follows.
|
275 |
+
```
|
276 |
+
System.out.println(“Hello world”);
|
277 |
+
```
|
278 |
+
|
279 |
+
-----
|
280 |
+
|
281 |
+
Human: The bakers at the Beverly Hills Bakery baked 200 loaves of bread on Monday morning. They sold 93 loaves in the morning and 39 loaves in the afternoon. A grocery store returned 6 unsold loaves. How many loaves of bread did they have left?
|
282 |
+
|
283 |
+
Assistant: Let’s think step by step. The bakers started with 200 loaves of bread. They sold 93 in the morning and 39 in the afternoon. So they sold 93+39=132 loaves of bread. Which means they had 200 - 132 = 68 loaves left. Finally, a grocery returned 6 unsold loaves to the store. Thus, they ended up with 68+6=74 loaves. The answer is 74.
|
284 |
+
|
285 |
+
Human: Nice. Can you write a python function to solve this problem? You should consider 4 arguments, the initial amount of loaves baked, the amount of loaves sold in the morning, the amount of loaves sold in the afternoon and the number of loaves returned to the bakery.
|
286 |
+
|
287 |
+
Assistant: Sure, here is the code.
|
288 |
+
|
289 |
+
```
|
290 |
+
def loaves_left(initial_number_of_loaves, loaves_sold_morning, loaves_sold_afternoon, loaves_returned):
|
291 |
+
loaves_sold_total = loaves_sold_morning + loaves_sold_afternoon
|
292 |
+
return initial_number_of_loaves - loaves_sold_total + loaves_returned
|
293 |
+
```
|
294 |
+
Human: Can you test your function with the following inputs?
|
295 |
+
24, 5, 9, 3
|
296 |
+
153, 57, 99, 32
|
297 |
+
|
298 |
+
Assistant: Definitely, we have:
|
299 |
+
loaves_left(24, 5, 9, 3) = 13
|
300 |
+
loaves_left(153, 57, 99, 32) = 29
|
301 |
+
|
302 |
+
-----
|
303 |
+
|
304 |
+
Human: Can you write a python function to randomly decide between eating a pizza or a burger with probability 0.3 and 0.7 respectively?
|
305 |
+
|
306 |
+
```
|
307 |
+
import random
|
308 |
+
def pizza_or_burger():
|
309 |
+
p = random.uniform(0, 1)
|
310 |
+
if p <= 0.3:
|
311 |
+
print(“Let’s eat a pizza for lunch.”)
|
312 |
+
else:
|
313 |
+
print(“Let’s eat a burger for lunch.”)
|
314 |
+
```
|
315 |
+
|
316 |
+
-----
|
317 |
+
|
318 |
+
Human: I have a dictionary with lots of dictionaries inside it, and so on, how can I make that into just one single dictionary?
|
319 |
+
|
320 |
+
Assistant: Are you talking about dictionaries in the context of python programming?
|
321 |
+
|
322 |
+
Human: Yeah these are dictionaries with keys and values, and the values themselves are dictionaries.
|
323 |
+
|
324 |
+
Assistant: Great, I think I understand what you’re asking. We might say that we want to flatten the dictionaries, right? And if there are several levels, we’ll want to do this recursively.
|
325 |
+
|
326 |
+
But to do this we will need to combine the keys from the outer dictionary with the keys from the inner dictionary, so that the final resulting keys are unambiguous. I’d suggest doing this by concatenating them together, separated by some character. Will that work for you, and what character would you like to use?
|
327 |
+
|
328 |
+
```
|
329 |
+
def recursively_flatten_dicts(d, separator="_"):
|
330 |
+
result = {}
|
331 |
+
for k, v in d.items():
|
332 |
+
if isinstance(v, dict):
|
333 |
+
new_dict = recursively_flatten_dicts(
|
334 |
+
{k + separator + p: w for p, w in v.items()}, separator=separator
|
335 |
+
)
|
336 |
+
result.update(new_dict)
|
337 |
+
else:
|
338 |
+
result[k] = v
|
339 |
+
return result
|
340 |
+
```
|
341 |
+
-----
|
342 |
+
|
343 |
+
'''
|
requirements.txt
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.1.0
|
2 |
+
aiohttp==3.8.4
|
3 |
+
aiosignal==1.3.1
|
4 |
+
altair==4.2.2
|
5 |
+
anyio==3.6.2
|
6 |
+
async-timeout==4.0.2
|
7 |
+
attrs==23.1.0
|
8 |
+
certifi==2022.12.7
|
9 |
+
charset-normalizer==3.1.0
|
10 |
+
click==8.1.3
|
11 |
+
contourpy==1.0.7
|
12 |
+
cycler==0.11.0
|
13 |
+
entrypoints==0.4
|
14 |
+
fastapi==0.95.1
|
15 |
+
ffmpy==0.3.0
|
16 |
+
filelock==3.12.0
|
17 |
+
fonttools==4.39.3
|
18 |
+
frozenlist==1.3.3
|
19 |
+
fsspec==2023.4.0
|
20 |
+
gradio==3.28.1
|
21 |
+
gradio_client==0.1.4
|
22 |
+
h11==0.14.0
|
23 |
+
httpcore==0.17.0
|
24 |
+
httpx==0.24.0
|
25 |
+
huggingface-hub==0.14.1
|
26 |
+
idna==3.4
|
27 |
+
importlib-resources==5.12.0
|
28 |
+
Jinja2==3.1.2
|
29 |
+
jsonschema==4.17.3
|
30 |
+
kiwisolver==1.4.4
|
31 |
+
linkify-it-py==2.0.0
|
32 |
+
markdown-it-py==2.2.0
|
33 |
+
MarkupSafe==2.1.2
|
34 |
+
matplotlib==3.7.1
|
35 |
+
mdit-py-plugins==0.3.3
|
36 |
+
mdurl==0.1.2
|
37 |
+
mpmath==1.3.0
|
38 |
+
multidict==6.0.4
|
39 |
+
networkx==3.1
|
40 |
+
numpy==1.24.3
|
41 |
+
orjson==3.8.11
|
42 |
+
packaging==23.1
|
43 |
+
pandas==2.0.1
|
44 |
+
Pillow==9.5.0
|
45 |
+
pkgutil_resolve_name==1.3.10
|
46 |
+
pydantic==1.10.7
|
47 |
+
pydub==0.25.1
|
48 |
+
pyparsing==3.0.9
|
49 |
+
pyrsistent==0.19.3
|
50 |
+
python-dateutil==2.8.2
|
51 |
+
python-multipart==0.0.6
|
52 |
+
pytz==2023.3
|
53 |
+
PyYAML==6.0
|
54 |
+
regex==2023.3.23
|
55 |
+
requests==2.29.0
|
56 |
+
semantic-version==2.10.0
|
57 |
+
six==1.16.0
|
58 |
+
sniffio==1.3.0
|
59 |
+
starlette==0.26.1
|
60 |
+
sympy==1.11.1
|
61 |
+
tokenizers==0.13.3
|
62 |
+
toolz==0.12.0
|
63 |
+
torch==2.0.0
|
64 |
+
tqdm==4.65.0
|
65 |
+
transformers==4.28.1
|
66 |
+
typing_extensions==4.5.0
|
67 |
+
tzdata==2023.3
|
68 |
+
uc-micro-py==1.0.1
|
69 |
+
urllib3==1.26.15
|
70 |
+
uvicorn==0.22.0
|
71 |
+
websockets==11.0.2
|
72 |
+
yarl==1.9.2
|
73 |
+
zipp==3.15.0
|
utils.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
from io import StringIO
|
4 |
+
import sys
|
5 |
+
import gradio
|
6 |
+
import os
|
7 |
+
|
8 |
+
|
9 |
+
def generate_response(query, top_p, token):
|
10 |
+
endpoint = "https://api-inference.huggingface.co/models/bigcode/starcoder"
|
11 |
+
params = {
|
12 |
+
'accept': 'application/json',
|
13 |
+
'Content-Type': 'application/json',
|
14 |
+
"Authorization": f"Bearer {token}"
|
15 |
+
}
|
16 |
+
data = {
|
17 |
+
'inputs': query,
|
18 |
+
"parameters": {
|
19 |
+
"details": True,
|
20 |
+
"do_sample": True,
|
21 |
+
"max_new_tokens": 256,
|
22 |
+
"repetition_penalty": 1.2,
|
23 |
+
"seed": 0,
|
24 |
+
"stop": ['<|endoftext|>'],
|
25 |
+
"temperature": 0.2,
|
26 |
+
"top_p": top_p,
|
27 |
+
}
|
28 |
+
}
|
29 |
+
try:
|
30 |
+
res = requests.post(endpoint, data=json.dumps(data), headers=params)
|
31 |
+
res = json.loads(res.content.decode("utf-8"))
|
32 |
+
return res[0]["generated_text"].split("<|endoftext|>")[0].strip()
|
33 |
+
except:
|
34 |
+
raise gradio.Error("Connection Error. Please check your ACCESS_TOKEN")
|
35 |
+
|
36 |
+
|
37 |
+
def run_code(code):
|
38 |
+
output_buffer = StringIO()
|
39 |
+
sys.stdout = output_buffer
|
40 |
+
exec(code, {})
|
41 |
+
sys.stdout = sys.__stdout__
|
42 |
+
pred = output_buffer.getvalue().strip()
|
43 |
+
return pred
|