-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
317 lines (234 loc) · 11.7 KB
/
runner.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from chain import *
import json
from utils import *
import requests
import os
import json
import time
import cv2
from loguru import logger
from langchain_core.callbacks import FileCallbackHandler, StdOutCallbackHandler
from langsmith.wrappers import wrap_openai
from langsmith import traceable
from image_inference import *
import glob2
from PIL import Image, ImageDraw, ImageFont
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
url = "172.24.115.43" #change URL HERE
OPENAI_API_KEY = "sk-" #ADD KEY HERE
os.environ["QT_QPA_PLATFORM"] = "offscreen"
def get_toolhead_state(url,debug=False):
"""
Fetches the state of the toolhead from a Moonraker 3D printer.
Args:
url (str): The base URL to the Moonraker API.
"""
full_url = f"http://{url}/printer/objects/query?toolhead"
try:
# Send a request to the Moonraker API to get the state of the toolhead
response = requests.get(full_url)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the JSON response and extract toolhead status
data = response.json()
toolhead_state = data.get('result', {}).get('status', {}).get('toolhead', {})
# Print the state of the toolhead
if debug:
print("Toolhead State:")
for key, value in toolhead_state.items():
print(f"{key}: {value}")
return True, toolhead_state
except requests.RequestException as e:
print(f"Failed to retrieve toolhead state: {e}")
return False, e
def crop_combine(top_loc, front_loc, save_loc):
load_top = np.array(Image.open(top_loc))
load_front = np.array(Image.open(front_loc))
# plt.imshow(load_top[200:,600:1600], origin='lower')
# plt.show()
# plt.imshow(load_front[:,200:1250])
# plt.show()
# Crop the images
load_top_cropped = load_top[200:, 650:1550]
load_front_cropped = load_front[200:, 400:1250]
# Determine the new image size
height_top, width_top, _ = load_top_cropped.shape
height_front, width_front, _ = load_front_cropped.shape
# Create a new image with combined width
combined_image = np.zeros((max(height_top, height_front), width_top + width_front, 3), dtype=np.uint8)
# Place the images side by side
combined_image[:height_top, :width_top, :] = load_top_cropped
combined_image[:height_front, width_top:width_top + width_front, :] = load_front_cropped
# Convert to PIL image for drawing
combined_image_pil = Image.fromarray(combined_image)
draw = ImageDraw.Draw(combined_image_pil)
font = ImageFont.load_default(size=100)
draw.text((10, 10), "Top", font=font, fill=(255, 255, 255))
draw.text((width_top + 10, 10), "Front", font=font, fill=(255, 255, 255))
combined_image_with_text = np.array(combined_image_pil)
# Display the combined image
plt.imshow(combined_image_with_text)
plt.tight_layout()
plt.axis('off')
plt.gca().set_axis_off()
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.savefig(save_loc, bbox_inches='tight', pad_inches=0)
plt.close()
def resume_print(ip_address):
"""
Pause the current print job on a 3D printer managed by MainsailOS.
Args:
ip_address (str): The IP address of the MainsailOS server.
"""
url = f"http://{ip_address}/printer/print/resume"
headers = {'Content-Type': 'application/json'}
data = {} # Depending on your setup, this might need to be 'M25' or another specific command
# try:
response = requests.post(url, json=data, headers=headers),
def get_printer_state(url):
"""
Fetches the state of the printer from a Moonraker 3D printer.
Args:
url (str): The base URL to the Moonraker API.
"""
url = f"http://{url}/printer/objects/query?print_stats"
response = requests.get(url)
response.raise_for_status()
data = response.json()
return data
def get_timelapse_image(frame_no, path):
"""
Fetches a timelapse image from a Moonraker 3D printer.
Args:
frame_no (int): The frame number of the timelapse image to fetch.
"""
url = f"http://172.24.115.43/server/files/timelapse_frames/frame{frame_no}.jpg"
response = requests.get(url)
response.raise_for_status()
with open(path, 'wb') as f:
f.write(response.content)
def get_image(url, path, mode="front"):
"""gets the image of the printer encodes it"""
print("Getting front image")
if mode=="front":
url = f"http://{url}/webcam3/?action=snapshot"
else:
url = f"http://{url}/webcam/?action=snapshot"
response = requests.get(url)
if response.status_code == 200:
image_path = path
with open(image_path, "wb") as f:
f.write(response.content)
# print("Image saved as printer_image.jpg")
else:
print("Failed to retrieve image, status code:", response.status_code)
def check_previous_images(path):
img_list = glob2.glob(path+"/top_**.jpg")
num=[0]
for i in img_list:
num.append(int(i.split("_")[-1].split(".")[0]))
return max(num)
def runner(printer_url="172.24.115.43",
image_save_path="./results_3/cmu/layer_images",
save_path="./results_3/cmu",
image_resize_factor=2,
openai_api_key=OPENAI_API_KEY,
):
flag= True
# prompts = json.load(open("./prompts/system_prompt.json", "r"))
image_system_prompt=load_text_file("./prompts/image_system_prompts.txt")
image_user_prompt=load_text_file("./prompts/image_user_prompt.txt")
while flag:
max_tries = 5
print(f"\033[91mChecking printer state\033[0m")
current_state = get_printer_state(url)
# print(current_state)
printer_status = current_state["result"]["status"]["print_stats"]["state"]
current_layer = current_state["result"]["status"]["print_stats"]["info"]["current_layer"]
print(f"\033[91mPrinter Status: {printer_status}\033[0m")
print(f"\033[91mCurrent Layer: {current_layer}\033[0m")
if printer_status == "printing":
time.sleep(20)
if printer_status == "complete":
print("\033[91mPrinting Complete. Exiting.\033[0m")
flag=False
if printer_status == "paused":
print("\033[91mPrinter is paused. Take snapshot.\033[0m")
time.sleep(10)
# nn = check_previous_images(image_save_path)
nn = current_layer
# nn=nn+1
pre=2
if nn>=3:
pre=nn-1
# print(nn)
# image_top = get_timelapse_image(str("%06d" % nn), image_save_path+f"/top_layer_{nn}.jpg")
image_front = get_image(printer_url, image_save_path+ f"/front_layer_{nn}.jpg")
image_top = get_image(printer_url, image_save_path+ f"/top_layer_{nn}.jpg", mode="top")
print("Image saved.")
time.sleep(5)
# croped = crop_combine(top_loc=image_save_path+ f"/top_layer_{nn}.jpg", front_loc=image_save_path+ f"/front_layer_{nn}.jpg")
# prompts = json.load(open("./prompts/system_prompt.json", "r"))
image_system_prompt=load_text_file("./prompts/image_system_prompts.txt")
image_user_prompt=load_text_file("./prompts/image_user_prompt.txt")
# failures,r = send_image(image_save_path+f"/top_layer_{nn}.jpg",system_prompt=prompts["system_prompt_eyes"],user_prompt=f"This the current image at layer {current_layer}. Identify the most visually prominent defects in the current layer.", resize_factor=image_resize_factor, api_key=openai_api_key, image_path_2=image_save_path+f"/front_layer_{pre}.jpg")
if nn==3:
croped = crop_combine(top_loc=image_save_path+ f"/top_layer_{nn}.jpg", front_loc=image_save_path+ f"/front_layer_{nn}.jpg", save_loc=image_save_path+ f"/combined_{nn}.jpg")
failures = send_image(image_save_path+f"/combined_{nn}.jpg", system_prompt=image_system_prompt, user_prompt = image_user_prompt, resize_factor=2)
# print("skipping correction for first layer")
# resume_print(url)
else:
print("Running image inference")
croped = crop_combine(top_loc=image_save_path+ f"/top_layer_{nn}.jpg", front_loc=image_save_path+ f"/front_layer_{nn}.jpg", save_loc=image_save_path+ f"/combined_{nn}.jpg")
failures = send_image(image_save_path+f"/combined_{nn}.jpg", system_prompt=image_system_prompt, user_prompt = image_user_prompt, resize_factor=2, previous_image_path=image_save_path+f"/combined_{pre}.jpg")
#save failures to a file
with open(save_path+f"/failures_{nn}.txt", "w") as f:
f.write(failures.content)
print(f"\033[92m Detected Failures:\n {failures}\033[0m")
#check if a file exists
if os.path.exists(save_path+f"/previous_solution_{pre}.txt"):
previous_solution = load_text_file(save_path+f"/previous_solution_{pre}.txt")
else:
previous_solution = []
graph = get_graph()
print("\033[94mRunning LLM AGENT")
logfile = save_path+f"/log{nn}.log"
logger.add(logfile, colorize=True, enqueue=True)
handler_1 = FileCallbackHandler(logfile)
handler_2 = StdOutCallbackHandler()
reasoning_planner=load_text_file("./prompts/info_reasoning.txt")
# observation=load_text_file("failure.txt")
observation = failures.content
printer_objects = load_text_file("./prompts/printer_objects.txt")
solution_reasoning= load_text_file("./prompts/solution_reasoning.txt")
gcode_cmd = load_text_file("./prompts/gcode_commands.txt")
out = graph.invoke(
{
"internal_messages": ["Given the failure (if any) plan for what information is required to identify the issue, query printer for the required information, plan the solution steps to solve the problem, execute the solution plan, resume print and finish.\n If no problem is detected, resume print."],
"printer_url": printer_url,
"information_known":["Printer url is http://172.24.115.43","Filament type is PLA","printer model is creatlity ender 5 plus", f"Printer status is paused", "Current layer is {current_layer}", "Tool position is at the home position", "BED is perfectly Calibrated", "Nozzle diameter is 1mm", "There are no issues with the nozzle and printer hardware.", "Layer height is 0.3", "Infill pattern is aligned rectiliner"],
"observations": observation,
"reasoning": reasoning_planner,
"solution_reasoning": solution_reasoning,
"printer_obj": printer_objects,
"adapted_recon_reasoning" : [],
"adapter_solution_reasoning" : [],
"gcode_commands":gcode_cmd,
"previous_solution" : previous_solution,
"solution_steps":[],
},
{"callbacks":[handler_1, handler_2]},
debug=True
)
previous_solution = out["solution_steps"]
with open(save_path+f"/previous_solution_{nn}.txt", "w") as f:
f.write(str(previous_solution))
with open(save_path+f"/LLM_out_{nn}.txt", "w") as f:
f.write(str(out))
runner()
# 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