Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,12 @@ 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 |
source1 = open(image1, "rb").read()
|
16 |
source2 = open(image2, "rb").read()
|
@@ -58,33 +60,38 @@ def image_prompt(prompt, image1, image2, image3, image4):
|
|
58 |
result = response.json()
|
59 |
|
60 |
job_id = result.get('job_id')
|
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 |
else:
|
87 |
-
return None, "
|
88 |
|
89 |
def gradio_app():
|
90 |
with gr.Blocks() as demo:
|
@@ -97,9 +104,20 @@ def gradio_app():
|
|
97 |
output_image = gr.Image(label="Generated Image")
|
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=
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
demo.launch()
|
104 |
|
105 |
if __name__ == "__main__":
|
|
|
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 |
+
# 📂 Get the directory where the script is located
|
14 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
15 |
+
|
16 |
def image_prompt(prompt, image1, image2, image3, image4):
|
17 |
source1 = open(image1, "rb").read()
|
18 |
source2 = open(image2, "rb").read()
|
|
|
60 |
result = response.json()
|
61 |
|
62 |
job_id = result.get('job_id')
|
63 |
+
return job_id
|
64 |
+
|
65 |
+
def query_status(job_id):
|
66 |
+
session = requests.Session()
|
67 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
68 |
+
session.mount('http://', HTTPAdapter(max_retries=retries))
|
69 |
+
|
70 |
+
query_url = f"http://18.119.36.46:8888/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
71 |
+
response = session.get(query_url, timeout=10) # Increase timeout as needed
|
72 |
+
job_data = response.json()
|
73 |
+
|
74 |
+
job_stage = job_data.get("job_stage")
|
75 |
+
|
76 |
+
if job_stage == "SUCCESS":
|
77 |
+
final_image_url = job_data.get("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 |
+
return image, "Job completed successfully."
|
83 |
+
else:
|
84 |
+
return None, "Final image URL not found in the job data."
|
85 |
+
elif job_stage == "RUNNING":
|
86 |
+
step_preview_base64 = job_data.get("job_step_preview")
|
87 |
+
if step_preview_base64:
|
88 |
+
image = Image.open(BytesIO(base64.b64decode(step_preview_base64)))
|
89 |
+
return image, "Job is running, step preview available."
|
90 |
+
return None, "Job is running, no step preview available."
|
91 |
+
elif job_stage == "FAILED":
|
92 |
+
return None, "Job failed."
|
93 |
else:
|
94 |
+
return None, "Unknown job stage."
|
95 |
|
96 |
def gradio_app():
|
97 |
with gr.Blocks() as demo:
|
|
|
104 |
output_image = gr.Image(label="Generated Image")
|
105 |
status = gr.Textbox(label="Status")
|
106 |
|
107 |
+
job_id = gr.State()
|
108 |
+
|
109 |
generate_button = gr.Button("Generate Image")
|
110 |
+
generate_button.click(image_prompt, inputs=[prompt, image1, image2, image3, image4], outputs=job_id)
|
111 |
|
112 |
+
def update_image(job_id):
|
113 |
+
if job_id:
|
114 |
+
image, status_text = query_status(job_id)
|
115 |
+
return image, status_text
|
116 |
+
return None, "No job ID found."
|
117 |
+
|
118 |
+
# ⏲️ Update the image every 2 seconds
|
119 |
+
demo.load(update_image, inputs=job_id, outputs=[output_image, status], every=2)
|
120 |
+
|
121 |
demo.launch()
|
122 |
|
123 |
if __name__ == "__main__":
|