Spaces:
Running
Running
Update app/main.py
#1
by Despressoo - opened
- app/main.py +35 -7
app/main.py
CHANGED
|
@@ -113,14 +113,42 @@ async def classify_file(request: FileRequest):
|
|
| 113 |
async def review_batch_code(request: BatchReviewRequest):
|
| 114 |
try:
|
| 115 |
loop = asyncio.get_event_loop()
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
-
if
|
| 123 |
-
raise
|
| 124 |
traceback.print_exc()
|
| 125 |
raise HTTPException(status_code=500, detail=str(e))
|
| 126 |
|
|
|
|
| 113 |
async def review_batch_code(request: BatchReviewRequest):
|
| 114 |
try:
|
| 115 |
loop = asyncio.get_event_loop()
|
| 116 |
+
|
| 117 |
+
# Helper function to process one file at a time
|
| 118 |
+
def process_single_file(file_req):
|
| 119 |
+
# We wrap it in a list so your existing service method still accepts it,
|
| 120 |
+
# but it only processes 1 file per thread.
|
| 121 |
+
return reviewer.service.review_batch_code([file_req])
|
| 122 |
+
|
| 123 |
+
# Spin up a ThreadPoolExecutor with up to 15 concurrent workers
|
| 124 |
+
with ThreadPoolExecutor(max_workers=15) as executor:
|
| 125 |
+
# Create a concurrent task for every file in the request
|
| 126 |
+
tasks = [
|
| 127 |
+
loop.run_in_executor(executor, process_single_file, f)
|
| 128 |
+
for f in request.files
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
# asyncio.gather fires them all off at the exact same time
|
| 132 |
+
raw_reviews = await asyncio.gather(*tasks, return_exceptions=True)
|
| 133 |
+
|
| 134 |
+
# Clean up the results and handle any individual file failures gracefully
|
| 135 |
+
valid_reviews = []
|
| 136 |
+
for i, result in enumerate(raw_reviews):
|
| 137 |
+
if isinstance(result, Exception):
|
| 138 |
+
# If we hit a rate limit, bubble it up immediately
|
| 139 |
+
if "429" in str(result):
|
| 140 |
+
raise HTTPException(status_code=429, detail="AI Quota Exceeded")
|
| 141 |
+
|
| 142 |
+
# Otherwise, log the specific file error but don't crash the whole batch
|
| 143 |
+
logger.error(f"Failed to analyze {request.files[i].fileName}: {result}")
|
| 144 |
+
else:
|
| 145 |
+
valid_reviews.append(result)
|
| 146 |
+
|
| 147 |
+
return {"results": valid_reviews}
|
| 148 |
+
|
| 149 |
except Exception as e:
|
| 150 |
+
if isinstance(e, HTTPException):
|
| 151 |
+
raise e
|
| 152 |
traceback.print_exc()
|
| 153 |
raise HTTPException(status_code=500, detail=str(e))
|
| 154 |
|