Manishh-07 commited on
Commit
4c6474d
·
verified ·
1 Parent(s): d667dc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -31,7 +31,15 @@ def generate_prompts_and_image(input_text):
31
  positive = lines[0] if lines else "No positive prompt generated"
32
  negative = lines[1] if len(lines) > 1 else "No negative prompt generated"
33
 
34
- # Load and modify ComfyUI workflow
 
 
 
 
 
 
 
 
35
  workflow_path = "workflow.json"
36
  temp_workflow_path = "temp_workflow.json"
37
  if not os.path.exists(workflow_path):
@@ -40,24 +48,34 @@ def generate_prompts_and_image(input_text):
40
  with open(workflow_path, "r") as f:
41
  workflow = json.load(f)
42
 
43
- # Inject prompts into nodes 6 (positive) and 7 (negative) from your workflow.json
44
- workflow["6"]["widgets_values"][0] = positive # Positive prompt (CLIPTextEncode)
45
- workflow["7"]["widgets_values"][0] = negative # Negative prompt (CLIPTextEncode)
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Save temporary workflow
48
  with open(temp_workflow_path, "w") as f:
49
  json.dump(workflow, f)
50
 
51
  # Run ComfyUI via subprocess
52
- comfyui_dir = os.path.join(os.path.dirname(__file__), "ComfyUI")
53
  comfyui_main = os.path.join(comfyui_dir, "main.py")
54
  output_dir = os.path.join(comfyui_dir, "output") # ComfyUI default output dir
55
  os.makedirs(output_dir, exist_ok=True)
56
 
57
  try:
58
- # Note: ComfyUI's main.py doesn't use --workflow directly; it reads from the workflow file
59
  result = subprocess.run(
60
- ["python", comfyui_main, "--input-directory", comfyui_dir, "--output-directory", output_dir],
61
  cwd=comfyui_dir,
62
  capture_output=True,
63
  text=True,
 
31
  positive = lines[0] if lines else "No positive prompt generated"
32
  negative = lines[1] if len(lines) > 1 else "No negative prompt generated"
33
 
34
+ # Debug: Check if ComfyUI and model exist
35
+ comfyui_dir = os.path.join(os.path.dirname(__file__), "ComfyUI")
36
+ checkpoint_path = os.path.join(comfyui_dir, "models", "checkpoints", "v1-5-pruned-emaonly-fp16.safetensors")
37
+ if not os.path.exists(comfyui_dir):
38
+ return {"error": f"ComfyUI directory not found at {comfyui_dir}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
39
+ if not os.path.exists(checkpoint_path):
40
+ return {"error": f"Checkpoint not found at {checkpoint_path}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
41
+
42
+ # Load and debug ComfyUI workflow
43
  workflow_path = "workflow.json"
44
  temp_workflow_path = "temp_workflow.json"
45
  if not os.path.exists(workflow_path):
 
48
  with open(workflow_path, "r") as f:
49
  workflow = json.load(f)
50
 
51
+ # Debug: Print workflow nodes to verify IDs
52
+ print("Workflow nodes:", json.dumps(workflow["nodes"], indent=2))
53
+
54
+ # Use hardcoded node IDs from workflow.json (6 for positive, 7 for negative)
55
+ positive_id, negative_id = 6, 7 # Hardcoded based on your workflow.json
56
+ try:
57
+ if str(positive_id) not in workflow or str(negative_id) not in workflow:
58
+ return {"error": f"Node IDs {positive_id} or {negative_id} not found in workflow.json", "time_taken": f"{time.time() - start_time:.2f} seconds"}
59
+
60
+ # Inject prompts into nodes 6 (positive) and 7 (negative)
61
+ workflow[str(positive_id)]["widgets_values"][0] = positive # Positive prompt (CLIPTextEncode)
62
+ workflow[str(negative_id)]["widgets_values"][0] = negative # Negative prompt (CLIPTextEncode)
63
+ except KeyError as e:
64
+ return {"error": f"Error injecting prompts: {str(e)}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
65
 
66
  # Save temporary workflow
67
  with open(temp_workflow_path, "w") as f:
68
  json.dump(workflow, f)
69
 
70
  # Run ComfyUI via subprocess
 
71
  comfyui_main = os.path.join(comfyui_dir, "main.py")
72
  output_dir = os.path.join(comfyui_dir, "output") # ComfyUI default output dir
73
  os.makedirs(output_dir, exist_ok=True)
74
 
75
  try:
76
+ # Note: ComfyUI's main.py doesn't use --input-directory or --output-directory directly; it reads from workflow
77
  result = subprocess.run(
78
+ ["python", comfyui_main],
79
  cwd=comfyui_dir,
80
  capture_output=True,
81
  text=True,