File size: 11,919 Bytes
79ffe8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
lex-mcp — Assistente Jurídico via MCP + Hugging Face
Expõe ferramentas especializadas em direito para qualquer LLM host compatível com MCP.
"""
from __future__ import annotations  # ← CORREÇÃO: adicionar __
import os
import re
from typing import Any
import httpx
from fastmcp import FastMCP
from huggingface_hub import HfApi, list_models  # ← CORREÇÃO: remover ModelFilter
from huggingface_hub.utils import RepositoryNotFoundError
from datasets import load_dataset, get_dataset_config_names, get_dataset_split_names

# ── Bootstrap ─────────────────────────────────────────────────────────────────
mcp = FastMCP(
    name="lex-mcp",
    instructions="""
Você é LEX, um assistente jurídico especializado alimentado por modelos e datasets
do Hugging Face Hub. Você possui quatro ferramentas:
• search_legal_models   — encontra modelos de NLP treinados em domínio jurídico
• explore_legal_dataset — inspeciona datasets jurídicos (jurisprudência, leis, contratos)
• analyze_legal_text    — roda inferência NLP em texto jurídico (classificação, NER, resumo)
• find_jurisprudence    — busca decisões e ementas em datasets de jurisprudência

IMPORTANTE: Sempre use as ferramentas para buscar dados atuais do Hub.
Nunca invente modelos ou citações. Indique limitações quando relevante.
Responda em português quando o usuário escrever em português.
""",
)

HF_TOKEN = os.getenv("HF_TOKEN")
api = HfApi(token=HF_TOKEN)

# Modelos jurídicos de referência no HF Hub (curados)
LEGAL_MODEL_HINTS = [
    "legal", "juridico", "jurídico", "law", "legislation",
    "bert-legal", "legalbert", "law-bert", "contracts", "court",
    "nlp-laval", "legal-xlm", "legalbench", "saul", "brazilianLegal",
]

LEGAL_DATASET_HINTS = [
    "legal", "law", "court", "jurisprudence", "legislation",
    "contracts", "case-law", "oab", "stf", "stj", "tjsp",
]

# ── Tool 1 — search_legal_models ──────────────────────────────────────────────
@mcp.tool(
    description=(
        "Busca modelos de NLP especializados em domínio jurídico no Hugging Face Hub.  "
        "Filtre por língua (ex: 'pt' para português), tarefa (ex: 'text-classification',  "
        "'token-classification', 'summarization') e palavras-chave.  "
        "Retorna os modelos mais baixados com metadados completos. "
    )
)
def search_legal_models(
    query: str = "legal",
    language: str = "pt",
    task: str = "",
    limit: int = 8,
) -> list[dict[str, Any]]:
    """Retorna modelos jurídicos ordenados por downloads."""
    # Enriquecer query com termos jurídicos se necessário
    legal_query = query if any(h in query.lower() for h in LEGAL_MODEL_HINTS) else f"legal {query}"

    # ← CORREÇÃO: Usar parâmetros diretos ao invés de ModelFilter
    results = list(
        list_models(
            search=legal_query,
            language=language or None,
            pipeline_tag=task or None,  # ← CORREÇÃO: pipeline_tag ao invés de task
            sort="downloads",
            direction=-1,
            limit=limit,
            token=HF_TOKEN,
            cardData=True,
        )
    )

    return [
        {
            "id": m.modelId,
            "task": m.pipeline_tag,
            "downloads": m.downloads,
            "likes": m.likes,
            "last_modified": str(m.lastModified)[:10],
            "tags": [t for t in (m.tags or []) if len(t) < 40][:8],
            "language": getattr(m, "language", None),
            "hf_url": f"https://huggingface.co/{m.modelId}",
        }
        for m in results
    ]

# ── Tool 2 — explore_legal_dataset ───────────────────────────────────────────
@mcp.tool(
    description=(
        "Inspeciona um dataset jurídico no Hugging Face Hub.  "
        "Retorna configs disponíveis, splits, schema de colunas e exemplos de registros.  "
        "Ideal para entender datasets de jurisprudência, legislação e contratos.  "
        "Use dataset_id como 'joelniklaus/MultiLegalPile', 'lexlms/lex_glue', etc. "
    )
)
def explore_legal_dataset(
    dataset_id: str,
    config: str = "default",
    split: str = "train",
    n_samples: int = 3,
) -> dict[str, Any]:
    """Retorna schema + amostras de um dataset jurídico."""
    try:
        configs = get_dataset_config_names(dataset_id, token=HF_TOKEN)
    except Exception:
        configs = [config]

    resolved_config = config if config in configs else (configs[0] if configs else None)

    try:
        splits = get_dataset_split_names(dataset_id, config_name=resolved_config, token=HF_TOKEN)
    except Exception:
        splits = [split]

    # ← CORREÇÃO: remover espaço em "spli t"
    resolved_split = split if split in splits else (splits[0] if splits else "train")

    try:
        ds = load_dataset(
            dataset_id,
            name=resolved_config,
            split=f"{resolved_split}[:{n_samples}]",
            token=HF_TOKEN,
            trust_remote_code=False,
        )
        features = {k: str(v) for k, v in ds.features.items()}
        samples = ds.to_list()
        # Truncar textos longos para não explodir o contexto
        for sample in samples:
            for key, val in sample.items():
                if isinstance(val, str) and len(val) > 600:
                    sample[key] = val[:600] + "…"
    except Exception as e:
        features = {}
        samples = []
        return {
            "dataset_id": dataset_id,
            "error": str(e),
            "configs_available": configs,
            "splits_available": splits,
        }

    return {
        "dataset_id": dataset_id,
        "hf_url": f"https://huggingface.co/datasets/{dataset_id}",
        "configs_available": configs,
        "splits_available": splits,
        "resolved": {"config": resolved_config, "split": resolved_split},
        "total_features": len(features),
        "features": features,
        "samples": samples,
    }

# ── Tool 3 — analyze_legal_text ──────────────────────────────────────────────
@mcp.tool(
    description=(
        "Roda inferência NLP em texto jurídico usando a Hugging Face Inference API.  "
        "Tarefas suportadas: 'summarization' (resumo de decisões),  "
        "'text-classification' (classificação de matéria/área do direito),  "
        "'token-classification' (NER: partes, datas, valores),  "
        "'question-answering' (responde perguntas sobre o texto).  "
        "Se model_id não for fornecido, usa modelos jurídicos recomendados. "
    )
)
def analyze_legal_text(
    text: str,
    task: str = "summarization",
    model_id: str = "",
    context: str = "",
) -> dict[str, Any]:
    """Executa análise NLP jurídica via Inference API."""
    # Modelos padrão por tarefa (jurídicos ou multilíngues de qualidade)
    DEFAULT_MODELS: dict[str, str] = {
        "summarization": "facebook/bart-large-cnn",
        "text-classification": "nlpaueb/legal-bert-base-uncased",
        "token-classification": "nlpaueb/legal-bert-base-uncased",
        "question-answering": "deepset/roberta-base-squad2",
        "fill-mask": "nlpaueb/legal-bert-base-uncased",
    }

    resolved_model = model_id or DEFAULT_MODELS.get(task, "facebook/bart-large-cnn")

    url = f"https://api-inference.huggingface.co/models/{resolved_model}"
    headers = {"Content-Type": "application/json"}
    if HF_TOKEN:
        headers["Authorization"] = f"Bearer {HF_TOKEN}"
    if task:
        headers["X-Task"] = task

    # Montar payload conforme a tarefa
    if task == "question-answering" and context:
        payload: dict[str, Any] = {"inputs": {"question": text, "context": context}}
    elif task == "summarization":
        # Truncar para evitar erros de tamanho máximo
        payload = {
            "inputs": text[:1024],
            "parameters": {"max_length": 200, "min_length": 40, "do_sample": False},
        }
    else:
        payload = {"inputs": text[:512]}

    with httpx.Client(timeout=60.0) as client:
        resp = client.post(url, headers=headers, json=payload)

    if resp.status_code == 503:
        return {
            "status": "model_loading",
            "model_id": resolved_model,
            "message": "Modelo está carregando. Tente novamente em 20-30 segundos.",
        }

    if resp.status_code != 200:
        return {
            "error": f"HTTP {resp.status_code}",
            "model_id": resolved_model,
            "detail": resp.text[:400],
        }

    try:
        result = resp.json()
    except Exception:
        result = resp.text

    return {
        "model_id": resolved_model,
        "task": task,
        "hf_url": f"https://huggingface.co/{resolved_model}",
        "result": result,
    }

# ── Tool 4 — find_jurisprudence ───────────────────────────────────────────────
@mcp.tool(
    description=(
        "Busca decisões judiciais e ementas em datasets de jurisprudência disponíveis  "
        "no Hugging Face Hub. Pesquisa por palavras-chave no texto das decisões.  "
        "Retorna ementas, tribunal, data e número do processo quando disponíveis.  "
        "Datasets suportados: 'joelniklaus/brazilian_court_decisions',  "
        "'lagepaul/jurisprudencia-brasil' e outros datasets jurídicos brasileiros. "
    )
)
def find_jurisprudence(
    keywords: str,
    dataset_id: str = "joelniklaus/brazilian_court_decisions",
    max_results: int = 5,
    split: str = "train",
) -> dict[str, Any]:
    """Busca decisões judiciais por palavras-chave."""
    try:
        configs = get_dataset_config_names(dataset_id, token=HF_TOKEN)
        resolved_config = configs[0] if configs else None
    except Exception:
        resolved_config = None

    try:
        # Carregar slice generoso para fazer busca textual
        ds = load_dataset(
            dataset_id,
            name=resolved_config,
            split=f"{split}[:500]",
            token=HF_TOKEN,
            trust_remote_code=False,
        )
    except Exception as e:
        return {"error": str(e), "dataset_id": dataset_id}

    # Identificar coluna de texto principal
    text_cols = [
        col for col in ds.column_names
        if any(kw in col.lower() for kw in ["text", "ementa", "decision", "body", "content", "acordao"])
    ]
    text_col = text_cols[0] if text_cols else ds.column_names[0]

    # Busca por keywords (case-insensitive)
    kw_pattern = re.compile("|".join(re.escape(k.strip()) for k in keywords.split(",")), re.IGNORECASE)

    matches = []
    for row in ds:
        haystack = str(row.get(text_col, ""))
        if kw_pattern.search(haystack):
            snippet = haystack[:500] + ("…" if len(haystack) > 500 else "")
            matches.append({
                "snippet": snippet,
                "columns": {k: str(v)[:200] for k, v in row.items() if k != text_col},
            })
        if len(matches) >= max_results:
            break

    return {
        "dataset_id": dataset_id,
        "hf_url": f"https://huggingface.co/datasets/{dataset_id}",
        "keywords_searched": keywords,
        "text_column_used": text_col,
        "total_matches": len(matches),
        "results": matches,
    }

# ── Entry point ───────────────────────────────────────────────────────────────
# ← CORREÇÃO: adicionar __
if __name__ == "__main__":
    mcp.run()