Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import requests
|
2 |
-
import os
|
3 |
from requests.adapters import HTTPAdapter
|
4 |
from requests.packages.urllib3.util.retry import Retry
|
5 |
import json
|
@@ -8,91 +7,112 @@ import time
|
|
8 |
import gradio as gr
|
9 |
from PIL import Image
|
10 |
from io import BytesIO
|
|
|
11 |
|
12 |
host = "http://18.119.36.46:8888"
|
13 |
|
14 |
-
# 📂 Get the directory where the script is located
|
15 |
-
script_dir = os.path.dirname(os.path.abspath(__file__))
|
16 |
-
|
17 |
def image_prompt(prompt, image1, image2, image3, image4):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
def query_status(job_id):
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
if
|
80 |
-
final_image_url =
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
else:
|
85 |
-
return None, "
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
return None, "Job failed."
|
94 |
-
else:
|
95 |
-
return None, "Unknown job stage."
|
96 |
|
97 |
def gradio_app():
|
98 |
with gr.Blocks() as demo:
|
@@ -116,10 +136,9 @@ def gradio_app():
|
|
116 |
return image, status_text
|
117 |
return None, "No job ID found."
|
118 |
|
119 |
-
# ⏲️ Update the image every 2 seconds
|
120 |
demo.load(update_image, inputs=job_id, outputs=[output_image, status], every=2)
|
121 |
|
122 |
demo.launch()
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
-
gradio_app()
|
|
|
1 |
import requests
|
|
|
2 |
from requests.adapters import HTTPAdapter
|
3 |
from requests.packages.urllib3.util.retry import Retry
|
4 |
import json
|
|
|
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 |
+
try:
|
16 |
+
with open(image1, "rb") as img1, open(image2, "rb") as img2, open(image3, "rb") as img3, open(image4, "rb") as img4:
|
17 |
+
source1 = img1.read()
|
18 |
+
source2 = img2.read()
|
19 |
+
source3 = img3.read()
|
20 |
+
source4 = img4.read()
|
21 |
+
|
22 |
+
params = {
|
23 |
+
"prompt": prompt,
|
24 |
+
"image_prompts": [
|
25 |
+
{
|
26 |
+
"cn_img": base64.b64encode(source1).decode('utf-8'),
|
27 |
+
"cn_stop": 1,
|
28 |
+
"cn_weight": 1,
|
29 |
+
"cn_type": "ImagePrompt"
|
30 |
+
},{
|
31 |
+
"cn_img": base64.b64encode(source2).decode('utf-8'),
|
32 |
+
"cn_stop": 1,
|
33 |
+
"cn_weight": 1,
|
34 |
+
"cn_type": "ImagePrompt"
|
35 |
+
},{
|
36 |
+
"cn_img": base64.b64encode(source3).decode('utf-8'),
|
37 |
+
"cn_stop": 1,
|
38 |
+
"cn_weight": 1,
|
39 |
+
"cn_type": "ImagePrompt"
|
40 |
+
},{
|
41 |
+
"cn_img": base64.b64encode(source4).decode('utf-8'),
|
42 |
+
"cn_stop": 1,
|
43 |
+
"cn_weight": 1,
|
44 |
+
"cn_type": "ImagePrompt"
|
45 |
+
}
|
46 |
+
],
|
47 |
+
"async_process": True
|
48 |
+
}
|
49 |
+
|
50 |
+
session = requests.Session()
|
51 |
+
retries = Retry(total=5, backoff_factor=2, status_forcelist=[502, 503, 504])
|
52 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
53 |
|
54 |
+
response = session.post(
|
55 |
+
url=f"{host}/v2/generation/text-to-image-with-ip",
|
56 |
+
data=json.dumps(params),
|
57 |
+
headers={"Content-Type": "application/json"},
|
58 |
+
timeout=15 # Slightly increased timeout
|
59 |
+
)
|
60 |
+
response.raise_for_status() # Raise exception for HTTP errors
|
61 |
+
result = response.json()
|
62 |
+
|
63 |
+
job_id = result.get('job_id')
|
64 |
+
if not job_id:
|
65 |
+
raise ValueError("Job ID not found in response")
|
66 |
+
|
67 |
+
return job_id
|
68 |
+
|
69 |
+
except requests.exceptions.RequestException as e:
|
70 |
+
return None, f"API request failed: {str(e)}"
|
71 |
+
except json.JSONDecodeError:
|
72 |
+
return None, "Failed to decode JSON response from the server."
|
73 |
+
except Exception as e:
|
74 |
+
return None, f"An unexpected error occurred: {str(e)}"
|
75 |
|
76 |
def query_status(job_id):
|
77 |
+
try:
|
78 |
+
session = requests.Session()
|
79 |
+
retries = Retry(total=5, backoff_factor=2, status_forcelist=[502, 503, 504])
|
80 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
81 |
+
|
82 |
+
query_url = f"http://18.119.36.46:8888/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
83 |
+
response = session.get(query_url, timeout=15)
|
84 |
+
response.raise_for_status() # Raise exception for HTTP errors
|
85 |
+
job_data = response.json()
|
86 |
+
|
87 |
+
job_stage = job_data.get("job_stage")
|
88 |
+
|
89 |
+
if job_stage == "SUCCESS":
|
90 |
+
final_image_url = job_data.get("job_result")[0].get("url")
|
91 |
+
if final_image_url:
|
92 |
+
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
93 |
+
image_response = session.get(final_image_url, timeout=15)
|
94 |
+
image_response.raise_for_status() # Raise exception for HTTP errors
|
95 |
+
image = Image.open(BytesIO(image_response.content))
|
96 |
+
return image, "Job completed successfully."
|
97 |
+
else:
|
98 |
+
return None, "Final image URL not found in the job data."
|
99 |
+
elif job_stage == "RUNNING":
|
100 |
+
step_preview_base64 = job_data.get("job_step_preview")
|
101 |
+
if step_preview_base64:
|
102 |
+
image = Image.open(BytesIO(base64.b64decode(step_preview_base64)))
|
103 |
+
return image, "Job is running, step preview available."
|
104 |
+
return None, "Job is running, no step preview available."
|
105 |
+
elif job_stage == "FAILED":
|
106 |
+
return None, "Job failed."
|
107 |
else:
|
108 |
+
return None, "Unknown job stage."
|
109 |
+
|
110 |
+
except requests.exceptions.RequestException as e:
|
111 |
+
return None, f"API request failed: {str(e)}"
|
112 |
+
except json.JSONDecodeError:
|
113 |
+
return None, "Failed to decode JSON response from the server."
|
114 |
+
except Exception as e:
|
115 |
+
return None, f"An unexpected error occurred: {str(e)}"
|
|
|
|
|
|
|
116 |
|
117 |
def gradio_app():
|
118 |
with gr.Blocks() as demo:
|
|
|
136 |
return image, status_text
|
137 |
return None, "No job ID found."
|
138 |
|
|
|
139 |
demo.load(update_image, inputs=job_id, outputs=[output_image, status], every=2)
|
140 |
|
141 |
demo.launch()
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
+
gradio_app()
|