Lupara90 commited on
Commit
a29d7ef
·
verified ·
1 Parent(s): 62abdbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -17
app.py CHANGED
@@ -1,46 +1,79 @@
1
  import os
 
 
2
  import json
3
  import gradio as gr
4
  import torch
5
- import spaces # Required for Hugging Face ZeroGPU decoration
6
 
7
- # 1. Initialize ComfyUI programmatically under the hood
8
- # This loads ComfyUI's core engine without launching the web server UI
9
- import sys
10
- sys.path.append("./ComfyUI")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  import main as comfy_main
12
 
13
- # 2. Define the isolated inference function that borrows the free GPU
14
- @spaces.gpu(duration=60) # Gives you temporary access to an A100/H200 GPU
 
 
 
15
  def generate_image(user_prompt):
16
  # Load your exported ComfyUI API JSON
 
 
 
 
17
  with open("workflow_api.json", "r") as f:
18
  prompt_workflow = json.load(f)
19
 
20
- # TARGET THE EXACT NODE ID:
21
- # Open your JSON to find which ID belongs to your "CLIP Text Encode" node
22
- # Replace '6' with your specific text prompt node ID
23
  prompt_workflow["6"]["inputs"]["text"] = user_prompt
24
 
25
  # Execute the workflow using ComfyUI's internal API handler
26
- # (ComfyUI processes this instantly via the borrowed GPU)
27
  output_images = comfy_main.execute_workflow_directly(prompt_workflow)
28
-
29
  # Return the generated image path
30
- return output_images[0]
 
 
 
31
 
32
- # 3. Create a clean, elegant web form for your users using Gradio
 
 
33
  with gr.Blocks() as demo:
34
  gr.Markdown("# My Custom ComfyUI App")
35
  gr.Markdown("Enter a prompt below to run my custom ComfyUI workflow online for free.")
36
 
37
  with gr.Row():
38
  with gr.Column():
39
- prompt_input = gr.Textbox(label="Your Prompt", placeholder="An astronaut riding a horse...")
40
  submit_btn = gr.Button("Generate")
41
  with gr.Column():
42
  image_output = gr.Image(label="Result")
43
 
44
- submit_btn.click(fn=generate_image, inputs=prompt_input, outputs=image_output)
 
 
 
 
45
 
46
- demo.launch()
 
 
 
1
  import os
2
+ import sys
3
+ import subprocess
4
  import json
5
  import gradio as gr
6
  import torch
7
+ import spaces # Required for Hugging Face ZeroGPU decoration
8
 
9
+ # ==========================================
10
+ # 1. Dynamically Clone & Initialize ComfyUI
11
+ # ==========================================
12
+ COMFYUI_DIR = os.path.abspath("ComfyUI")
13
+
14
+ # If ComfyUI hasn't been cloned yet, clone it
15
+ if not os.path.exists(COMFYUI_DIR):
16
+ print("ComfyUI directory not found. Cloning repository...")
17
+ subprocess.run(
18
+ ["git", "clone", "https://github.com/comfyanonymous/ComfyUI.git", COMFYUI_DIR],
19
+ check=True
20
+ )
21
+
22
+ # Append the cloned ComfyUI folder to python path so its modules (like main.py) can be found
23
+ if COMFYUI_DIR not in sys.path:
24
+ sys.path.append(COMFYUI_DIR)
25
+
26
+ # Now it is safe to import main
27
  import main as comfy_main
28
 
29
+
30
+ # ==========================================
31
+ # 2. Define GPU Inference Function
32
+ # ==========================================
33
+ @spaces.gpu(duration=60) # Borrow free ZeroGPU for 60 seconds per run
34
  def generate_image(user_prompt):
35
  # Load your exported ComfyUI API JSON
36
+ # (Make sure workflow_api.json is uploaded to your Space's main directory)
37
+ if not os.path.exists("workflow_api.json"):
38
+ return None # Or handle error appropriately
39
+
40
  with open("workflow_api.json", "r") as f:
41
  prompt_workflow = json.load(f)
42
 
43
+ # TARGET THE EXACT NODE ID:
44
+ # Open your workflow_api.json to verify which ID belongs to your "CLIP Text Encode" node.
45
+ # Replace '6' with your specific text prompt node ID if different.
46
  prompt_workflow["6"]["inputs"]["text"] = user_prompt
47
 
48
  # Execute the workflow using ComfyUI's internal API handler
 
49
  output_images = comfy_main.execute_workflow_directly(prompt_workflow)
50
+
51
  # Return the generated image path
52
+ if output_images and len(output_images) > 0:
53
+ return output_images[0]
54
+ return None
55
+
56
 
57
+ # ==========================================
58
+ # 3. Create Gradio Web Interface
59
+ # ==========================================
60
  with gr.Blocks() as demo:
61
  gr.Markdown("# My Custom ComfyUI App")
62
  gr.Markdown("Enter a prompt below to run my custom ComfyUI workflow online for free.")
63
 
64
  with gr.Row():
65
  with gr.Column():
66
+ prompt_input = gr.Textbox(label="Prompt", placeholder="A futuristic city...")
67
  submit_btn = gr.Button("Generate")
68
  with gr.Column():
69
  image_output = gr.Image(label="Result")
70
 
71
+ submit_btn.click(
72
+ fn=generate_image,
73
+ inputs=prompt_input,
74
+ outputs=image_output
75
+ )
76
 
77
+ # Start the application
78
+ if __name__ == "__main__":
79
+ demo.launch()