Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,88 +12,76 @@ import os
|
|
12 |
host = "http://18.119.36.46:8888"
|
13 |
|
14 |
def image_prompt(prompt, image1, image2, image3, image4):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
response = session.post(
|
53 |
url=f"{host}/v2/generation/text-to-image-with-ip",
|
54 |
data=json.dumps(params),
|
55 |
headers={"Content-Type": "application/json"},
|
56 |
-
timeout=10
|
57 |
)
|
|
|
58 |
result = response.json()
|
59 |
-
|
60 |
job_id = result.get('job_id')
|
61 |
-
|
62 |
-
|
63 |
-
query_url = f"{host}/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
64 |
-
response = session.get(query_url, timeout=10) # Increase timeout as needed
|
65 |
-
job_data = response.json()
|
66 |
-
|
67 |
-
job_stage = job_data.get("job_stage")
|
68 |
-
job_step_preview = job_data.get("job_step_preview")
|
69 |
-
job_result = job_data.get("job_result")
|
70 |
-
|
71 |
-
# Continuously print out or display the attributes every 2 seconds
|
72 |
-
print(f"Job Stage: {job_stage}")
|
73 |
-
if job_step_preview:
|
74 |
-
print("Step Preview Available")
|
75 |
-
image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
|
76 |
-
if job_result:
|
77 |
-
print(f"Job Result: {job_result}")
|
78 |
-
|
79 |
-
if job_stage == "SUCCESS":
|
80 |
-
final_image_url = job_result[0].get("url")
|
81 |
-
if final_image_url:
|
82 |
-
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
83 |
-
image_response = session.get(final_image_url, timeout=10) # Increase timeout as needed
|
84 |
-
image = Image.open(BytesIO(image_response.content))
|
85 |
-
return image, "Job completed successfully."
|
86 |
-
else:
|
87 |
-
return None, "Final image URL not found in the job data."
|
88 |
-
elif job_stage == "RUNNING":
|
89 |
-
if job_step_preview:
|
90 |
-
image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
|
91 |
-
time.sleep(2) # Wait 2 seconds before the next update
|
92 |
-
elif job_stage == "FAILED":
|
93 |
-
return None, "Job failed."
|
94 |
-
else:
|
95 |
return None, "Job ID not found."
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
def gradio_app():
|
98 |
with gr.Blocks() as demo:
|
99 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
|
|
|
12 |
host = "http://18.119.36.46:8888"
|
13 |
|
14 |
def image_prompt(prompt, image1, image2, image3, image4):
|
15 |
+
session = requests.Session()
|
16 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
17 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
18 |
+
|
19 |
+
# Read and encode images
|
20 |
+
image_paths = [image1, image2, image3, image4]
|
21 |
+
image_data = [
|
22 |
+
{
|
23 |
+
"cn_img": base64.b64encode(open(image_path, "rb").read()).decode('utf-8'),
|
24 |
+
"cn_stop": 1,
|
25 |
+
"cn_weight": 1,
|
26 |
+
"cn_type": "ImagePrompt"
|
27 |
+
} for image_path in image_paths if image_path
|
28 |
+
]
|
29 |
+
|
30 |
params = {
|
31 |
"prompt": prompt,
|
32 |
+
"image_prompts": image_data,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
"async_process": True
|
34 |
}
|
|
|
|
|
|
|
|
|
35 |
|
36 |
response = session.post(
|
37 |
url=f"{host}/v2/generation/text-to-image-with-ip",
|
38 |
data=json.dumps(params),
|
39 |
headers={"Content-Type": "application/json"},
|
40 |
+
timeout=10
|
41 |
)
|
42 |
+
|
43 |
result = response.json()
|
|
|
44 |
job_id = result.get('job_id')
|
45 |
+
|
46 |
+
if not job_id:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return None, "Job ID not found."
|
48 |
|
49 |
+
# Polling for job status
|
50 |
+
start_time = time.time()
|
51 |
+
max_wait_time = 300 # 5 minutes max wait time
|
52 |
+
while time.time() - start_time < max_wait_time:
|
53 |
+
query_url = f"{host}/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
54 |
+
response = session.get(query_url, timeout=10)
|
55 |
+
job_data = response.json()
|
56 |
+
|
57 |
+
job_stage = job_data.get("job_stage")
|
58 |
+
job_step_preview = job_data.get("job_step_preview")
|
59 |
+
job_result = job_data.get("job_result")
|
60 |
+
|
61 |
+
# If there is a step preview, display it
|
62 |
+
if job_step_preview:
|
63 |
+
step_image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
|
64 |
+
return step_image, "Processing..." # Update the gr.Image widget with step preview
|
65 |
+
|
66 |
+
# If the job is completed successfully, display the final image
|
67 |
+
if job_stage == "SUCCESS":
|
68 |
+
final_image_url = job_result[0].get("url")
|
69 |
+
if final_image_url:
|
70 |
+
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
71 |
+
image_response = session.get(final_image_url, timeout=10)
|
72 |
+
final_image = Image.open(BytesIO(image_response.content))
|
73 |
+
return final_image, "Job completed successfully."
|
74 |
+
return None, "Final image URL not found in the job data."
|
75 |
+
|
76 |
+
# If the job failed
|
77 |
+
elif job_stage == "FAILED":
|
78 |
+
return None, "Job failed."
|
79 |
+
|
80 |
+
# If the job is still running, continue polling
|
81 |
+
time.sleep(2)
|
82 |
+
|
83 |
+
return None, "Job timed out."
|
84 |
+
|
85 |
def gradio_app():
|
86 |
with gr.Blocks() as demo:
|
87 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
|