Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|