Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import ReactCodeAgent, HfEngine, Tool
|
4 |
+
from gradio_agentchatbot import (
|
5 |
+
AgentChatbot,
|
6 |
+
stream_from_transformers_agent,
|
7 |
+
ChatMessage,
|
8 |
+
)
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
from huggingface_hub import login
|
11 |
+
from transformers.agents.default_tools import (
|
12 |
+
BASE_PYTHON_TOOLS,
|
13 |
+
LIST_SAFE_MODULES,
|
14 |
+
evaluate_python_code,
|
15 |
+
)
|
16 |
+
|
17 |
+
# to load SerpAPI key
|
18 |
+
load_dotenv()
|
19 |
+
login(os.getenv("HUGGINGFACEHUB_API_KEY"))
|
20 |
+
|
21 |
+
|
22 |
+
llm_engine = HfEngine(model="meta-llama/Meta-Llama-3-70B-Instruct")
|
23 |
+
|
24 |
+
authorized_imports = ["numpy"]
|
25 |
+
|
26 |
+
agent = ReactCodeAgent(
|
27 |
+
llm_engine=llm_engine,
|
28 |
+
tools=[],
|
29 |
+
additional_authorized_imports=authorized_imports,
|
30 |
+
max_iterations=10,
|
31 |
+
)
|
32 |
+
|
33 |
+
class FinalAnswerToolWithVerification(Tool):
|
34 |
+
name = "final_answer"
|
35 |
+
description = "Provides a final answer to the given problem"
|
36 |
+
inputs = {
|
37 |
+
"answer": {"type": "text", "description": "The final answer to the problem"}
|
38 |
+
}
|
39 |
+
output_type = "any"
|
40 |
+
|
41 |
+
def forward(self, answer):
|
42 |
+
if "def test" not in answer:
|
43 |
+
raise Exception(
|
44 |
+
"I can only accept from you a code snippet answer that defines test functions in python, anything else will not work. PLEASE PROVIDE ME A FULL CODE SNIPPET CONTAINING THE DEFINITION OF THE TESTS."
|
45 |
+
)
|
46 |
+
return answer
|
47 |
+
|
48 |
+
final_answer_tool = FinalAnswerToolWithVerification()
|
49 |
+
|
50 |
+
agent._toolbox.update_tool(final_answer_tool)
|
51 |
+
|
52 |
+
function = """import numpy as np
|
53 |
+
def moving_average(x, w):
|
54 |
+
return np.convolve(x, np.ones(w), 'valid') / w"""
|
55 |
+
|
56 |
+
task = """I will give you a basic function that I've created.
|
57 |
+
Now I want you to generate a set of unit tests for these functions, check that they run, and give them to me.
|
58 |
+
Please follow these steps in order:
|
59 |
+
1. Define and run the function given o you, so that it gets defined in your interpreter.
|
60 |
+
2. Generate one test function as a python blob, with assert statements
|
61 |
+
3. Run the test function in a code snippet and make sure the tests pass
|
62 |
+
4. Return to me the complete TEST function (not the original function) as a string code snippet.
|
63 |
+
---
|
64 |
+
Example:
|
65 |
+
Here is your function:
|
66 |
+
```py
|
67 |
+
def get_even_numbers(numbers):
|
68 |
+
even_numbers = []
|
69 |
+
for number in numbers:
|
70 |
+
if number % 2 == 0:
|
71 |
+
even_numbers.append(number)
|
72 |
+
return even_numbers
|
73 |
+
```
|
74 |
+
Now generate test functions for me!
|
75 |
+
Thought: Let's re-define the given function and generate a test.
|
76 |
+
Code:
|
77 |
+
```py
|
78 |
+
def get_even_numbers(numbers):
|
79 |
+
even_numbers = []
|
80 |
+
for number in numbers:
|
81 |
+
if number % 2 == 0:
|
82 |
+
even_numbers.append(number)
|
83 |
+
return even_numbers
|
84 |
+
def test_get_even_numbers():
|
85 |
+
assert get_even_numbers([1, 2, 3, 4, 5]) == [2, 4]
|
86 |
+
print("No error found!")
|
87 |
+
test_get_even_numbers()
|
88 |
+
```
|
89 |
+
Observation: "No error found!"
|
90 |
+
Thought: the interpreter ran tests with no error. So we can return the function IN A TEXT SNIPPET.
|
91 |
+
Code:
|
92 |
+
```py
|
93 |
+
fianl_answer_snippet = \"\"\"
|
94 |
+
def test_get_even_numbers():
|
95 |
+
assert get_even_numbers([1, 2, 3, 4, 5]) == [2, 4]
|
96 |
+
print("No error found!")
|
97 |
+
\"\"\"
|
98 |
+
final_answer(final_answer_snippet)
|
99 |
+
```
|
100 |
+
---
|
101 |
+
Now proceed!
|
102 |
+
Here is your function:
|
103 |
+
```py
|
104 |
+
<<function>>
|
105 |
+
```
|
106 |
+
Now generate test functions for me!
|
107 |
+
"""
|
108 |
+
|
109 |
+
|
110 |
+
def interact_with_agent(prompt):
|
111 |
+
full_prompt = task.replace("<<function>>", prompt)
|
112 |
+
messages = []
|
113 |
+
messages.append(ChatMessage(role="user", content=full_prompt))
|
114 |
+
yield messages
|
115 |
+
for msg in stream_from_transformers_agent(agent, full_prompt):
|
116 |
+
messages.append(msg)
|
117 |
+
yield messages
|
118 |
+
yield messages
|
119 |
+
|
120 |
+
|
121 |
+
with gr.Blocks(theme="soft") as demo:
|
122 |
+
gr.Markdown("""### Python test generator
|
123 |
+
Write your function in the left textbox, and the agent on the right will generate tests for you!""")
|
124 |
+
with gr.Row():
|
125 |
+
with gr.Column():
|
126 |
+
text_input = gr.Textbox(
|
127 |
+
lines=1, label="Your function to test", value=function
|
128 |
+
)
|
129 |
+
submit = gr.Button("Generate tests!")
|
130 |
+
with gr.Column():
|
131 |
+
chatbot = AgentChatbot(label="Agent")
|
132 |
+
|
133 |
+
submit.click(interact_with_agent, [text_input], [chatbot])
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
demo.launch()
|