Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
test.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from func_timeout import FunctionTimedOut, func_timeout
|
3 |
+
import os
|
4 |
+
|
5 |
+
LOGO_HEADER = """from myturtle_cv import Turtle
|
6 |
+
from myturtle import HALF_INF, INF, EPS_DIST, EPS_ANGLE
|
7 |
+
|
8 |
+
turtle = Turtle()
|
9 |
+
def forward(dist):
|
10 |
+
turtle.forward(dist)
|
11 |
+
def left(angle):
|
12 |
+
turtle.left(angle)
|
13 |
+
def right(angle):
|
14 |
+
turtle.right(angle)
|
15 |
+
def teleport(x, y, theta):
|
16 |
+
turtle.teleport(x, y, theta)
|
17 |
+
def penup():
|
18 |
+
turtle.penup()
|
19 |
+
def pendown():
|
20 |
+
turtle.pendown()
|
21 |
+
def position():
|
22 |
+
return turtle.x, turtle.y
|
23 |
+
def heading():
|
24 |
+
return turtle.heading
|
25 |
+
def isdown():
|
26 |
+
return turtle.is_down
|
27 |
+
def fork_state():
|
28 |
+
\"\"\"
|
29 |
+
Fork the current state of the turtle.
|
30 |
+
|
31 |
+
Usage:
|
32 |
+
with fork_state():
|
33 |
+
forward(100)
|
34 |
+
left(90)
|
35 |
+
forward(100)
|
36 |
+
\"\"\"
|
37 |
+
return turtle._TurtleState(turtle)"""
|
38 |
+
|
39 |
+
def run_code(new_folder, counter, code):
|
40 |
+
import matplotlib
|
41 |
+
fname = f"{new_folder}/logo_{counter}_.jpg"
|
42 |
+
counter += 1
|
43 |
+
code_with_header_and_save= f"""
|
44 |
+
{LOGO_HEADER}
|
45 |
+
{code}
|
46 |
+
# turtle.save('{fname}')
|
47 |
+
gif = turtle.save_gif('')
|
48 |
+
"""
|
49 |
+
try:
|
50 |
+
results = {}
|
51 |
+
func_timeout(20, exec, args=(code_with_header_and_save, results))
|
52 |
+
# exec(code_with_header_and_save, globals())
|
53 |
+
if 'gif' in results:
|
54 |
+
return results['gif']
|
55 |
+
except FunctionTimedOut:
|
56 |
+
print("Timeout")
|
57 |
+
except Exception as e:
|
58 |
+
print(e)
|
59 |
+
|
60 |
+
MOCK_RESPONSE = [
|
61 |
+
"""for i in range(7):
|
62 |
+
with fork_state():
|
63 |
+
for j in range(4):
|
64 |
+
forward(2*i)
|
65 |
+
left(90.0)
|
66 |
+
"""
|
67 |
+
] * 16
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
codes = MOCK_RESPONSE
|
71 |
+
from concurrent.futures import ProcessPoolExecutor
|
72 |
+
from concurrent.futures import as_completed
|
73 |
+
gif_results = []
|
74 |
+
import hashlib
|
75 |
+
import random
|
76 |
+
random_id = hashlib.md5(str(random.random()).encode()).hexdigest()[0:4]
|
77 |
+
gradio_test_images_folder = "gradio_test_images"
|
78 |
+
new_folder = os.path.join(gradio_test_images_folder, random_id)
|
79 |
+
with ProcessPoolExecutor() as executor:
|
80 |
+
futures = [executor.submit(run_code, new_folder, i, code) for i, code in enumerate(codes)]
|
81 |
+
for future in as_completed(futures):
|
82 |
+
try:
|
83 |
+
gif_results.append(future.result())
|
84 |
+
except Exception as exc:
|
85 |
+
print(f'Generated an exception: {exc}')
|
86 |
+
|
87 |
+
# with open("temp.py", 'w') as f:
|
88 |
+
# f.write(code_with_header_and_save)
|
89 |
+
|
90 |
+
# p = subprocess.Popen(["python", "temp.py"], stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=my_env)
|
91 |
+
# out, errs = p.communicate()
|
92 |
+
# out, errs, = out.decode(), errs.decode()
|
93 |
+
# render
|
94 |
+
print(random_id)
|
95 |
+
folder_path = f"gradio_test_images/{random_id}"
|
96 |
+
breakpoint()
|
97 |
+
gif_results, codes
|