File size: 4,846 Bytes
e24d656
 
17a8bc3
 
bc0f945
e24d656
17a8bc3
 
 
e24d656
17a8bc3
e24d656
 
 
 
 
 
 
 
 
17a8bc3
e24d656
 
 
 
 
 
 
17a8bc3
e24d656
17a8bc3
e24d656
 
 
 
 
17a8bc3
e24d656
 
 
 
bc0f945
17a8bc3
e24d656
 
 
 
 
 
 
 
17a8bc3
e24d656
17a8bc3
 
 
e24d656
17a8bc3
 
 
e24d656
 
 
 
 
 
 
 
 
 
17a8bc3
e24d656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc0f945
e24d656
 
 
 
 
17a8bc3
 
bc0f945
 
 
 
 
 
 
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
# app.py
import gradio as gr
import os
import httpx
from huggingface_hub import list_models
from datasets import load_dataset

HF_TOKEN = os.getenv("HF_TOKEN")

def search_models(query="legal", language="pt", limit=5):
    try:
        results = list(
            list_models(
                search=query,
                language=language or None,
                sort="downloads",
                direction=-1,
                limit=int(limit),
                token=HF_TOKEN,
            )
        )
        output = ""
        for m in results[:int(limit)]:
            output += f"**{m.modelId}**\n"
            output += f"- Task: {m.pipeline_tag}\n"
            output += f"- Downloads: {m.downloads:,}\n"
            output += f"- URL: https://huggingface.co/{m.modelId}\n\n"
        return output if output else "Nenhum modelo encontrado."
    except Exception as e:
        return f"Erro: {str(e)}"

def analyze_text(text, task="summarization"):
    try:
        models = {
            "summarization": "facebook/bart-large-cnn",
            "text-classification": "nlpaueb/legal-bert-base-uncased",
        }
        model_id = models.get(task, "facebook/bart-large-cnn")
        url = f"https://api-inference.huggingface.co/models/{model_id}"
        headers = {"Content-Type": "application/json"}
        if HF_TOKEN:
            headers["Authorization"] = f"Bearer {HF_TOKEN}"
        payload = {"inputs": text[:512]}
        with httpx.Client(timeout=60.0) as client:
            resp = client.post(url, headers=headers, json=payload)
        if resp.status_code == 200:
            return str(resp.json())
        else:
            return f"Erro HTTP {resp.status_code}: {resp.text[:200]}"
    except Exception as e:
        return f"Erro: {str(e)}"

def explore_dataset(dataset_id, n_samples=3):
    try:
        ds = load_dataset(
            dataset_id,
            split=f"train[:{n_samples}]",
            token=HF_TOKEN,
            trust_remote_code=False,
        )
        output = f"**Dataset:** {dataset_id}\n\n"
        output += f"**Colunas:** {', '.join(ds.column_names)}\n\n"
        for i, sample in enumerate(ds.to_list()[:int(n_samples)]):
            output += f"--- Amostra {i+1} ---\n"
            for key, val in sample.items():
                if isinstance(val, str) and len(val) > 300:
                    val = val[:300] + "..."
                output += f"{key}: {val}\n"
            output += "\n"
        return output
    except Exception as e:
        return f"Erro: {str(e)}"

with gr.Blocks(title="LEX - Assistente Jurídico", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# ⚖️ LEX - Assistente Jurídico MCP")
    gr.Markdown("Powered by Hugging Face Hub + FastMCP")
    
    with gr.Tab("🔍 Buscar Modelos"):
        with gr.Row():
            with gr.Column():
                model_query = gr.Textbox(label="Query", value="legal")
                model_lang = gr.Textbox(label="Idioma", value="pt")
                model_limit = gr.Slider(1, 20, value=5, step=1, label="Limite")
                model_btn = gr.Button("Buscar", variant="primary")
            with gr.Column():
                model_output = gr.Textbox(label="Resultados", lines=15, interactive=False)
        model_btn.click(fn=search_models, inputs=[model_query, model_lang, model_limit], outputs=model_output)
    
    with gr.Tab("📝 Analisar Texto"):
        with gr.Row():
            with gr.Column():
                text_input = gr.Textbox(label="Texto Jurídico", lines=5)
                task_type = gr.Dropdown(
                    choices=["summarization", "text-classification"],
                    value="summarization",
                    label="Tarefa"
                )
                analyze_btn = gr.Button("Analisar", variant="primary")
            with gr.Column():
                analyze_output = gr.Textbox(label="Resultado", lines=10, interactive=False)
        analyze_btn.click(fn=analyze_text, inputs=[text_input, task_type], outputs=analyze_output)
    
    with gr.Tab("📊 Explorar Dataset"):
        with gr.Row():
            with gr.Column():
                dataset_input = gr.Textbox(label="Dataset ID", value="joelniklaus/brazilian_court_decisions")
                sample_count = gr.Slider(1, 10, value=3, step=1, label="Amostras")
                dataset_btn = gr.Button("Explorar", variant="primary")
            with gr.Column():
                dataset_output = gr.Textbox(label="Dataset Info", lines=15, interactive=False)
        dataset_btn.click(fn=explore_dataset, inputs=[dataset_input, sample_count], outputs=dataset_output)

if __name__ == "__main__":
    # ✅ CORREÇÃO: Configuração correta para HF Spaces
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(os.getenv("PORT", 7860)),
        share=False,
        show_error=True
    )