Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,61 @@
|
|
1 |
-
|
2 |
import re
|
3 |
from langdetect import detect, DetectorFactory
|
4 |
from transformers import pipeline
|
5 |
import gradio as gr
|
6 |
|
7 |
-
#
|
8 |
DetectorFactory.seed = 0
|
9 |
|
10 |
-
#
|
11 |
generator = pipeline(
|
12 |
-
"
|
13 |
-
model="
|
14 |
device=-1
|
15 |
)
|
16 |
|
17 |
-
#
|
18 |
COMMANDS = {
|
19 |
-
"resumo":
|
20 |
-
"reescrever":
|
21 |
-
"expandir":
|
22 |
-
"corrigir":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
}
|
30 |
|
31 |
def detect_language(text: str) -> str:
|
32 |
try:
|
33 |
-
|
34 |
-
return
|
35 |
except:
|
36 |
return "pt"
|
37 |
|
@@ -41,67 +65,39 @@ def find_command(text: str) -> str:
|
|
41 |
for kw in kws:
|
42 |
if kw in low:
|
43 |
return cmd
|
44 |
-
return "
|
45 |
|
46 |
def clean_text(text: str) -> str:
|
47 |
-
txt = re.sub(r"\s+", " ", text)
|
48 |
for kws in COMMANDS.values():
|
49 |
for kw in kws:
|
50 |
txt = re.sub(rf"\b{kw}\b", "", txt, flags=re.IGNORECASE)
|
51 |
return txt.strip()
|
52 |
|
53 |
-
def build_prompt(core: str, cmd: str, lang: str) -> str:
|
54 |
-
if cmd == "resumo":
|
55 |
-
head = {
|
56 |
-
"pt": "Resuma o texto a seguir de forma concisa:\n\n",
|
57 |
-
"en": "Summarize the following text concisely:\n\n",
|
58 |
-
"fr": "Résumez le texte suivant de manière concise :\n\n"
|
59 |
-
}
|
60 |
-
elif cmd == "reescrever":
|
61 |
-
head = {
|
62 |
-
"pt": "Reescreva este texto com mais clareza e estilo:\n\n",
|
63 |
-
"en": "Rewrite this text with more clarity and style:\n\n",
|
64 |
-
"fr": "Réécrivez ce texte avec plus de clarté et de style :\n\n"
|
65 |
-
}
|
66 |
-
elif cmd == "expandir":
|
67 |
-
head = {
|
68 |
-
"pt": "Expanda este texto, adicionando detalhes e explicações:\n\n",
|
69 |
-
"en": "Expand this text, adding details and explanations:\n\n",
|
70 |
-
"fr": "Développez ce texte en ajoutant des détails et des explications :\n\n"
|
71 |
-
}
|
72 |
-
elif cmd == "corrigir":
|
73 |
-
head = {
|
74 |
-
"pt": "Corrija gramática, ortografia e estilo deste texto:\n\n",
|
75 |
-
"en": "Correct the grammar, spelling, and style of this text:\n\n",
|
76 |
-
"fr": "Corrigez la grammaire, l'orthographe et le style de ce texte :\n\n"
|
77 |
-
}
|
78 |
-
else:
|
79 |
-
head = {"pt":"","en":"","fr":""}
|
80 |
-
return head[lang] + core + "\n\n"
|
81 |
-
|
82 |
def gerar_resposta(texto: str) -> str:
|
83 |
lang = detect_language(texto)
|
84 |
cmd = find_command(texto)
|
85 |
core = clean_text(texto)
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
|
95 |
-
return
|
96 |
|
97 |
-
|
|
|
98 |
fn=gerar_resposta,
|
99 |
-
inputs=gr.Textbox(lines=6, placeholder="
|
100 |
-
outputs=gr.Textbox(label="Resposta
|
101 |
-
title="🧠 IA
|
102 |
-
description="Detecta comando embutido, idioma e
|
103 |
-
allow_flagging="never"
|
104 |
)
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
-
|
|
|
|
|
1 |
import re
|
2 |
from langdetect import detect, DetectorFactory
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Para resultados determinísticos na detecção de idioma
|
7 |
DetectorFactory.seed = 0
|
8 |
|
9 |
+
# Pipeline de geração de texto instruído (text2text) com FLAN-T5
|
10 |
generator = pipeline(
|
11 |
+
"text2text-generation",
|
12 |
+
model="google/flan-t5-small",
|
13 |
device=-1
|
14 |
)
|
15 |
|
16 |
+
# Palavras-chave de comando em pt/en/fr
|
17 |
COMMANDS = {
|
18 |
+
"resumo": ["resuma", "resumo", "resumir", "summarize", "résumé", "résumer"],
|
19 |
+
"reescrever":["reescreva", "reformule", "reformular", "rewrite", "réécrire"],
|
20 |
+
"expandir": ["expanda", "expansão", "expandir", "detalhe", "expand", "développez"],
|
21 |
+
"corrigir": ["corrija", "corrigir", "melhore", "revise", "correct", "corriger"]
|
22 |
+
}
|
23 |
+
|
24 |
+
# Mensagens de instrução por comando e idioma
|
25 |
+
INSTRUCTIONS = {
|
26 |
+
"resumo": {
|
27 |
+
"pt": "Resuma o texto a seguir de forma concisa:",
|
28 |
+
"en": "Please summarize the following text concisely:",
|
29 |
+
"fr": "Veuillez résumer le texte suivant de manière concise :"
|
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 |
+
code = detect(text)
|
58 |
+
return code if code in HUMANIZE else "pt"
|
59 |
except:
|
60 |
return "pt"
|
61 |
|
|
|
65 |
for kw in kws:
|
66 |
if kw in low:
|
67 |
return cmd
|
68 |
+
return "expandir" # padrão se não achar comando
|
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 |
txt = re.sub(rf"\b{kw}\b", "", txt, flags=re.IGNORECASE)
|
75 |
return txt.strip()
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
def gerar_resposta(texto: str) -> str:
|
78 |
lang = detect_language(texto)
|
79 |
cmd = find_command(texto)
|
80 |
core = clean_text(texto)
|
81 |
|
82 |
+
# 1) Geração principal via FLAN-T5
|
83 |
+
instr = INSTRUCTIONS[cmd][lang]
|
84 |
+
prompt1 = f"{instr}\n\n{core}"
|
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 |
+
return out2
|
92 |
|
93 |
+
# Interface Gradio
|
94 |
+
app = gr.Interface(
|
95 |
fn=gerar_resposta,
|
96 |
+
inputs=gr.Textbox(lines=6, placeholder="Escreva algo com 'resuma', 'expanda', etc...", label="Entrada"),
|
97 |
+
outputs=gr.Textbox(label="Resposta"),
|
98 |
+
title="🧠 IA Instruída Multilíngue",
|
99 |
+
description="Detecta idioma e comando embutido, responde no mesmo idioma e humaniza o texto."
|
|
|
100 |
)
|
101 |
|
102 |
if __name__ == "__main__":
|
103 |
+
app.launch()
|