Spaces:
Runtime error
Runtime error
rayochoajr
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -9,84 +9,88 @@ from PIL import Image
|
|
9 |
from io import BytesIO
|
10 |
import os
|
11 |
|
12 |
-
# Your host address - this will need to scale in the future
|
13 |
host = "http://18.119.36.46:8888"
|
14 |
|
15 |
def image_prompt(prompt, image1, image2, image3, image4):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
# Initiating the job
|
41 |
-
response = session.post(
|
42 |
-
url=f"{host}/v2/generation/text-to-image-with-ip",
|
43 |
-
data=json.dumps(params),
|
44 |
-
headers={"Content-Type": "application/json"},
|
45 |
-
timeout=10 # Timeout can be adjusted as needed
|
46 |
-
)
|
47 |
-
response.raise_for_status() # Ensure we catch any HTTP errors
|
48 |
-
result = response.json()
|
49 |
-
job_id = result.get('job_id')
|
50 |
-
|
51 |
-
if job_id:
|
52 |
-
while True:
|
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 |
-
response.raise_for_status() # Catch any issues with querying the job
|
56 |
-
job_data = response.json()
|
57 |
-
|
58 |
-
job_stage = job_data.get("job_stage")
|
59 |
-
job_step_preview = job_data.get("job_step_preview")
|
60 |
-
job_result = job_data.get("job_result")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
break
|
75 |
-
else:
|
76 |
-
yield None, "Final image URL not found. Something went amiss."
|
77 |
-
break
|
78 |
-
|
79 |
-
elif job_stage == "FAILED":
|
80 |
-
yield None, "Job failed. Let's check the parameters and try again."
|
81 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
yield None, "Job ID not found. Did we miss something in the setup?"
|
87 |
-
|
88 |
-
except Exception as e:
|
89 |
-
yield None, f"An error occurred: {str(e)}. We'll need to debug this."
|
90 |
|
91 |
def gradio_app():
|
92 |
with gr.Blocks() as demo:
|
|
|
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 |
+
source1 = open(image1, "rb").read()
|
16 |
+
source2 = open(image2, "rb").read()
|
17 |
+
source3 = open(image3, "rb").read()
|
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 |
+
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 # Increase timeout as needed
|
57 |
+
)
|
58 |
+
result = response.json()
|
59 |
+
|
60 |
+
job_id = result.get('job_id')
|
61 |
+
if job_id:
|
62 |
+
while True:
|
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 |
+
# Update image and status in real-time
|
72 |
+
if job_stage == "RUNNING" and job_step_preview:
|
73 |
+
image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
|
74 |
+
yield image, f"Job is running. Stage: {job_stage}"
|
75 |
|
76 |
+
elif job_stage == "SUCCESS":
|
77 |
+
final_image_url = job_result[0].get("url")
|
78 |
+
if final_image_url:
|
79 |
+
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
80 |
+
image_response = session.get(final_image_url, timeout=10) # Increase timeout as needed
|
81 |
+
image = Image.open(BytesIO(image_response.content))
|
82 |
+
yield image, "Job completed successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
break
|
84 |
+
else:
|
85 |
+
yield None, "Final image URL not found in the job data."
|
86 |
+
break
|
87 |
+
elif job_stage == "FAILED":
|
88 |
+
yield None, "Job failed."
|
89 |
+
break
|
90 |
|
91 |
+
time.sleep(2) # Wait 2 seconds before the next update
|
92 |
+
else:
|
93 |
+
yield None, "Job ID not found."
|
|
|
|
|
|
|
|
|
94 |
|
95 |
def gradio_app():
|
96 |
with gr.Blocks() as demo:
|