瀏覽代碼

update AI api

Sergiu 9 月之前
父節點
當前提交
ae2e779210
共有 3 個文件被更改,包括 45 次插入1 次删除
  1. 17 0
      04_stable_diffusion/ai_scene_info.json
  2. 7 0
      04_stable_diffusion/sd_comfy_api.py
  3. 21 1
      04_stable_diffusion/sd_comfy_api_v2.py

+ 17 - 0
04_stable_diffusion/ai_scene_info.json

@@ -0,0 +1,17 @@
+{
+    "ai_scene": {
+        "workflow": "canvas_3d_to_img_standard_V1.json",
+        "prompt": {
+            "positive_prompt": "placeholder text",
+            "negative_prompt": "placeholder text"
+        },
+        "settings": {
+            "checkpoint": "",
+            "sampler": "",
+            "cfg": 5,
+            "steps": 5
+        }
+    },
+    "project_id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
+    "user_id": "1125441"
+}

+ 7 - 0
04_stable_diffusion/sd_comfy_api.py

@@ -42,6 +42,11 @@ def queue_prompt(prompt):
     req = request.Request("http://127.0.0.1:8188/prompt", data=data)
     request.urlopen(req)
 
+def load_debug_ai_scene_info():
+    #open ai_scene_info.json
+    with open("D:/Git/ap-canvas-creation-module/04_stable_diffusion/ai_scene_info.json", "r") as f:
+        ai_scene_info = json.load(f)
+    return ai_scene_info
 
 def find_node(json_obj, title):
     for key, value in json_obj.items():
@@ -84,6 +89,8 @@ def main():
     except Exception as e:
         print("Error:", e)
 
+    ai_scene_info = load_debug_ai_scene_info()
+
     positive_text = ai_scene_info["ai_scene"]["settings"]["positive_prompt"]
     negative_text = ai_scene_info["ai_scene"]["settings"]["negative_prompt"]
 

+ 21 - 1
04_stable_diffusion/sd_comfy_api_v2.py

@@ -15,18 +15,28 @@ import base64
 server_address = "127.0.0.1:8188"
 client_id = str(uuid.uuid4())
 
+
+def load_debug_ai_scene_info():
+    #open ai_scene_info.json
+    with open("D:/Git/ap-canvas-creation-module/04_stable_diffusion/ai_scene_info.json", "r") as f:
+        ai_scene_info = json.load(f)
+    return ai_scene_info
+
 def convert_base64_string_to_object(base64_string):
     bytes = base64.b64decode(base64_string)
     string = bytes.decode("ascii")
 
     return json.loads(string)
 
+
+
 def set_filename(json_obj, title, new_prefix):
     for key, value in json_obj.items():
         if isinstance(value, dict):
             if value.get("_meta", {}).get("title") == title:
                 if "inputs" in value and "filename_prefix" in value["inputs"]:
                     value["inputs"]["filename_prefix"] = new_prefix
+                    return new_prefix
                 else:
                     result = set_filename(value, title, new_prefix)
                     if result:
@@ -45,6 +55,7 @@ def find_node(json_obj, title):
     return None
 
 
+
 def queue_prompt(prompt):
     p = {"prompt": prompt, "client_id": client_id}
     data = json.dumps(p).encode('utf-8')
@@ -70,7 +81,8 @@ def get_prompt(ai_scene_info):
     # image_depth_path = image_path + "depth0001.png"
 
     prompt = json.loads(prompt_text_json)
-    set_filename(prompt, "Save Image", "{project_id}/basic_api_example".format(project_id=ai_scene_info["project_id"]))
+    file_name =  set_filename(prompt, "Save Image", "{project_id}/basic_api_example".format(project_id=ai_scene_info["project_id"]))
+    
 
     ksampler_main = find_node(prompt, "KSampler")
     ksampler_main["inputs"]["noise_seed"] = random.randint(0, 1000000)
@@ -150,12 +162,20 @@ 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']
+
+
+            
+            
+
     
 
     #Commented out code to display the output images: