arthurdfr commited on
Commit
b577d15
·
verified ·
1 Parent(s): 6d4f237

Add optimized ComfyUI interface with reduced max_attempts and delay.

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ time
5
+
6
+ # Configuration
7
+ token = "YOUR_TOKEN" # Replace with your token if needed
8
+ api_base_url = "https://api.comfyui.com" # Replace with the actual API base URL
9
+
10
+ # Load the workflow JSON file
11
+ def load_workflow_json(file_path):
12
+ with open(file_path, "r") as file:
13
+ return json.load(file)
14
+
15
+ # Load the workflow
16
+ workflow = load_workflow_json("workflow.json")
17
+
18
+ # Function to queue a prompt for processing
19
+ def queue_prompt(prompt):
20
+ response = requests.post(
21
+ f"{api_base_url}/prompt",
22
+ headers={"Authorization": f"Bearer {token}"},
23
+ json={"prompt": prompt}
24
+ )
25
+ return response.json()
26
+
27
+ # Function to get the status of a queued prompt
28
+ def get_prompt_status(prompt_id):
29
+ response = requests.get(
30
+ f"{api_base_url}/prompt/{prompt_id}",
31
+ headers={"Authorization": f"Bearer {token}"}
32
+ )
33
+ return response.json()
34
+
35
+ # Function to generate an image
36
+ def generate_image(positive_prompt, negative_prompt, cfg_scale, steps):
37
+ # Update the workflow with the input parameters
38
+ workflow["positive_prompt"] = positive_prompt
39
+ workflow["negative_prompt"] = negative_prompt
40
+ workflow["cfg_scale"] = cfg_scale
41
+ workflow["steps"] = steps
42
+
43
+ # Queue the prompt
44
+ prompt_response = queue_prompt(workflow)
45
+ prompt_id = prompt_response.get("id")
46
+
47
+ # Wait for the prompt to be processed
48
+ max_attempts = 30 # Reduced from 60
49
+ for _ in range(max_attempts):
50
+ status_response = get_prompt_status(prompt_id)
51
+ status = status_response.get("status")
52
+ if status == "completed":
53
+ # Retrieve the generated image
54
+ image_url = status_response.get("result")
55
+ return image_url
56
+ time.sleep(0.5) # Reduced delay from 1 second
57
+
58
+ return "Image generation timed out."
59
+
60
+ # Gradio interface
61
+ demo = gr.Interface(
62
+ fn=generate_image,
63
+ inputs=[
64
+ gr.inputs.Textbox(label="Positive Prompt"),
65
+ gr.inputs.Textbox(label="Negative Prompt"),
66
+ gr.inputs.Slider(minimum=1, maximum=20, default=7, label="CFG Scale"),
67
+ gr.inputs.Slider(minimum=10, maximum=100, default=50, label="Steps")
68
+ ],
69
+ outputs=gr.outputs.Image(label="Generated Image")
70
+ )
71
+
72
+ demo.launch()