Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Replace semaphores with simple batching of 20 for Replicate calls
Claude962a8f912d90
diff --git a/backend/agent/tools/runtime.py b/backend/agent/tools/runtime.pyindex c8b5e5c2..b1680507 100644--- a/backend/agent/tools/runtime.py+++ b/backend/agent/tools/runtime.py@@ -261,15 +261,12 @@ class AgentToolRuntime:summary={"error": "No valid image_urls"},)- max_concurrent = 20- semaphore = asyncio.Semaphore(max_concurrent)-- async def _limited(url: str) -> str:- async with semaphore:- return await remove_background(url, REPLICATE_API_KEY)-- tasks = [_limited(url) for url in unique_urls]- raw_results = await asyncio.gather(*tasks, return_exceptions=True)+ batch_size = 20+ raw_results: list[str | BaseException] = []+ for i in range(0, len(unique_urls), batch_size):+ batch = unique_urls[i : i + batch_size]+ tasks = [remove_background(url, REPLICATE_API_KEY) for url in batch]+ raw_results.extend(await asyncio.gather(*tasks, return_exceptions=True))results: List[Dict[str, Any]] = []for url, raw in zip(unique_urls, raw_results):diff --git a/backend/image_generation/generation.py b/backend/image_generation/generation.pyindex 9219088b..c412043d 100644--- a/backend/image_generation/generation.py+++ b/backend/image_generation/generation.py@@ -7,7 +7,7 @@ from openai import AsyncOpenAIfrom image_generation.replicate import call_replicate-MAX_CONCURRENT_REPLICATE_REQUESTS = 20+REPLICATE_BATCH_SIZE = 20async def process_tasks(@@ -21,14 +21,11 @@ async def process_tasks(tasks = [generate_image_dalle(prompt, api_key, base_url) for prompt in prompts]results = await asyncio.gather(*tasks, return_exceptions=True)else:- semaphore = asyncio.Semaphore(MAX_CONCURRENT_REPLICATE_REQUESTS)-- async def _limited(prompt: str) -> str:- async with semaphore:- return await generate_image_replicate(prompt, api_key)-- tasks = [_limited(prompt) for prompt in prompts]- results = await asyncio.gather(*tasks, return_exceptions=True)+ results: list[str | BaseException] = []+ for i in range(0, len(prompts), REPLICATE_BATCH_SIZE):+ batch = prompts[i : i + REPLICATE_BATCH_SIZE]+ tasks = [generate_image_replicate(p, api_key) for p in batch]+ results.extend(await asyncio.gather(*tasks, return_exceptions=True))end_time = time.time()generation_time = end_time - start_timeprint(f"Image generation time: {generation_time:.2f} seconds")