Update app.py
Browse files
app.py
CHANGED
@@ -7,84 +7,79 @@ import time
|
|
7 |
import gradio as gr
|
8 |
from PIL import Image
|
9 |
from io import BytesIO
|
10 |
-
import os
|
11 |
|
12 |
host = "http://18.119.36.46:8888"
|
13 |
|
14 |
def image_prompt(prompt, image1, image2, image3, image4):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
source4 = open(image4, "rb").read()
|
19 |
-
|
20 |
-
params = {
|
21 |
-
"prompt": prompt,
|
22 |
-
"image_prompts": [
|
23 |
-
{
|
24 |
-
"cn_img": base64.b64encode(source1).decode('utf-8'),
|
25 |
-
"cn_stop": 1,
|
26 |
-
"cn_weight": 1,
|
27 |
-
"cn_type": "ImagePrompt"
|
28 |
-
},{
|
29 |
-
"cn_img": base64.b64encode(source2).decode('utf-8'),
|
30 |
-
"cn_stop": 1,
|
31 |
-
"cn_weight": 1,
|
32 |
-
"cn_type": "ImagePrompt"
|
33 |
-
},{
|
34 |
-
"cn_img": base64.b64encode(source3).decode('utf-8'),
|
35 |
-
"cn_stop": 1,
|
36 |
-
"cn_weight": 1,
|
37 |
-
"cn_type": "ImagePrompt"
|
38 |
-
},{
|
39 |
-
"cn_img": base64.b64encode(source4).decode('utf-8'),
|
40 |
-
"cn_stop": 1,
|
41 |
-
"cn_weight": 1,
|
42 |
-
"cn_type": "ImagePrompt"
|
43 |
-
}
|
44 |
-
],
|
45 |
-
"async_process": True
|
46 |
-
}
|
47 |
-
|
48 |
-
session = requests.Session()
|
49 |
-
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
50 |
-
session.mount('http://', HTTPAdapter(max_retries=retries))
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
def gradio_app():
|
90 |
with gr.Blocks() as demo:
|
@@ -98,7 +93,7 @@ def gradio_app():
|
|
98 |
status = gr.Textbox(label="Status")
|
99 |
|
100 |
generate_button = gr.Button("Generate Image")
|
101 |
-
generate_button.click(image_prompt, inputs=[prompt, image1, image2, image3, image4], outputs=[output_image, status])
|
102 |
|
103 |
demo.launch()
|
104 |
|
|
|
7 |
import gradio as gr
|
8 |
from PIL import Image
|
9 |
from io import BytesIO
|
|
|
10 |
|
11 |
host = "http://18.119.36.46:8888"
|
12 |
|
13 |
def image_prompt(prompt, image1, image2, image3, image4):
|
14 |
+
try:
|
15 |
+
image_sources = [open(image, "rb").read() for image in [image1, image2, image3, image4]]
|
16 |
+
encoded_images = [base64.b64encode(img).decode('utf-8') for img in image_sources]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
params = {
|
19 |
+
"prompt": prompt,
|
20 |
+
"image_prompts": [
|
21 |
+
{
|
22 |
+
"cn_img": encoded_img,
|
23 |
+
"cn_stop": 1,
|
24 |
+
"cn_weight": 1,
|
25 |
+
"cn_type": "ImagePrompt"
|
26 |
+
} for encoded_img in encoded_images
|
27 |
+
],
|
28 |
+
"async_process": True
|
29 |
+
}
|
30 |
+
|
31 |
+
session = requests.Session()
|
32 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
33 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
34 |
+
|
35 |
+
response = session.post(
|
36 |
+
url=f"{host}/v2/generation/text-to-image-with-ip",
|
37 |
+
data=json.dumps(params),
|
38 |
+
headers={"Content-Type": "application/json"},
|
39 |
+
timeout=10
|
40 |
+
)
|
41 |
+
response.raise_for_status()
|
42 |
+
result = response.json()
|
43 |
+
job_id = result.get('job_id')
|
44 |
+
|
45 |
+
if job_id:
|
46 |
+
while True:
|
47 |
+
query_url = f"{host}/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
48 |
+
response = session.get(query_url, timeout=10)
|
49 |
+
response.raise_for_status()
|
50 |
+
job_data = response.json()
|
51 |
+
|
52 |
+
job_stage = job_data.get("job_stage")
|
53 |
+
job_step_preview = job_data.get("job_step_preview")
|
54 |
+
job_result = job_data.get("job_result")
|
55 |
+
|
56 |
+
if job_stage == "RUNNING" and job_step_preview:
|
57 |
+
image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
|
58 |
+
yield image, f"Job is running smoothly. Current stage: {job_stage}. Hang tight!"
|
59 |
+
|
60 |
+
elif job_stage == "SUCCESS":
|
61 |
+
final_image_url = job_result[0].get("url")
|
62 |
+
if final_image_url:
|
63 |
+
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
64 |
+
image_response = session.get(final_image_url, timeout=10)
|
65 |
+
image = Image.open(BytesIO(image_response.content))
|
66 |
+
yield image, "Job completed successfully. Enjoy your masterpiece!"
|
67 |
+
break
|
68 |
+
else:
|
69 |
+
yield None, "Final image URL not found. Something went amiss."
|
70 |
+
break
|
71 |
+
|
72 |
+
elif job_stage == "FAILED":
|
73 |
+
yield None, "Job failed. Let's check the parameters and try again."
|
74 |
+
break
|
75 |
+
|
76 |
+
time.sleep(2)
|
77 |
+
|
78 |
+
else:
|
79 |
+
yield None, "Job ID not found. Did we miss something in the setup?"
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
yield None, f"An error occurred: {str(e)}. We'll need to debug this."
|
83 |
|
84 |
def gradio_app():
|
85 |
with gr.Blocks() as demo:
|
|
|
93 |
status = gr.Textbox(label="Status")
|
94 |
|
95 |
generate_button = gr.Button("Generate Image")
|
96 |
+
generate_button.click(fn=image_prompt, inputs=[prompt, image1, image2, image3, image4], outputs=[output_image, status], stream=True)
|
97 |
|
98 |
demo.launch()
|
99 |
|