Freak-ppa commited on
Commit
64eb3b9
·
verified ·
1 Parent(s): 380f596

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -41,22 +41,23 @@ def wait_for_image_with_prefix(folder, prefix):
41
 
42
  while True:
43
  files = os.listdir(folder)
44
- image_files = [f for f in files if f.lower().startswith(prefix.lower()) and
45
  f.lower().endswith(('.png', '.jpg', '.jpeg'))]
46
-
47
  if image_files:
48
  # Sort by modification time to get the latest file
49
  image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder, x)), reverse=True)
50
  latest_image = os.path.join(folder, image_files[0])
51
-
52
  if is_file_ready(latest_image):
53
  # Wait a bit more to ensure the file is completely written
54
  time.sleep(1)
55
  return latest_image
56
-
57
  # If no matching file found, wait before checking again
58
  time.sleep(1)
59
 
 
60
  def delete_image_file(file_path):
61
  try:
62
  if os.path.exists(file_path):
@@ -67,6 +68,7 @@ def delete_image_file(file_path):
67
  except Exception as e:
68
  logger.debug(f"error {file_path}: {str(e)}")
69
 
 
70
  def start_queue(prompt_workflow):
71
  p = {"prompt": prompt_workflow}
72
  data = json.dumps(p).encode('utf-8')
@@ -80,16 +82,19 @@ def check_server_ready():
80
  except requests.RequestException:
81
  return False
82
 
83
- queue_reqs=set()
 
 
84
 
85
  @spaces.GPU(duration=240)
86
  def generate_image(prompt, image):
 
 
87
  prompt = json.loads(prompt)
88
-
89
  image = Image.fromarray(image)
90
- image.save(INPUT_DIR+'/input.png', format='PNG')
91
-
92
- prefix_filename = str(random.randint(0, 999999))
93
  queue_reqs.add(prefix_filename)
94
  process = None
95
  try:
@@ -115,10 +120,7 @@ def generate_image(prompt, image):
115
  if latest_image:
116
  logger.debug(f"file is: {latest_image}")
117
  try:
118
- with open(latest_image, 'rb') as f:
119
- photo = f.read()
120
- logger.debug(f"file bytes size: {len(photo)}")
121
- return io.BytesIO(photo)
122
  finally:
123
  delete_image_file(latest_image)
124
  time.sleep(1)
@@ -139,16 +141,14 @@ def generate_image(prompt, image):
139
  process.kill()
140
 
141
 
142
-
143
-
144
  if __name__ == "__main__":
145
  demo = gr.Interface(fn=generate_image, inputs=[
146
  "text",
147
  gr.Image(image_mode='RGBA', type="numpy")
148
  ],
149
- outputs=[
150
- gr.Image(type="numpy", image_mode='RGBA')
151
- ])
152
  demo.launch(debug=True)
153
  logger.debug('demo.launch()')
154
 
 
41
 
42
  while True:
43
  files = os.listdir(folder)
44
+ image_files = [f for f in files if f.lower().startswith(prefix.lower()) and
45
  f.lower().endswith(('.png', '.jpg', '.jpeg'))]
46
+
47
  if image_files:
48
  # Sort by modification time to get the latest file
49
  image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder, x)), reverse=True)
50
  latest_image = os.path.join(folder, image_files[0])
51
+
52
  if is_file_ready(latest_image):
53
  # Wait a bit more to ensure the file is completely written
54
  time.sleep(1)
55
  return latest_image
56
+
57
  # If no matching file found, wait before checking again
58
  time.sleep(1)
59
 
60
+
61
  def delete_image_file(file_path):
62
  try:
63
  if os.path.exists(file_path):
 
68
  except Exception as e:
69
  logger.debug(f"error {file_path}: {str(e)}")
70
 
71
+
72
  def start_queue(prompt_workflow):
73
  p = {"prompt": prompt_workflow}
74
  data = json.dumps(p).encode('utf-8')
 
82
  except requests.RequestException:
83
  return False
84
 
85
+
86
+ queue_reqs = set()
87
+
88
 
89
  @spaces.GPU(duration=240)
90
  def generate_image(prompt, image):
91
+ prefix_filename = str(random.randint(0, 999999))
92
+ prompt = prompt.replace('ComfyUI', prefix_filename)
93
  prompt = json.loads(prompt)
94
+
95
  image = Image.fromarray(image)
96
+ image.save(INPUT_DIR + '/input.png', format='PNG')
97
+
 
98
  queue_reqs.add(prefix_filename)
99
  process = None
100
  try:
 
120
  if latest_image:
121
  logger.debug(f"file is: {latest_image}")
122
  try:
123
+ return Image.open(latest_image)
 
 
 
124
  finally:
125
  delete_image_file(latest_image)
126
  time.sleep(1)
 
141
  process.kill()
142
 
143
 
 
 
144
  if __name__ == "__main__":
145
  demo = gr.Interface(fn=generate_image, inputs=[
146
  "text",
147
  gr.Image(image_mode='RGBA', type="numpy")
148
  ],
149
+ outputs=[
150
+ gr.Image(type="numpy", image_mode='RGBA')
151
+ ])
152
  demo.launch(debug=True)
153
  logger.debug('demo.launch()')
154