|  | @@ -11,9 +11,72 @@ import io
 | 
	
		
			
				|  |  |  import random
 | 
	
		
			
				|  |  |  import sys
 | 
	
		
			
				|  |  |  import base64
 | 
	
		
			
				|  |  | +import requests
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  server_address = "127.0.0.1:8188"
 | 
	
		
			
				|  |  |  client_id = str(uuid.uuid4())
 | 
	
		
			
				|  |  | +api_path = "https://canvas-api-test.anvil.app/_/api"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +image_path = "D:/Temp/ComfyUI_windows_portable_nvidia/ComfyUI_windows_portable/ComfyUI/output/"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def update_ai_image_task_status(row_id, new_status):
 | 
	
		
			
				|  |  | +    # Define the URL for the API endpoint
 | 
	
		
			
				|  |  | +    url = "{}/tasks/ai-image/update-status".format(api_path)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # Create a JSON payload
 | 
	
		
			
				|  |  | +    payload = {"row_id": row_id, "new_status": new_status}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # Make a POST request to the API endpoint with the JSON payload
 | 
	
		
			
				|  |  | +    response = requests.post(url, json=payload)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # Handle the response
 | 
	
		
			
				|  |  | +    if response.status_code == 200:
 | 
	
		
			
				|  |  | +        print("Status update was successful")
 | 
	
		
			
				|  |  | +        return response.json()
 | 
	
		
			
				|  |  | +    else:
 | 
	
		
			
				|  |  | +        print("Status update failed")
 | 
	
		
			
				|  |  | +        print("Status code:", response.status_code)
 | 
	
		
			
				|  |  | +        print("Response:", response.text)
 | 
	
		
			
				|  |  | +        return None
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def get_ai_image_task(row_id):
 | 
	
		
			
				|  |  | +    # Define the URL for the API endpoint
 | 
	
		
			
				|  |  | +    url = "{}/tasks/ai-image/{}".format(api_path, row_id)
 | 
	
		
			
				|  |  | +    print("Constructed URL:", url)  # Print the URL for debugging
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # Make a GET request to the API endpoint
 | 
	
		
			
				|  |  | +    response = requests.get(url)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # Handle the response
 | 
	
		
			
				|  |  | +    if response.status_code == 200:
 | 
	
		
			
				|  |  | +        print("Request was successful")
 | 
	
		
			
				|  |  | +        return response.json()
 | 
	
		
			
				|  |  | +    else:
 | 
	
		
			
				|  |  | +        print("Request failed")
 | 
	
		
			
				|  |  | +        print("Status code:", response.status_code)
 | 
	
		
			
				|  |  | +        print("Response:", response.text)
 | 
	
		
			
				|  |  | +        return None
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def find_image_and_convert_to_base64(image_path):
 | 
	
		
			
				|  |  | +    with open(image_path, "rb") as image_file:
 | 
	
		
			
				|  |  | +        image_data = image_file.read()
 | 
	
		
			
				|  |  | +        image_base64 = base64.b64encode(image_data).decode("utf-8")
 | 
	
		
			
				|  |  | +        return image_base64
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def upload_image_to_anvil(row_id, image_base64):
 | 
	
		
			
				|  |  | +    url = "{}/tasks/ai-image/upload-image".format(api_path)
 | 
	
		
			
				|  |  | +    payload = {"row_id": row_id, "image_base64": image_base64}
 | 
	
		
			
				|  |  | +    response = requests.post(url, json=payload)
 | 
	
		
			
				|  |  | +    if response.status_code == 200:
 | 
	
		
			
				|  |  | +        print("Image uploaded successfully")
 | 
	
		
			
				|  |  | +        update_ai_image_task_status(row_id=row_id, new_status=3)
 | 
	
		
			
				|  |  | +        return response.json()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    else:
 | 
	
		
			
				|  |  | +        print("Image upload failed")
 | 
	
		
			
				|  |  | +        print("Status code:", response.status_code)
 | 
	
		
			
				|  |  | +        print("Response:", response.text)
 | 
	
		
			
				|  |  | +        return None
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  def load_debug_ai_scene_info():
 | 
	
	
		
			
				|  | @@ -151,7 +214,11 @@ def get_images(ws, prompt):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      return output_images
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -def main():
 | 
	
		
			
				|  |  | +def main(*args):
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    deadlinePlugin = args[0]
 | 
	
		
			
				|  |  | +    job = deadlinePlugin.GetJob()
 | 
	
		
			
				|  |  | +    row_id = job.JobExtraInfo0
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      argv = sys.argv
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -166,20 +233,32 @@ def main():
 | 
	
		
			
				|  |  |      except Exception as e:
 | 
	
		
			
				|  |  |          print("Error:", e)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      # ai_scene_info = load_debug_ai_scene_info()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      prompt = get_prompt(ai_scene_info)    
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      ws = websocket.WebSocket()
 | 
	
		
			
				|  |  |      ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
 | 
	
		
			
				|  |  | -    images = get_images(ws, prompt)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    prompt_id = queue_prompt(prompt)['prompt_id']
 | 
	
		
			
				|  |  | +    update_ai_image_task_status(row_id, 2)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    images = get_images(ws, prompt)        
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      for node_id in images:
 | 
	
		
			
				|  |  |          for image_info in images[node_id]:
 | 
	
		
			
				|  |  |              if image_info['type'] == 'output':
 | 
	
		
			
				|  |  | -                print("Upload image")
 | 
	
		
			
				|  |  | +                response = get_ai_image_task(row_id)
 | 
	
		
			
				|  |  | +                data = json.loads(response["data"])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                project_id = data["project_id"]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                image_base64 = find_image_and_convert_to_base64(
 | 
	
		
			
				|  |  | +                    image_path + "{}/{}".format(project_id,image_info['filename'])
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +                upload_image_to_anvil(row_id, image_base64)
 | 
	
		
			
				|  |  |                  
 | 
	
		
			
				|  |  |              
 | 
	
		
			
				|  |  |  
 |