From fdd4ff827e8de678c94612b78c87b1039d5a9592 Mon Sep 17 00:00:00 2001 From: henrisel Date: Mon, 10 Nov 2025 18:19:16 +0200 Subject: [PATCH] reimplement function from invokeai_mcp_server to make requests to local InvokeAI --- 3d-generation-pipeline/invoke_ai_api.py | 72 +++++++++++++++++++ .../_PROJECT/Scenes/DeltaBuilding_base.unity | 4 +- .../ModelGenerationPipelineStarter.cs | 4 +- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 3d-generation-pipeline/invoke_ai_api.py diff --git a/3d-generation-pipeline/invoke_ai_api.py b/3d-generation-pipeline/invoke_ai_api.py new file mode 100644 index 00000000..630400a8 --- /dev/null +++ b/3d-generation-pipeline/invoke_ai_api.py @@ -0,0 +1,72 @@ +from invokeai_mcp_server import create_text2img_graph, enqueue_graph, wait_for_completion, get_image_url +from urllib.parse import urljoin + +import asyncio + +INVOKEAI_BASE_URL = "http://127.0.0.1:9090" + + +async def generate_image(arguments: dict): + + # Extract parameters + prompt = arguments["prompt"] + negative_prompt = arguments.get("negative_prompt", "") + width = arguments.get("width", 512) + height = arguments.get("height", 512) + steps = arguments.get("steps", 30) + cfg_scale = arguments.get("cfg_scale", 7.5) + scheduler = arguments.get("scheduler", "euler") + seed = arguments.get("seed") + model_key = arguments.get("model_key") + lora_key = arguments.get("lora_key") + lora_weight = arguments.get("lora_weight", 1.0) + vae_key = arguments.get("vae_key") + + #logger.info(f"Generating image with prompt: {prompt[:50]}...") + + # Create graph + graph = await create_text2img_graph( + prompt=prompt, + negative_prompt=negative_prompt, + model_key=model_key, + lora_key=lora_key, + lora_weight=lora_weight, + vae_key=vae_key, + width=width, + height=height, + steps=steps, + cfg_scale=cfg_scale, + scheduler=scheduler, + seed=seed + ) + + # Enqueue and wait for completion + result = await enqueue_graph(graph) + batch_id = result["batch"]["batch_id"] + + #logger.info(f"Enqueued batch {batch_id}, waiting for completion...") + + completed = await wait_for_completion(batch_id) + + # Extract image name from result + if "result" in completed and "outputs" in completed["result"]: + outputs = completed["result"]["outputs"] + # Find the image output + for node_id, output in outputs.items(): + if output.get("type") == "image_output": + image_name = output["image"]["image_name"] + image_url = await get_image_url(image_name) + + text=f"Image generated successfully!\n\nImage Name: {image_name}\nImage URL: {image_url}\n\nYou can view the image at: {urljoin(INVOKEAI_BASE_URL, f'/api/v1/images/i/{image_name}/full')}" + print(text) + + # Fallback if we couldn't find image output + #text=f"Image generation completed but output format was unexpected. Batch ID: {batch_id}\n\nResult: {json.dumps(completed, indent=2)}" + +async def main(): + args = { + "prompt": "a golden katana with a fancy pommel" + } + await generate_image(args) + +asyncio.run(main()) diff --git a/Assets/_PROJECT/Scenes/DeltaBuilding_base.unity b/Assets/_PROJECT/Scenes/DeltaBuilding_base.unity index 9357cb38..56a49281 100644 --- a/Assets/_PROJECT/Scenes/DeltaBuilding_base.unity +++ b/Assets/_PROJECT/Scenes/DeltaBuilding_base.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6c4b9052863d6b6d99035c9570983fc7dc582ff3573c4b2f3b749239fe3e30ac -size 63212086 +oid sha256:82fdda2d339b0491cf35eec7b12f32500980662caff77ffda2a62cf34f59c700 +size 63212155 diff --git a/Assets/_PROJECT/Scripts/3DModeGeneration/ModelGenerationPipelineStarter.cs b/Assets/_PROJECT/Scripts/3DModeGeneration/ModelGenerationPipelineStarter.cs index 9d41e5a1..7349bd5d 100644 --- a/Assets/_PROJECT/Scripts/3DModeGeneration/ModelGenerationPipelineStarter.cs +++ b/Assets/_PROJECT/Scripts/3DModeGeneration/ModelGenerationPipelineStarter.cs @@ -11,6 +11,8 @@ public class ModelGenerationPipelineStarter : MonoBehaviour private MeshRenderer meshRenderer; + public string inputPrompt; + // Start is called before the first frame update void Start() { @@ -48,7 +50,7 @@ public class ModelGenerationPipelineStarter : MonoBehaviour { return await Task.Run(() => { - string inputPrompt = "tasty golden apple, photorealistic, smooth background"; + // Path to your virtual environment's python.exe string pythonExe = @"D:\users\henrisel\DeltaVR3DModelGeneration\3d-generation-pipeline\.venv\Scripts\python.exe";