Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForQuestionAnswering
|
4 |
+
|
5 |
+
def carregar_modelos():
|
6 |
+
# Caminhos no Hugging Face Hub
|
7 |
+
caminho_gpt = "mateuscasare/GPTNeo-Vade-Mecum-2023"
|
8 |
+
caminho_bert = "mateuscasare/BERT-Vade-Mecum-2023"
|
9 |
+
caminho_bert_juridico = "mateuscasare/modelo_juridico_pt"
|
10 |
+
|
11 |
+
dispositivo = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
|
13 |
+
# Carregar modelos do Hugging Face Hub
|
14 |
+
tokenizer_gpt = AutoTokenizer.from_pretrained(caminho_gpt)
|
15 |
+
model_gpt = AutoModelForCausalLM.from_pretrained(caminho_gpt).to(dispositivo)
|
16 |
+
|
17 |
+
tokenizer_bert = AutoTokenizer.from_pretrained(caminho_bert)
|
18 |
+
model_bert = AutoModelForQuestionAnswering.from_pretrained(caminho_bert).to(dispositivo)
|
19 |
+
|
20 |
+
tokenizer_bert_juridico = AutoTokenizer.from_pretrained(caminho_bert_juridico)
|
21 |
+
model_bert_juridico = AutoModelForQuestionAnswering.from_pretrained(caminho_bert_juridico).to(dispositivo)
|
22 |
+
|
23 |
+
return tokenizer_gpt, model_gpt, tokenizer_bert, model_bert, tokenizer_bert_juridico, model_bert_juridico, dispositivo
|
24 |
+
|
25 |
+
def main():
|
26 |
+
st.set_page_config(page_title="Assistente Jurídico", layout="wide")
|
27 |
+
st.title("⚖️ Assistente Jurídico")
|
28 |
+
|
29 |
+
# Carregar modelos
|
30 |
+
tokenizer_gpt, model_gpt, tokenizer_bert, model_bert, tokenizer_bert_juridico, model_bert_juridico, dispositivo = carregar_modelos()
|
31 |
+
|
32 |
+
# Interface principal
|
33 |
+
tab1, tab2, tab3 = st.tabs(["Geração de Texto", "Perguntas e Respostas", "Perguntas Jurídicas Específicas"])
|
34 |
+
|
35 |
+
with tab1:
|
36 |
+
st.subheader("📝 Gerador de Textos Jurídicos")
|
37 |
+
prompt = st.text_area("Descreva sua necessidade:", height=150, placeholder="Ex: Preciso de uma petição de habeas corpus...")
|
38 |
+
|
39 |
+
col1, col2 = st.columns(2)
|
40 |
+
with col1:
|
41 |
+
max_length = st.slider("Tamanho da resposta", 100, 2000, 500)
|
42 |
+
temperatura = st.slider("Criatividade", 0.0, 1.0, 0.7)
|
43 |
+
|
44 |
+
if st.button("Gerar", type="primary"):
|
45 |
+
with st.spinner("Gerando texto..."):
|
46 |
+
inputs = tokenizer_gpt(prompt, return_tensors="pt").to(dispositivo)
|
47 |
+
outputs = model_gpt.generate(**inputs, max_length=max_length, temperature=temperatura, do_sample=True)
|
48 |
+
texto_gerado = tokenizer_gpt.decode(outputs[0], skip_special_tokens=True)
|
49 |
+
st.text_area("Texto gerado:", texto_gerado, height=400)
|
50 |
+
|
51 |
+
with tab2:
|
52 |
+
st.subheader("❓ Consulta Jurídica (Modelo Genérico)")
|
53 |
+
contexto = st.text_area("Contexto:", height=150, placeholder="Insira o contexto jurídico aqui...")
|
54 |
+
pergunta = st.text_input("Sua pergunta:", placeholder="Ex: Qual o prazo para...?")
|
55 |
+
|
56 |
+
if st.button("Responder (Modelo Genérico)", type="primary"):
|
57 |
+
with st.spinner("Analisando..."):
|
58 |
+
inputs = tokenizer_bert(pergunta, contexto, return_tensors="pt").to(dispositivo)
|
59 |
+
outputs = model_bert(**inputs)
|
60 |
+
answer_start = torch.argmax(outputs.start_logits)
|
61 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
62 |
+
answer = tokenizer_bert.decode(inputs["input_ids"][0][answer_start:answer_end])
|
63 |
+
st.success(f"Resposta: {answer}")
|
64 |
+
|
65 |
+
with tab3:
|
66 |
+
st.subheader("⚖️ Consulta Jurídica Específica")
|
67 |
+
contexto_juridico = st.text_area("Contexto Jurídico:", height=150, placeholder="Insira o contexto jurídico específico aqui...")
|
68 |
+
pergunta_juridica = st.text_input("Sua pergunta jurídica:", placeholder="Ex: Qual o prazo para recorrer?")
|
69 |
+
|
70 |
+
if st.button("Responder (Modelo Jurídico)", type="primary"):
|
71 |
+
with st.spinner("Analisando..."):
|
72 |
+
inputs = tokenizer_bert_juridico(pergunta_juridica, contexto_juridico, return_tensors="pt").to(dispositivo)
|
73 |
+
outputs = model_bert_juridico(**inputs)
|
74 |
+
answer_start = torch.argmax(outputs.start_logits)
|
75 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
76 |
+
answer = tokenizer_bert_juridico.decode(inputs["input_ids"][0][answer_start:answer_end])
|
77 |
+
st.success(f"Resposta Jurídica: {answer}")
|
78 |
+
|
79 |
+
with st.expander("ℹ️ Sobre os Modelos"):
|
80 |
+
st.markdown("""
|
81 |
+
Este assistente utiliza três modelos de IA:
|
82 |
+
- **GPTNeo Vade-Mecum**: Para geração de textos jurídicos.
|
83 |
+
- **BERT Vade-Mecum**: Para responder perguntas gerais sobre direito.
|
84 |
+
- **Modelo Jurídico PT**: Treinado especificamente com textos jurídicos brasileiros para maior precisão.
|
85 |
+
""")
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|