Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,100 +3,68 @@ from langdetect import detect, DetectorFactory
|
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
|
6 |
-
#
|
7 |
DetectorFactory.seed = 0
|
8 |
|
9 |
-
# Pipeline
|
10 |
generator = pipeline(
|
11 |
"text2text-generation",
|
12 |
-
model="
|
|
|
13 |
device=-1
|
14 |
)
|
15 |
|
16 |
-
# Palavras-chave de comando em pt/en/fr
|
17 |
COMMANDS = {
|
18 |
-
"resumo": ["resuma", "resumo", "resumir"
|
19 |
-
"reescrever":["reescreva", "reformule", "reformular"
|
20 |
-
"expandir": ["expanda", "expansão", "expandir", "detalhe"
|
21 |
-
"corrigir": ["corrija", "corrigir", "melhore", "revise"
|
22 |
}
|
23 |
|
24 |
-
# Mensagens de instrução por comando e idioma
|
25 |
INSTRUCTIONS = {
|
26 |
-
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
-
},
|
31 |
-
"reescrever": {
|
32 |
-
"pt": "Reescreva este texto com mais clareza e estilo:",
|
33 |
-
"en": "Rewrite this text with more clarity and style:",
|
34 |
-
"fr": "Réécrivez ce texte avec plus de clarté et de style :"
|
35 |
-
},
|
36 |
-
"expandir": {
|
37 |
-
"pt": "Expanda este texto, adicionando detalhes e explicações:",
|
38 |
-
"en": "Expand the following text by adding details and explanations:",
|
39 |
-
"fr": "Développez ce texte en ajoutant des détails et des explications :"
|
40 |
-
},
|
41 |
-
"corrigir": {
|
42 |
-
"pt": "Corrija gramática, ortografia e estilo deste texto:",
|
43 |
-
"en": "Correct the grammar, spelling, and style of this text:",
|
44 |
-
"fr": "Corrigez la grammaire, l'orthographe et le style de ce texte :"
|
45 |
-
},
|
46 |
-
}
|
47 |
-
|
48 |
-
# Prompt de humanização por idioma
|
49 |
-
HUMANIZE = {
|
50 |
-
"pt": "Por favor, torne o texto a seguir mais natural e humano:",
|
51 |
-
"en": "Please make the following text more natural and human-like:",
|
52 |
-
"fr": "Veuillez rendre le texte suivant plus naturel et humain :"
|
53 |
}
|
54 |
|
55 |
def detect_language(text: str) -> str:
|
56 |
try:
|
57 |
-
|
58 |
-
return code if code in HUMANIZE else "pt"
|
59 |
except:
|
60 |
return "pt"
|
61 |
|
62 |
def find_command(text: str) -> str:
|
63 |
-
low = text.lower()
|
64 |
for cmd, kws in COMMANDS.items():
|
65 |
for kw in kws:
|
66 |
-
if kw in
|
67 |
return cmd
|
68 |
-
return "expandir"
|
69 |
|
70 |
def clean_text(text: str) -> str:
|
71 |
-
txt = re.sub(r"\s+", " ", text).strip()
|
72 |
for kws in COMMANDS.values():
|
73 |
for kw in kws:
|
74 |
-
|
75 |
-
return
|
76 |
|
77 |
def gerar_resposta(texto: str) -> str:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
out1 = generator(prompt1, max_length=512, do_sample=False)[0]["generated_text"].strip()
|
86 |
-
|
87 |
-
# 2) Humanização
|
88 |
-
prompt2 = f"{HUMANIZE[lang]}\n\n{out1}"
|
89 |
-
out2 = generator(prompt2, max_length=256, do_sample=False)[0]["generated_text"].strip()
|
90 |
|
91 |
-
|
|
|
92 |
|
93 |
-
# Interface Gradio
|
94 |
app = gr.Interface(
|
95 |
fn=gerar_resposta,
|
96 |
-
inputs=gr.Textbox(lines=6,
|
97 |
-
outputs=gr.Textbox(label="Resposta"),
|
98 |
-
title="🧠 IA Instruída
|
99 |
-
description="
|
100 |
)
|
101 |
|
102 |
if __name__ == "__main__":
|
|
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Garante previsibilidade na detecção de idioma
|
7 |
DetectorFactory.seed = 0
|
8 |
|
9 |
+
# Pipeline usando ptt5-base (português)
|
10 |
generator = pipeline(
|
11 |
"text2text-generation",
|
12 |
+
model="unicamp-dl/ptt5-base-portuguese-vocab",
|
13 |
+
tokenizer="unicamp-dl/ptt5-base-portuguese-vocab",
|
14 |
device=-1
|
15 |
)
|
16 |
|
|
|
17 |
COMMANDS = {
|
18 |
+
"resumo": ["resuma", "resumo", "resumir"],
|
19 |
+
"reescrever":["reescreva", "reformule", "reformular"],
|
20 |
+
"expandir": ["expanda", "expansão", "expandir", "detalhe"],
|
21 |
+
"corrigir": ["corrija", "corrigir", "melhore", "revise"]
|
22 |
}
|
23 |
|
|
|
24 |
INSTRUCTIONS = {
|
25 |
+
"resumo": "Resuma o seguinte texto:",
|
26 |
+
"reescrever":"Reescreva o seguinte texto com mais clareza e estilo:",
|
27 |
+
"expandir": "Expanda o seguinte texto com mais detalhes e explicações:",
|
28 |
+
"corrigir": "Corrija erros gramaticais e melhore o estilo do seguinte texto:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
|
31 |
def detect_language(text: str) -> str:
|
32 |
try:
|
33 |
+
return detect(text)
|
|
|
34 |
except:
|
35 |
return "pt"
|
36 |
|
37 |
def find_command(text: str) -> str:
|
|
|
38 |
for cmd, kws in COMMANDS.items():
|
39 |
for kw in kws:
|
40 |
+
if kw.lower() in text.lower():
|
41 |
return cmd
|
42 |
+
return "expandir"
|
43 |
|
44 |
def clean_text(text: str) -> str:
|
|
|
45 |
for kws in COMMANDS.values():
|
46 |
for kw in kws:
|
47 |
+
text = re.sub(rf"\b{kw}\b", "", text, flags=re.IGNORECASE)
|
48 |
+
return re.sub(r"\s+", " ", text).strip()
|
49 |
|
50 |
def gerar_resposta(texto: str) -> str:
|
51 |
+
idioma = detect_language(texto)
|
52 |
+
if idioma != "pt":
|
53 |
+
return "Desculpe, atualmente esta IA responde apenas em português."
|
54 |
|
55 |
+
cmd = find_command(texto)
|
56 |
+
core = clean_text(texto)
|
57 |
+
prompt = f"{INSTRUCTIONS[cmd]} {core}"
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
saida = generator(prompt, max_length=256, do_sample=False)[0]["generated_text"]
|
60 |
+
return saida.strip()
|
61 |
|
|
|
62 |
app = gr.Interface(
|
63 |
fn=gerar_resposta,
|
64 |
+
inputs=gr.Textbox(lines=6, label="Digite seu texto com 'resuma', 'expanda', etc..."),
|
65 |
+
outputs=gr.Textbox(label="Resposta da IA"),
|
66 |
+
title="🧠 IA de Texto Instruída em Português",
|
67 |
+
description="Entende comandos embutidos e responde em português com clareza.",
|
68 |
)
|
69 |
|
70 |
if __name__ == "__main__":
|