Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
"
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
model="neuralmind/bert-base-portuguese-cased"
|
20 |
-
)
|
21 |
-
|
22 |
-
def avaliar_respostas(prompt, respostas):
|
23 |
-
instrucao = f"""
|
24 |
-
Avalie qual resposta é melhor para o prompt '{prompt}':
|
25 |
-
1. {respostas[0]}
|
26 |
-
2. {respostas[1]}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
def chatbot(prompt):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
|
|
51 |
|
52 |
-
# Interface com exemplos em PT-BR
|
53 |
with gr.Blocks(title="Chatbot ABNT") as app:
|
54 |
-
gr.Markdown("## 🇧🇷 Chatbot Acadêmico (Português
|
55 |
with gr.Row():
|
56 |
-
entrada = gr.Textbox(label="
|
57 |
-
saida = gr.Textbox(label="Resposta
|
58 |
btn = gr.Button("Enviar")
|
59 |
btn.click(chatbot, inputs=entrada, outputs=saida)
|
60 |
gr.Examples(
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Modelos em Português Brasileiro validados (públicos)
|
5 |
+
MODELOS = {
|
6 |
+
"curto": "pierreguillou/bert-base-cased-squad-v1.1-portuguese",
|
7 |
+
"longo": "dominguesm/alpaca-ptbr-7b",
|
8 |
+
"arbitro": "neuralmind/bert-base-portuguese-cased"
|
9 |
+
}
|
10 |
|
11 |
+
# Configuração segura com fallback
|
12 |
+
try:
|
13 |
+
modelo_rapido = pipeline(
|
14 |
+
"text-generation",
|
15 |
+
model=MODELOS["curto"],
|
16 |
+
max_length=50,
|
17 |
+
do_sample=False
|
18 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
modelo_detalhado = pipeline(
|
21 |
+
"text-generation",
|
22 |
+
model=MODELOS["longo"],
|
23 |
+
max_length=100,
|
24 |
+
temperature=0.7
|
25 |
+
)
|
26 |
|
27 |
+
arbitro = pipeline(
|
28 |
+
"text-classification",
|
29 |
+
model=MODELOS["arbitro"]
|
30 |
+
)
|
31 |
+
except Exception as e:
|
32 |
+
raise gr.Error(f"Erro ao carregar modelos: {str(e)}")
|
33 |
|
34 |
def chatbot(prompt):
|
35 |
+
try:
|
36 |
+
# Força PT-BR e evita respostas em inglês
|
37 |
+
prompt_pt = f"Responda em português brasileiro de forma acadêmica: {prompt}"
|
38 |
+
|
39 |
+
resposta_curta = modelo_rapido(prompt_pt)[0]['generated_text']
|
40 |
+
resposta_longa = modelo_detalhado(prompt_pt)[0]['generated_text']
|
41 |
+
|
42 |
+
# Filtro de qualidade básico
|
43 |
+
resposta_curta = resposta_curta.split(".")[0] + "."
|
44 |
+
resposta_longa = resposta_longa.split("\n")[0]
|
45 |
+
|
46 |
+
return f"🔵 Resposta Concisa:\n{resposta_curta}\n\n🟢 Resposta Detalhada:\n{resposta_longa}"
|
47 |
|
48 |
+
except Exception as e:
|
49 |
+
return f"⚠ Erro: {str(e)}"
|
50 |
|
|
|
51 |
with gr.Blocks(title="Chatbot ABNT") as app:
|
52 |
+
gr.Markdown("## 🇧🇷 Chatbot Acadêmico (Português)")
|
53 |
with gr.Row():
|
54 |
+
entrada = gr.Textbox(label="Sua pergunta", placeholder="Ex: Qual a fórmula da água?")
|
55 |
+
saida = gr.Textbox(label="Resposta", lines=5)
|
56 |
btn = gr.Button("Enviar")
|
57 |
btn.click(chatbot, inputs=entrada, outputs=saida)
|
58 |
gr.Examples(
|