Spaces:
Sleeping
Sleeping
fix: batch processing (5 pages/appel) + max_tokens 16000 pour 1000+ refs
Browse files- core/agent.py +22 -14
- static/index.html +8 -4
core/agent.py
CHANGED
|
@@ -12,28 +12,36 @@ Chaque objet doit avoir exactement ces quatre champs :
|
|
| 12 |
Ne retourne aucun texte, commentaire ou balise avant ou après le tableau JSON.\
|
| 13 |
"""
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
-
def extract_articles(
|
| 17 |
-
pages_text: list[str],
|
| 18 |
-
margin_pct: float,
|
| 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(
|
| 30 |
model="claude-sonnet-4-6",
|
| 31 |
-
max_tokens=
|
| 32 |
system=_SYSTEM,
|
| 33 |
messages=[{"role": "user", "content": user_msg}],
|
| 34 |
)
|
| 35 |
-
|
| 36 |
raw = message.content[0].text.strip()
|
| 37 |
if raw.startswith("```"):
|
| 38 |
raw = raw.split("\n", 1)[1].rsplit("```", 1)[0]
|
| 39 |
return json.loads(raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
Ne retourne aucun texte, commentaire ou balise avant ou après le tableau JSON.\
|
| 13 |
"""
|
| 14 |
|
| 15 |
+
_BATCH_SIZE = 5 # pages par appel Claude
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def _call_claude(client: anthropic.Anthropic, batch: list[str], default_unit: str) -> list[dict]:
|
| 19 |
+
combined = "\n\n--- PAGE ---\n\n".join(batch)
|
| 20 |
+
user_msg = f"Unité par défaut : {default_unit}\n\nTexte du catalogue :\n{combined}"
|
| 21 |
message = client.messages.create(
|
| 22 |
model="claude-sonnet-4-6",
|
| 23 |
+
max_tokens=16000,
|
| 24 |
system=_SYSTEM,
|
| 25 |
messages=[{"role": "user", "content": user_msg}],
|
| 26 |
)
|
|
|
|
| 27 |
raw = message.content[0].text.strip()
|
| 28 |
if raw.startswith("```"):
|
| 29 |
raw = raw.split("\n", 1)[1].rsplit("```", 1)[0]
|
| 30 |
return json.loads(raw)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def extract_articles(
|
| 34 |
+
pages_text: list[str],
|
| 35 |
+
margin_pct: float,
|
| 36 |
+
default_unit: str,
|
| 37 |
+
) -> list[dict]:
|
| 38 |
+
"""Process pages in batches and return all articles combined."""
|
| 39 |
+
client = anthropic.Anthropic(timeout=180.0)
|
| 40 |
+
all_articles: list[dict] = []
|
| 41 |
+
|
| 42 |
+
batches = [pages_text[i:i + _BATCH_SIZE] for i in range(0, len(pages_text), _BATCH_SIZE)]
|
| 43 |
+
for batch in batches:
|
| 44 |
+
articles = _call_claude(client, batch, default_unit)
|
| 45 |
+
all_articles.extend(articles)
|
| 46 |
+
|
| 47 |
+
return all_articles
|
static/index.html
CHANGED
|
@@ -360,17 +360,21 @@
|
|
| 360 |
}, 1500);
|
| 361 |
|
| 362 |
await new Promise((resolve, reject) => {
|
|
|
|
| 363 |
const poll = setInterval(async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
try {
|
| 365 |
const s = await fetch(`/status/${job_id}`);
|
| 366 |
const data = await s.json();
|
| 367 |
if (data.status === 'done') {
|
| 368 |
-
clearInterval(poll);
|
| 369 |
-
clearInterval(ticker);
|
| 370 |
resolve(data);
|
| 371 |
} else if (data.status === 'error') {
|
| 372 |
-
clearInterval(poll);
|
| 373 |
-
clearInterval(ticker);
|
| 374 |
reject(new Error(data.error));
|
| 375 |
}
|
| 376 |
} catch (e) { /* réseau temporaire, on réessaie */ }
|
|
|
|
| 360 |
}, 1500);
|
| 361 |
|
| 362 |
await new Promise((resolve, reject) => {
|
| 363 |
+
const started = Date.now();
|
| 364 |
const poll = setInterval(async () => {
|
| 365 |
+
if (Date.now() - started > 600_000) {
|
| 366 |
+
clearInterval(poll); clearInterval(ticker);
|
| 367 |
+
reject(new Error('Délai dépassé (10 min). Essaie avec moins de pages.'));
|
| 368 |
+
return;
|
| 369 |
+
}
|
| 370 |
try {
|
| 371 |
const s = await fetch(`/status/${job_id}`);
|
| 372 |
const data = await s.json();
|
| 373 |
if (data.status === 'done') {
|
| 374 |
+
clearInterval(poll); clearInterval(ticker);
|
|
|
|
| 375 |
resolve(data);
|
| 376 |
} else if (data.status === 'error') {
|
| 377 |
+
clearInterval(poll); clearInterval(ticker);
|
|
|
|
| 378 |
reject(new Error(data.error));
|
| 379 |
}
|
| 380 |
} catch (e) { /* réseau temporaire, on réessaie */ }
|