pseudotheos commited on
Commit
33fe4d2
1 Parent(s): 69ce22d

added queue

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -40,7 +40,21 @@ stream_handler.setFormatter(formatter)
40
  logger.addHandler(file_handler)
41
  logger.addHandler(stream_handler)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  app = FastAPI()
 
44
 
45
  BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
46
 
@@ -242,7 +256,8 @@ async def generate_image(
242
  sampler_type: str = Form(...),
243
  image: UploadFile = File(...)
244
  ):
245
- try:
 
246
  # Save the uploaded image to a temporary file
247
  temp_image_path = f"/tmp/{int(time.time())}_{image.filename}"
248
  with open(temp_image_path, "wb") as temp_image:
@@ -268,6 +283,19 @@ async def generate_image(
268
  logger.error("Error occurred during image generation: %s", str(e))
269
  return "Failed to generate image"
270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  async def start_fastapi():
272
  # Get internal IP address
273
  internal_ip = socket.gethostbyname(socket.gethostname())
@@ -280,6 +308,10 @@ async def start_fastapi():
280
 
281
  print(f"Internal URL: http://{internal_ip}:7860")
282
  print(f"Public URL: http://{public_ip}:7860")
 
 
 
 
283
 
284
  # Run FastAPI using hypercorn
285
  config = uvicorn.Config(app="app:app", host="0.0.0.0", port=7860, reload=True)
 
40
  logger.addHandler(file_handler)
41
  logger.addHandler(stream_handler)
42
 
43
+ class ImageGenerationQueue:
44
+ def __init__(self):
45
+ self.queue = asyncio.Queue()
46
+
47
+ async def add_task(self, task):
48
+ await self.queue.put(task)
49
+
50
+ async def process_queue(self):
51
+ while True:
52
+ task = await self.queue.get()
53
+ await task()
54
+ self.queue.task_done()
55
+
56
  app = FastAPI()
57
+ queue_manager = ImageGenerationQueue()
58
 
59
  BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
60
 
 
256
  sampler_type: str = Form(...),
257
  image: UploadFile = File(...)
258
  ):
259
+ async def generate_image_task():
260
+ try:
261
  # Save the uploaded image to a temporary file
262
  temp_image_path = f"/tmp/{int(time.time())}_{image.filename}"
263
  with open(temp_image_path, "wb") as temp_image:
 
283
  logger.error("Error occurred during image generation: %s", str(e))
284
  return "Failed to generate image"
285
 
286
+ pass
287
+ try:
288
+ await queue_manager.add_task(generate_image_task)
289
+ position_in_queue = queue_manager.queue.qsize()
290
+ total_queue_size = queue_manager.queue.qsize() # Implement this function
291
+ return {"position_in_queue": position_in_queue, "total_queue_size": total_queue_size}
292
+
293
+ except Exception as e:
294
+ logger.error("Error occurred during image generation: %s", str(e))
295
+ return "Failed to add task to the queue"
296
+
297
+
298
+
299
  async def start_fastapi():
300
  # Get internal IP address
301
  internal_ip = socket.gethostbyname(socket.gethostname())
 
308
 
309
  print(f"Internal URL: http://{internal_ip}:7860")
310
  print(f"Public URL: http://{public_ip}:7860")
311
+ queue_manager = ImageGenerationQueue()
312
+
313
+ # Start processing the image generation queue
314
+ queue_processing_task = asyncio.create_task(queue_manager.process_queue())
315
 
316
  # Run FastAPI using hypercorn
317
  config = uvicorn.Config(app="app:app", host="0.0.0.0", port=7860, reload=True)