Spaces:
Sleeping
Sleeping
fix quick_start_agent
Browse files- quick_deploy_agent.py +2 -13
quick_deploy_agent.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# quickstart_agent.py
|
| 2 |
from __future__ import annotations
|
| 3 |
import json, re, unicodedata, ast
|
| 4 |
from typing import List, Dict, Any, Optional
|
|
@@ -39,7 +38,7 @@ def ean_check_digit_ok(ean: str) -> bool:
|
|
| 39 |
class ValidateEANTool(Tool):
|
| 40 |
name, description = "validate_ean", "Valide un EAN/GTIN (clé GS1)."
|
| 41 |
inputs = {"ean": {"type": "string", "description": "Code EAN/GTIN (8/12/13/14 chiffres)."}}
|
| 42 |
-
output_type = "dict"
|
| 43 |
|
| 44 |
def forward(self, ean: str):
|
| 45 |
import re
|
|
@@ -52,8 +51,6 @@ class ValidateEANTool(Tool):
|
|
| 52 |
check = (10 - (total % 10)) % 10
|
| 53 |
return {"valid": check == int(digits[-1]), "normalized": digits}
|
| 54 |
|
| 55 |
-
# ---- OFFtoCOICOP : accepte off_payload (JSON brut) OU champs séparés ----
|
| 56 |
-
|
| 57 |
# ---- OFFByEAN : robuste + sortie normalisée + step3_inputs ----
|
| 58 |
class OFFByEAN(Tool):
|
| 59 |
name = "openfoodfacts_product_by_ean"
|
|
@@ -139,8 +136,6 @@ class OFFByEAN(Tool):
|
|
| 139 |
|
| 140 |
return {"ok": False, "status": 0, "code": code, "error": last_err or "not found"}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
# ---- RegexCOICOP : normalisation locale + regex précompilées ----
|
| 145 |
class RegexCOICOP(Tool):
|
| 146 |
name, description = "coicop_regex_rules", "Règles regex → candidats COICOP."
|
|
@@ -172,7 +167,6 @@ class RegexCOICOP(Tool):
|
|
| 172 |
if not c and re.search(r"\bFROMAGE\b", s): c.append({"code":"01.1.4.5","why":"générique fromage/laits caillés","score":0.6})
|
| 173 |
return {"candidates": c}
|
| 174 |
|
| 175 |
-
|
| 176 |
# ---- OFFtoCOICOP : normalisation locale + regex règles ----
|
| 177 |
class OFFtoCOICOP(Tool):
|
| 178 |
name, description = "map_off_to_coicop", "Mappe catégories OFF vers COICOP (off_payload ou champs séparés)."
|
|
@@ -230,7 +224,6 @@ class OFFtoCOICOP(Tool):
|
|
| 230 |
|
| 231 |
return {"candidates": c}
|
| 232 |
|
| 233 |
-
|
| 234 |
# ---- SemSim : COICOP embarqué + import lazy du modèle ----
|
| 235 |
class SemSim(Tool):
|
| 236 |
name, description = "coicop_semantic_similarity", "Embeddings → top-k COICOP."
|
|
@@ -275,7 +268,6 @@ class SemSim(Tool):
|
|
| 275 |
)
|
| 276 |
return {"candidates": ranked[:max(1,int(topk))]}
|
| 277 |
|
| 278 |
-
|
| 279 |
# ---- Resolve : import local json ----
|
| 280 |
class Resolve(Tool):
|
| 281 |
name, description = "resolve_coicop_candidates", "Fusionne candidats → choix final + alternatives + explication."
|
|
@@ -284,7 +276,6 @@ class Resolve(Tool):
|
|
| 284 |
output_type = "dict"
|
| 285 |
|
| 286 |
def forward(self, json_lists, topn: int = 3):
|
| 287 |
-
# accepter listes de strings JSON OU de dicts
|
| 288 |
import json
|
| 289 |
from typing import Dict, Any
|
| 290 |
bucket: Dict[str, Dict[str, Any]] = {}
|
|
@@ -311,8 +302,6 @@ class Resolve(Tool):
|
|
| 311 |
exp = f"Choix {final['code']} (score {final['score_final']:.2f}) – votes={final['votes']} – raisons: {', '.join(sorted(set(final['evidences'])))}"
|
| 312 |
return {"final": final, "alternatives": alts, "explanation": exp}
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
def build_agent(model_id: str | None = None) -> CodeAgent:
|
| 317 |
model_id = model_id or "Qwen/Qwen2.5-Coder-7B-Instruct" # léger pour tester
|
| 318 |
agent = CodeAgent(
|
|
@@ -349,4 +338,4 @@ if __name__ == "__main__":
|
|
| 349 |
Attend un JSON final.
|
| 350 |
"""
|
| 351 |
out = agent.run(task)
|
| 352 |
-
print(parse_result(out))
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
import json, re, unicodedata, ast
|
| 3 |
from typing import List, Dict, Any, Optional
|
|
|
|
| 38 |
class ValidateEANTool(Tool):
|
| 39 |
name, description = "validate_ean", "Valide un EAN/GTIN (clé GS1)."
|
| 40 |
inputs = {"ean": {"type": "string", "description": "Code EAN/GTIN (8/12/13/14 chiffres)."}}
|
| 41 |
+
output_type = "dict"
|
| 42 |
|
| 43 |
def forward(self, ean: str):
|
| 44 |
import re
|
|
|
|
| 51 |
check = (10 - (total % 10)) % 10
|
| 52 |
return {"valid": check == int(digits[-1]), "normalized": digits}
|
| 53 |
|
|
|
|
|
|
|
| 54 |
# ---- OFFByEAN : robuste + sortie normalisée + step3_inputs ----
|
| 55 |
class OFFByEAN(Tool):
|
| 56 |
name = "openfoodfacts_product_by_ean"
|
|
|
|
| 136 |
|
| 137 |
return {"ok": False, "status": 0, "code": code, "error": last_err or "not found"}
|
| 138 |
|
|
|
|
|
|
|
| 139 |
# ---- RegexCOICOP : normalisation locale + regex précompilées ----
|
| 140 |
class RegexCOICOP(Tool):
|
| 141 |
name, description = "coicop_regex_rules", "Règles regex → candidats COICOP."
|
|
|
|
| 167 |
if not c and re.search(r"\bFROMAGE\b", s): c.append({"code":"01.1.4.5","why":"générique fromage/laits caillés","score":0.6})
|
| 168 |
return {"candidates": c}
|
| 169 |
|
|
|
|
| 170 |
# ---- OFFtoCOICOP : normalisation locale + regex règles ----
|
| 171 |
class OFFtoCOICOP(Tool):
|
| 172 |
name, description = "map_off_to_coicop", "Mappe catégories OFF vers COICOP (off_payload ou champs séparés)."
|
|
|
|
| 224 |
|
| 225 |
return {"candidates": c}
|
| 226 |
|
|
|
|
| 227 |
# ---- SemSim : COICOP embarqué + import lazy du modèle ----
|
| 228 |
class SemSim(Tool):
|
| 229 |
name, description = "coicop_semantic_similarity", "Embeddings → top-k COICOP."
|
|
|
|
| 268 |
)
|
| 269 |
return {"candidates": ranked[:max(1,int(topk))]}
|
| 270 |
|
|
|
|
| 271 |
# ---- Resolve : import local json ----
|
| 272 |
class Resolve(Tool):
|
| 273 |
name, description = "resolve_coicop_candidates", "Fusionne candidats → choix final + alternatives + explication."
|
|
|
|
| 276 |
output_type = "dict"
|
| 277 |
|
| 278 |
def forward(self, json_lists, topn: int = 3):
|
|
|
|
| 279 |
import json
|
| 280 |
from typing import Dict, Any
|
| 281 |
bucket: Dict[str, Dict[str, Any]] = {}
|
|
|
|
| 302 |
exp = f"Choix {final['code']} (score {final['score_final']:.2f}) – votes={final['votes']} – raisons: {', '.join(sorted(set(final['evidences'])))}"
|
| 303 |
return {"final": final, "alternatives": alts, "explanation": exp}
|
| 304 |
|
|
|
|
|
|
|
| 305 |
def build_agent(model_id: str | None = None) -> CodeAgent:
|
| 306 |
model_id = model_id or "Qwen/Qwen2.5-Coder-7B-Instruct" # léger pour tester
|
| 307 |
agent = CodeAgent(
|
|
|
|
| 338 |
Attend un JSON final.
|
| 339 |
"""
|
| 340 |
out = agent.run(task)
|
| 341 |
+
print(parse_result(out))
|