Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Limit concurrent Replicate requests to 20 per tool call
Claudeb18302fe3494
diff --git a/backend/agent/tools/runtime.py b/backend/agent/tools/runtime.pyindex ba031c83..c8b5e5c2 100644--- a/backend/agent/tools/runtime.py+++ b/backend/agent/tools/runtime.py@@ -261,9 +261,14 @@ class AgentToolRuntime:summary={"error": "No valid image_urls"},)- tasks = [- remove_background(url, REPLICATE_API_KEY) for url in unique_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)results: List[Dict[str, Any]] = []diff --git a/backend/image_generation/generation.py b/backend/image_generation/generation.pyindex 008ae779..9219088b 100644--- a/backend/image_generation/generation.py+++ b/backend/image_generation/generation.py@@ -7,6 +7,9 @@ from openai import AsyncOpenAIfrom image_generation.replicate import call_replicate+MAX_CONCURRENT_REPLICATE_REQUESTS = 20++async def process_tasks(prompts: List[str],api_key: str,@@ -16,9 +19,16 @@ async def process_tasks(start_time = time.time()if model == "dalle3":tasks = [generate_image_dalle(prompt, api_key, base_url) for prompt in prompts]+ results = await asyncio.gather(*tasks, return_exceptions=True)else:- tasks = [generate_image_replicate(prompt, api_key) for prompt in prompts]- results = await asyncio.gather(*tasks, return_exceptions=True)+ 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)end_time = time.time()generation_time = end_time - start_timeprint(f"Image generation time: {generation_time:.2f} seconds")diff --git a/backend/image_generation/replicate.py b/backend/image_generation/replicate.pyindex 97114001..843eade9 100644--- a/backend/image_generation/replicate.py+++ b/backend/image_generation/replicate.py@@ -10,9 +10,6 @@ REMOVE_BACKGROUND_VERSION = ()POLL_INTERVAL_SECONDS = 0.1MAX_POLLS = 100-MAX_CONCURRENT_REQUESTS = 20--_replicate_semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)def _build_headers(api_token: str) -> dict[str, str]:@@ -60,7 +57,7 @@ async def _run_prediction() -> Any:headers = _build_headers(api_token)- async with _replicate_semaphore, httpx.AsyncClient() as client:+ async with httpx.AsyncClient() as client:try:response = await client.post(endpoint_url, headers=headers, json=payload)response.raise_for_status()