rayochoajr commited on
Commit
79b7222
·
verified ·
1 Parent(s): e6f2d3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -70
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
- try:
17
- # Reading image files and encoding them
18
- image_sources = [open(image, "rb").read() for image in [image1, image2, image3, image4]]
19
- encoded_images = [base64.b64encode(img).decode('utf-8') for img in image_sources]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Prepare the payload with all the image prompts
22
- params = {
23
- "prompt": prompt,
24
- "image_prompts": [
25
- {
26
- "cn_img": encoded_img,
27
- "cn_stop": 1,
28
- "cn_weight": 1,
29
- "cn_type": "ImagePrompt"
30
- } for encoded_img in encoded_images
31
- ],
32
- "async_process": True
33
- }
34
-
35
- # Setup retry strategy for robust request handling
36
- session = requests.Session()
37
- retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
38
- session.mount('http://', HTTPAdapter(max_retries=retries))
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
- # Real-time update to image and status
63
- if job_stage == "RUNNING" and job_step_preview:
64
- image = Image.open(BytesIO(base64.b64decode(job_step_preview)))
65
- yield image, f"Job is running smoothly. Current stage: {job_stage}. Hang tight!"
66
 
67
- elif 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
- image = Image.open(BytesIO(image_response.content))
73
- yield image, "Job completed successfully. Enjoy your masterpiece!"
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
- time.sleep(2) # Pause for 2 seconds before checking again
84
-
85
- else:
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: