Spaces:
Sleeping
Sleeping
fix: async execution + 120s timeout to prevent connection errors
Browse files- app.py +11 -2
- core/agent.py +4 -1
app.py
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
|
|
|
| 4 |
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
| 7 |
from fastapi.responses import FileResponse
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
|
|
|
|
|
|
|
| 10 |
from core.extractor import extract_pages
|
| 11 |
from core.agent import extract_articles
|
| 12 |
from core.csv_writer import to_csv
|
|
@@ -42,11 +46,16 @@ async def extract(
|
|
| 42 |
pdf_path = tmp.name
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
if not pages:
|
| 47 |
raise HTTPException(status_code=422, detail="Aucune page dans la plage indiquée.")
|
| 48 |
|
| 49 |
-
articles =
|
|
|
|
|
|
|
| 50 |
if not articles:
|
| 51 |
raise HTTPException(status_code=422, detail="Aucun article détecté.")
|
| 52 |
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import tempfile
|
| 5 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 6 |
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
| 9 |
from fastapi.responses import FileResponse
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
|
| 12 |
+
_executor = ThreadPoolExecutor(max_workers=4)
|
| 13 |
+
|
| 14 |
from core.extractor import extract_pages
|
| 15 |
from core.agent import extract_articles
|
| 16 |
from core.csv_writer import to_csv
|
|
|
|
| 46 |
pdf_path = tmp.name
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
loop = asyncio.get_event_loop()
|
| 50 |
+
pages = await loop.run_in_executor(
|
| 51 |
+
_executor, lambda: extract_pages(pdf_path, page_from, page_to, password)
|
| 52 |
+
)
|
| 53 |
if not pages:
|
| 54 |
raise HTTPException(status_code=422, detail="Aucune page dans la plage indiquée.")
|
| 55 |
|
| 56 |
+
articles = await loop.run_in_executor(
|
| 57 |
+
_executor, lambda: extract_articles(pages, margin_pct, default_unit)
|
| 58 |
+
)
|
| 59 |
if not articles:
|
| 60 |
raise HTTPException(status_code=422, detail="Aucun article détecté.")
|
| 61 |
|
core/agent.py
CHANGED
|
@@ -19,8 +19,11 @@ def extract_articles(
|
|
| 19 |
default_unit: str,
|
| 20 |
) -> list[dict]:
|
| 21 |
"""Call Claude to parse catalogue pages and return a list of article dicts."""
|
| 22 |
-
client = anthropic.Anthropic()
|
| 23 |
combined = "\n\n--- PAGE ---\n\n".join(pages_text)
|
|
|
|
|
|
|
|
|
|
| 24 |
user_msg = f"Unité par défaut : {default_unit}\n\nTexte du catalogue :\n{combined}"
|
| 25 |
|
| 26 |
message = client.messages.create(
|
|
|
|
| 19 |
default_unit: str,
|
| 20 |
) -> list[dict]:
|
| 21 |
"""Call Claude to parse catalogue pages and return a list of article dicts."""
|
| 22 |
+
client = anthropic.Anthropic(timeout=120.0)
|
| 23 |
combined = "\n\n--- PAGE ---\n\n".join(pages_text)
|
| 24 |
+
# Tronque si trop long pour éviter les timeouts (≈ 60 pages max)
|
| 25 |
+
if len(combined) > 80_000:
|
| 26 |
+
combined = combined[:80_000]
|
| 27 |
user_msg = f"Unité par défaut : {default_unit}\n\nTexte du catalogue :\n{combined}"
|
| 28 |
|
| 29 |
message = client.messages.create(
|