forked from ChenShawn/MultiModal-Jupyter-Sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpress_test.py
More file actions
145 lines (119 loc) · 3.29 KB
/
press_test.py
File metadata and controls
145 lines (119 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import random
import requests
import grequests
import base64
from PIL import Image
from io import BytesIO
from datetime import datetime
code_1 = """
import matplotlib.pyplot as plt
x = [1,2,3]
y = [4,8,6]
print(f' [DEBUG] {x=}')
plt.plot(x, y)
plt.title('Debug Plot')
plt.show()
import time
while True:
time.sleep(1)
"""
code_2 = """
import numpy as np
x = np.array(x)
y = np.array(y)
z = x + y
print(' [DEBUG 123]')
plt.plot(x, z)
plt.title('Debug Plot 2')
plt.show()
print(' [DEBUG 234] {z=}')
plt.plot(x, -z)
plt.title('DEBUG PLOT 3')
plt.show()
print(' [DEBUG 345]')
"""
code_3 = """
import matplotlib.pyplot as plt
from PIL import Image
img = Image.open('highlighted_space.jpg').convert('RGB')
img_crop = img.crop((0, 0, 400, 600)) # Crop the image to a 100x100 square
plt.imshow(img_crop)
plt.axis('off')
plt.show()
# 中文注释
print('打印中文输出')
img_crop2 = img.crop((400, 600, 800, 1200)) # Crop another part of the image
plt.imshow(img_crop2)
plt.axis('off')
plt.show()
"""
INITIALIZATION_CODE_TEMPLATE = """
from PIL import Image
import base64
from io import BytesIO
_img_base64 = "{base64_image}"
image = Image.open(BytesIO(base64.b64decode(_img_base64)))
# dsadsfarg
import matplotlib.pyplot as plt
plt.imshow(image)
plt.title('中文测试')
plt.axis('off')
plt.show()
"""
def base64_to_image(base64_string: str) -> Image.Image:
image_data = base64.b64decode(base64_string)
image = Image.open(BytesIO(image_data))
return image
def image_to_base64(img: Image.Image, format: str = "PNG") -> str:
buffer = BytesIO()
img.save(buffer, format=format)
buffer.seek(0)
img_bytes = buffer.read()
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
return img_base64
test_sid = 'debug_jupyter_250805'
test_timeout = 5
reqlist = []
for it in range(128):
res1 = grequests.post(
# 'http://10.39.10.230:12345/jupyter_sandbox',
'http://127.0.0.1:80/jupyter_sandbox',
json={
"session_id": test_sid,
"code": code_1,
"timeout": test_timeout,
}
)
reqlist.append(res1)
time_before = datetime.now()
reslist = grequests.map(reqlist)
reslist = [res.json() for res in reslist]
time_after = datetime.now()
time_duration = time_after - time_before
print(f" [*] Complete {len(reqlist)} requests in :", time_duration)
exit()
target_image = Image.open('highlighted_space.jpg').convert('RGB')
target_image_base64 = image_to_base64(target_image)
code_string = INITIALIZATION_CODE_TEMPLATE.format(base64_image=target_image_base64)
res2 = requests.post(
# 'http://10.39.10.230:12345/jupyter_sandbox',
'http://127.0.0.1:12345/jupyter_sandbox',
json={
"session_id": test_sid,
"code": code_string,
"timeout": test_timeout,
}
).json()
print(f' [DEBUG #222] {res2.keys()=}')
print(f' [DEBUG #222] {res2["status"]=}')
print(f' [DEBUG #222] {res2["execution_time"]=}')
result_dict = res2['output']
# for k, v in result_dict.items():
# print(f' [DEBUG #222] {k=}, {len(v)=}')
print(f' [stdout] {result_dict["stdout"]=}')
print(f' [stderr] {result_dict["stderr"]=}')
print(f' [images] {len(result_dict["images"])=}')
for idx, img in enumerate(result_dict['images']):
img_pil = base64_to_image(img)
img_pil.save(f'./debug_output/{test_sid}-{idx}.png', format='PNG')
print(' Done!!')