Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
from requests.adapters import HTTPAdapter
|
3 |
from requests.packages.urllib3.util.retry import Retry
|
4 |
import json
|
5 |
import base64
|
6 |
-
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 |
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()
|
@@ -55,9 +73,9 @@ def image_prompt(prompt, image1, image2, image3, image4):
|
|
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
|
59 |
)
|
60 |
-
response.raise_for_status() #
|
61 |
result = response.json()
|
62 |
|
63 |
job_id = result.get('job_id')
|
@@ -74,6 +92,7 @@ def image_prompt(prompt, image1, image2, image3, image4):
|
|
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])
|
@@ -81,7 +100,7 @@ def query_status(job_id):
|
|
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()
|
85 |
job_data = response.json()
|
86 |
|
87 |
job_stage = job_data.get("job_stage")
|
@@ -91,7 +110,7 @@ def query_status(job_id):
|
|
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()
|
95 |
image = Image.open(BytesIO(image_response.content))
|
96 |
return image, "Job completed successfully."
|
97 |
else:
|
@@ -115,20 +134,23 @@ def query_status(job_id):
|
|
115 |
return None, f"An unexpected error occurred: {str(e)}"
|
116 |
|
117 |
def gradio_app():
|
|
|
118 |
with gr.Blocks() as demo:
|
119 |
-
|
|
|
|
|
120 |
with gr.Row():
|
121 |
image1 = gr.Image(label="Image Prompt 1", type="filepath")
|
122 |
image2 = gr.Image(label="Image Prompt 2", type="filepath")
|
123 |
image3 = gr.Image(label="Image Prompt 3", type="filepath")
|
124 |
image4 = gr.Image(label="Image Prompt 4", type="filepath")
|
125 |
-
output_image = gr.Image(label="Generated Image")
|
126 |
-
status = gr.Textbox(label="Status")
|
127 |
-
|
128 |
-
job_id = gr.State()
|
129 |
|
130 |
generate_button = gr.Button("Generate Image")
|
131 |
-
|
|
|
|
|
|
|
|
|
132 |
|
133 |
def update_image(job_id):
|
134 |
if job_id:
|
@@ -136,6 +158,7 @@ def gradio_app():
|
|
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()
|
|
|
1 |
+
"""
|
2 |
+
{
|
3 |
+
"inputs": {
|
4 |
+
"prompt": "A text prompt to generate the image from.",
|
5 |
+
"image1": "Filepath for the first image prompt.",
|
6 |
+
"image2": "Filepath for the second image prompt.",
|
7 |
+
"image3": "Filepath for the third image prompt.",
|
8 |
+
"image4": "Filepath for the fourth image prompt."
|
9 |
+
},
|
10 |
+
"outputs": {
|
11 |
+
"output_image": "The final generated image or step preview.",
|
12 |
+
"status": "Current status of the image generation process (e.g., Running, Success, Failed)."
|
13 |
+
},
|
14 |
+
"description": "This Gradio app takes a text prompt and up to four image prompts to generate an AI-powered image. It provides real-time updates on the job's progress and displays the final image once complete."
|
15 |
+
}
|
16 |
+
"""
|
17 |
+
|
18 |
import requests
|
19 |
from requests.adapters import HTTPAdapter
|
20 |
from requests.packages.urllib3.util.retry import Retry
|
21 |
import json
|
22 |
import base64
|
|
|
23 |
import gradio as gr
|
24 |
from PIL import Image
|
25 |
from io import BytesIO
|
26 |
import os
|
27 |
|
28 |
+
# Host server configuration
|
29 |
host = "http://18.119.36.46:8888"
|
30 |
|
31 |
def image_prompt(prompt, image1, image2, image3, image4):
|
32 |
+
"""Handles the image generation process by sending the text and image prompts to the server."""
|
33 |
try:
|
34 |
with open(image1, "rb") as img1, open(image2, "rb") as img2, open(image3, "rb") as img3, open(image4, "rb") as img4:
|
35 |
source1 = img1.read()
|
|
|
73 |
url=f"{host}/v2/generation/text-to-image-with-ip",
|
74 |
data=json.dumps(params),
|
75 |
headers={"Content-Type": "application/json"},
|
76 |
+
timeout=15
|
77 |
)
|
78 |
+
response.raise_for_status() # Ensure we raise errors for bad HTTP status codes
|
79 |
result = response.json()
|
80 |
|
81 |
job_id = result.get('job_id')
|
|
|
92 |
return None, f"An unexpected error occurred: {str(e)}"
|
93 |
|
94 |
def query_status(job_id):
|
95 |
+
"""Polls the server to get the current status of the image generation job."""
|
96 |
try:
|
97 |
session = requests.Session()
|
98 |
retries = Retry(total=5, backoff_factor=2, status_forcelist=[502, 503, 504])
|
|
|
100 |
|
101 |
query_url = f"http://18.119.36.46:8888/v1/generation/query-job?job_id={job_id}&require_step_preview=true"
|
102 |
response = session.get(query_url, timeout=15)
|
103 |
+
response.raise_for_status()
|
104 |
job_data = response.json()
|
105 |
|
106 |
job_stage = job_data.get("job_stage")
|
|
|
110 |
if final_image_url:
|
111 |
final_image_url = final_image_url.replace("127.0.0.1", "18.119.36.46")
|
112 |
image_response = session.get(final_image_url, timeout=15)
|
113 |
+
image_response.raise_for_status()
|
114 |
image = Image.open(BytesIO(image_response.content))
|
115 |
return image, "Job completed successfully."
|
116 |
else:
|
|
|
134 |
return None, f"An unexpected error occurred: {str(e)}"
|
135 |
|
136 |
def gradio_app():
|
137 |
+
"""Defines the Gradio app layout and functionality."""
|
138 |
with gr.Blocks() as demo:
|
139 |
+
gr.Markdown("# AI Image Generator\nEnter a text prompt and upload up to 4 images to generate a unique AI-powered image.")
|
140 |
+
|
141 |
+
prompt = gr.Textbox(label="Text Prompt", placeholder="Describe the image you want to generate")
|
142 |
with gr.Row():
|
143 |
image1 = gr.Image(label="Image Prompt 1", type="filepath")
|
144 |
image2 = gr.Image(label="Image Prompt 2", type="filepath")
|
145 |
image3 = gr.Image(label="Image Prompt 3", type="filepath")
|
146 |
image4 = gr.Image(label="Image Prompt 4", type="filepath")
|
|
|
|
|
|
|
|
|
147 |
|
148 |
generate_button = gr.Button("Generate Image")
|
149 |
+
|
150 |
+
output_image = gr.Image(label="Generated Image", visible=False)
|
151 |
+
status = gr.Textbox(label="Status", value="Awaiting input...", interactive=False)
|
152 |
+
|
153 |
+
job_id = gr.State()
|
154 |
|
155 |
def update_image(job_id):
|
156 |
if job_id:
|
|
|
158 |
return image, status_text
|
159 |
return None, "No job ID found."
|
160 |
|
161 |
+
generate_button.click(image_prompt, inputs=[prompt, image1, image2, image3, image4], outputs=[job_id])
|
162 |
demo.load(update_image, inputs=job_id, outputs=[output_image, status], every=2)
|
163 |
|
164 |
demo.launch()
|