Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,162 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
|
29 |
-
prompt
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
-
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
-
|
58 |
-
with gr.Blocks(css=css) as demo:
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
|
68 |
-
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
1 |
+
"""
|
2 |
+
{
|
3 |
+
"inputs": {
|
4 |
+
"prompt": "A text prompt to generate the image from.",
|
5 |
+
"image_prompts": [
|
6 |
+
{
|
7 |
+
"cn_img": "Base64 encoded image data for the first image prompt.",
|
8 |
+
"cn_stop": "ControlNet stop value for the first image prompt.",
|
9 |
+
"cn_weight": "ControlNet weight value for the first image prompt.",
|
10 |
+
"cn_type": "Type of the first image prompt."
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cn_img": "Base64 encoded image data for the second image prompt.",
|
14 |
+
"cn_stop": "ControlNet stop value for the second image prompt.",
|
15 |
+
"cn_weight": "ControlNet weight value for the second image prompt.",
|
16 |
+
"cn_type": "Type of the second image prompt."
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"cn_img": "Base64 encoded image data for the third image prompt.",
|
20 |
+
"cn_stop": "ControlNet stop value for the third image prompt.",
|
21 |
+
"cn_weight": "ControlNet weight value for the third image prompt.",
|
22 |
+
"cn_type": "Type of the third image prompt."
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cn_img": "Base64 encoded image data for the fourth image prompt.",
|
26 |
+
"cn_stop": "ControlNet stop value for the fourth image prompt.",
|
27 |
+
"cn_weight": "ControlNet weight value for the fourth image prompt.",
|
28 |
+
"cn_type": "Type of the fourth image prompt."
|
29 |
+
}
|
30 |
+
],
|
31 |
+
"async_process": "Boolean to indicate if the process should be asynchronous."
|
32 |
+
},
|
33 |
+
"outputs": {
|
34 |
+
"job_id": "The ID of the job submitted for image generation.",
|
35 |
+
"job_stage": "The current stage of the job (e.g., RUNNING, SUCCESS, FAILED).",
|
36 |
+
"final_image_url": "The URL of the final generated image.",
|
37 |
+
"step_preview": "Base64 encoded step preview image data."
|
38 |
+
}
|
39 |
+
}
|
40 |
+
"""
|
41 |
|
42 |
+
import requests
|
43 |
+
from requests.adapters import HTTPAdapter
|
44 |
+
from requests.packages.urllib3.util.retry import Retry
|
45 |
+
import json
|
46 |
+
import base64
|
47 |
+
import time
|
48 |
+
import gradio as gr
|
49 |
+
from PIL import Image
|
50 |
+
import os
|
51 |
|
52 |
+
host = "http://18.119.36.46:8888"
|
|
|
53 |
|
54 |
+
# 📂 Get the directory where the script is located
|
55 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
56 |
|
57 |
+
def image_prompt(prompt, image1, image2, image3, image4):
|
58 |
+
source1 = open(image1, "rb").read()
|
59 |
+
source2 = open(image2, "rb").read()
|
60 |
+
source3 = open(image3, "rb").read()
|
61 |
+
source4 = open(image4, "rb").read()
|
62 |
|
63 |
+
params = {
|
64 |
+
"prompt": prompt,
|
65 |
+
"image_prompts": [
|
66 |
+
{
|
67 |
+
"cn_img": base64.b64encode(source1).decode('utf-8'),
|
68 |
+
"cn_stop": 1,
|
69 |
+
"cn_weight": 1,
|
70 |
+
"cn_type": "ImagePrompt"
|
71 |
+
},{
|
72 |
+
"cn_img": base64.b64encode(source2).decode('utf-8'),
|
73 |
+
"cn_stop": 1,
|
74 |
+
"cn_weight": 1,
|
75 |
+
"cn_type": "ImagePrompt"
|
76 |
+
},{
|
77 |
+
"cn_img": base64.b64encode(source3).decode('utf-8'),
|
78 |
+
"cn_stop": 1,
|
79 |
+
"cn_weight": 1,
|
80 |
+
"cn_type": "ImagePrompt"
|
81 |
+
},{
|
82 |
+
"cn_img": base64.b64encode(source4).decode('utf-8'),
|
83 |
+
"cn_stop": 1,
|
84 |
+
"cn_weight": 1,
|
85 |
+
"cn_type": "ImagePrompt"
|
86 |
+
}
|
87 |
+
],
|
88 |
+
"async_process": True
|
89 |
+
}
|
90 |
|
91 |
+
session = requests.Session()
|
92 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
93 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
94 |
|
95 |
+
response = session.post(
|
96 |
+
url=f"{host}/v2/generation/text-to-image-with-ip",
|
97 |
+
data=json.dumps(params),
|
98 |
+
headers={"Content-Type": "application/json"},
|
99 |
+
timeout=10 # Increase timeout as needed
|
100 |
+
)
|
101 |
+
result = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
job_id = result.get('job_id')
|
104 |
+
if job_id:
|
105 |
+
while True:
|
106 |
+
query_url = f"http://18.119.36.46:8888/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
107 |
+
response = session.get(query_url, timeout=10) # Increase timeout as needed
|
108 |
+
job_data = response.json()
|
|
|
109 |
|
110 |
+
job_stage = job_data.get("job_stage")
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
if job_stage == "SUCCESS":
|
113 |
+
final_image_url = job_data.get("job_result")[0].get("url")
|
114 |
+
if final_image_url:
|
115 |
+
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
116 |
+
image_response = session.get(final_image_url, timeout=10) # Increase timeout as needed
|
117 |
+
with open("output.png", "wb") as f:
|
118 |
+
f.write(image_response.content)
|
119 |
+
return "output.png", "Job completed successfully."
|
120 |
+
else:
|
121 |
+
return None, "Final image URL not found in the job data."
|
122 |
+
elif job_stage == "RUNNING":
|
123 |
+
step_preview_base64 = job_data.get("job_step_preview")
|
124 |
+
if step_preview_base64:
|
125 |
+
with open("output.png", "wb") as f:
|
126 |
+
f.write(base64.b64decode(step_preview_base64))
|
127 |
+
time.sleep(5)
|
128 |
+
elif job_stage == "FAILED":
|
129 |
+
return None, "Job failed."
|
130 |
+
else:
|
131 |
+
return None, "Job ID not found."
|
132 |
|
133 |
+
def create_status_image():
|
134 |
+
if os.path.exists("output.png"):
|
135 |
+
return "output.png"
|
136 |
+
else:
|
137 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
def gradio_app():
|
140 |
+
with gr.Blocks() as demo:
|
141 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
|
142 |
+
with gr.Row():
|
143 |
+
image1 = gr.Image(label="Image Prompt 1", type="filepath")
|
144 |
+
image2 = gr.Image(label="Image Prompt 2", type="filepath")
|
145 |
+
image3 = gr.Image(label="Image Prompt 3", type="filepath")
|
146 |
+
image4 = gr.Image(label="Image Prompt 4", type="filepath")
|
147 |
+
output_image = gr.Image(label="Generated Image")
|
148 |
+
status = gr.Textbox(label="Status")
|
149 |
+
|
150 |
+
generate_button = gr.Button("Generate Image")
|
151 |
+
generate_button.click(image_prompt, inputs=[prompt, image1, image2, image3, image4], outputs=[output_image, status])
|
152 |
+
|
153 |
+
# 🖼️ Display the status image
|
154 |
+
status_image = gr.Image(label="Queue Status", interactive=False)
|
155 |
+
|
156 |
+
# ⏲️ Update the image every 5 seconds
|
157 |
+
demo.load(create_status_image, every=5, outputs=status_image)
|
158 |
+
|
159 |
+
demo.launch()
|
160 |
|
161 |
+
if __name__ == "__main__":
|
162 |
+
gradio_app()
|