Lupara90 commited on
Commit
ff46616
·
verified ·
1 Parent(s): a49802e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -61,8 +61,7 @@ if not os.path.exists(COMFYUI_DIR):
61
  if COMFYUI_DIR not in sys.path:
62
  sys.path.insert(0, COMFYUI_DIR)
63
 
64
- # Crucial ZeroGPU Fix: Mock command line arguments so ComfyUI doesn't try
65
- # to initialize CUDA/comfy-aimdo during python file parsing on startup.
66
  sys.argv = [sys.argv[0], "--cpu"]
67
 
68
  # Safe to import the main engine now
@@ -74,29 +73,52 @@ import main as comfy_main
74
  # ==========================================
75
  @spaces.GPU(duration=60) # Borrow free ZeroGPU for 60 seconds per run
76
  def generate_image(user_prompt):
77
- # Load your exported ComfyUI API JSON
78
  if not os.path.exists("workflow_api.json"):
 
79
  return None
80
 
81
  with open("workflow_api.json", "r") as f:
82
  prompt_workflow = json.load(f)
83
 
84
- # TARGET THE EXACT NODE ID:
85
  # Open your workflow_api.json to verify which ID belongs to your "CLIP Text Encode" node.
86
  # Replace '6' with your specific text prompt node ID if different.
87
  prompt_workflow["6"]["inputs"]["text"] = user_prompt
88
 
89
- # Temporarily switch ComfyUI to CUDA execution during ZeroGPU allocation
90
- # override the global device variables safely inside the GPU context
91
  import comfy.model_management
92
  comfy.model_management.get_torch_device = lambda: torch.device("cuda")
93
 
94
- # Execute the workflow using ComfyUI's internal API handler
95
- output_images = comfy_main.execute_workflow_directly(prompt_workflow)
96
-
97
- # Return the generated image path
98
- if output_images and len(output_images) > 0:
99
- return output_images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  return None
101
 
102
 
 
61
  if COMFYUI_DIR not in sys.path:
62
  sys.path.insert(0, COMFYUI_DIR)
63
 
64
+ # Force ComfyUI to bypass local hardware optimizations/init blocks at startup
 
65
  sys.argv = [sys.argv[0], "--cpu"]
66
 
67
  # Safe to import the main engine now
 
73
  # ==========================================
74
  @spaces.GPU(duration=60) # Borrow free ZeroGPU for 60 seconds per run
75
  def generate_image(user_prompt):
 
76
  if not os.path.exists("workflow_api.json"):
77
+ print("Error: workflow_api.json not found in the root directory!")
78
  return None
79
 
80
  with open("workflow_api.json", "r") as f:
81
  prompt_workflow = json.load(f)
82
 
83
+ # 1. Inject the user's custom prompt into the workflow configuration.
84
  # Open your workflow_api.json to verify which ID belongs to your "CLIP Text Encode" node.
85
  # Replace '6' with your specific text prompt node ID if different.
86
  prompt_workflow["6"]["inputs"]["text"] = user_prompt
87
 
88
+ # 2. Explicitly override device constraints to utilize Hugging Face's GPU context
 
89
  import comfy.model_management
90
  comfy.model_management.get_torch_device = lambda: torch.device("cuda")
91
 
92
+ # 3. Execute the loaded workflow programmatically
93
+ # We parse the prompt format to feed it directly to ComfyUI's execution loop
94
+ from execution import validate_prompt
95
+
96
+ # Check if the structure of our JSON is valid
97
+ valid_prompt = validate_prompt(prompt_workflow)
98
+ if not valid_prompt[0]:
99
+ print(f"Workflow Validation Failed: {valid_prompt[1]}")
100
+ return None
101
+
102
+ # Retrieve unique execution ID and enqueue task
103
+ import uuid
104
+ prompt_id = str(uuid.uuid4())
105
+
106
+ # Run ComfyUI's internal queue mechanics to generate the asset
107
+ # This automatically processes nodes, saves outputs locally, and cleans memory
108
+ try:
109
+ # Trigger the execution queue thread
110
+ comfy_main.execute_workflow(prompt_workflow, prompt_id)
111
+
112
+ # Locate the saved output image (typically saved to ComfyUI/output/)
113
+ output_dir = os.path.join(COMFYUI_DIR, "output")
114
+ if os.path.exists(output_dir):
115
+ files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, f))]
116
+ if files:
117
+ # Return the newest generated file in the output directory
118
+ return max(files, key=os.path.getmtime)
119
+ except Exception as e:
120
+ print(f"Execution failed: {e}")
121
+
122
  return None
123
 
124