Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,47 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Cargar el modelo y el tokenizador
|
| 6 |
model_name = "BSC-LT/salamandra-2b"
|
| 7 |
|
| 8 |
-
if "
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
tokenizer.pad_token = tokenizer.eos_token # 馃敼 Evita errores de atenci贸n
|
| 11 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
outputs = model.generate(
|
| 24 |
inputs.input_ids,
|
| 25 |
-
attention_mask=inputs.attention_mask,
|
| 26 |
-
max_new_tokens=
|
| 27 |
-
do_sample=True,
|
| 28 |
-
temperature=0.
|
| 29 |
-
top_p=0.9, # 馃敼
|
| 30 |
-
repetition_penalty=1.1, # 馃敼 Evita
|
| 31 |
-
early_stopping=
|
| 32 |
)
|
| 33 |
|
| 34 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
# Interfaz en Gradio
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
-
gr.Markdown("#
|
| 39 |
-
input_text = gr.Textbox(label="
|
| 40 |
-
output_text = gr.Textbox(label="
|
| 41 |
-
submit_button = gr.Button("
|
| 42 |
-
submit_button.click(
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Cargar el modelo y el tokenizador solo una vez para evitar recargas
|
| 6 |
model_name = "BSC-LT/salamandra-2b"
|
| 7 |
|
| 8 |
+
if "tokenizer" not in globals():
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
tokenizer.pad_token = tokenizer.eos_token # 馃敼 Evita errores de atenci贸n
|
|
|
|
| 11 |
|
| 12 |
+
if "model" not in globals():
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 14 |
+
model.eval() # 馃敼 Optimiza la inferencia para que sea m谩s r谩pida
|
| 15 |
|
| 16 |
+
# Funci贸n exclusiva para humanizaci贸n de texto
|
| 17 |
+
def humanize_text(input_text):
|
| 18 |
+
system_prompt = (
|
| 19 |
+
"Reescribe el siguiente texto para que sea m谩s natural y humano, "
|
| 20 |
+
"sin cambiar su significado ni a帽adir informaci贸n nueva. Usa un lenguaje claro y fluido."
|
| 21 |
)
|
| 22 |
|
| 23 |
+
prompt = f"{system_prompt}\n\nTexto original: {input_text}\n\nTexto humanizado:"
|
| 24 |
+
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 26 |
+
|
| 27 |
outputs = model.generate(
|
| 28 |
inputs.input_ids,
|
| 29 |
+
attention_mask=inputs.attention_mask,
|
| 30 |
+
max_new_tokens=100, # 馃敼 Se aumenta para evitar cortes en textos largos
|
| 31 |
+
do_sample=True, # 馃敼 Mantiene variabilidad en la reescritura
|
| 32 |
+
temperature=0.6, # 馃敼 Da un poco m谩s de flexibilidad en la generaci贸n
|
| 33 |
+
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
|
| 34 |
+
repetition_penalty=1.1, # 馃敼 Evita respuestas repetitivas
|
| 35 |
+
early_stopping=True,
|
| 36 |
)
|
| 37 |
|
| 38 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
|
| 40 |
# Interfaz en Gradio
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# 鉁嶏笍 Humanizaci贸n de Texto con ALIA")
|
| 43 |
+
input_text = gr.Textbox(label="Pega aqu铆 el texto generado por IA para humanizar")
|
| 44 |
+
output_text = gr.Textbox(label="Texto humanizado por ALIA", interactive=False)
|
| 45 |
+
submit_button = gr.Button("Humanizar Texto")
|
| 46 |
+
submit_button.click(humanize_text, inputs=input_text, outputs=output_text)
|
| 47 |
|
| 48 |
demo.launch()
|