Spaces:
Paused
Paused
File size: 2,470 Bytes
6c036c1 aab52b9 b08158b a9b1392 aab52b9 b08158b 61e43c4 b08158b 19693ec 4025a2e b08158b 5ef6d7c bd65059 5ef6d7c e3d3bd5 b08158b 19693ec b08158b bd65059 f60a1b9 b08158b bd65059 b08158b bd65059 b1f7af6 19693ec aab52b9 19693ec aab52b9 4025a2e b08158b aab52b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Cargar el modelo y el tokenizador solo una vez para evitar recargas
model_name = "BSC-LT/salamandra-2b"
if "tokenizer" not in globals():
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token # 馃敼 Evita errores de atenci贸n
if "model" not in globals():
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
model.eval() # 馃敼 Optimiza la inferencia para que sea m谩s r谩pida
# Funci贸n exclusiva para humanizaci贸n de texto con enfoque en marketing
def humanize_text(input_text):
system_prompt = (
"Reescribe el siguiente texto con un tono m谩s natural, fluido y cercano, "
"ideal para comunicaci贸n efectiva en marketing. "
"Haz que sea m谩s f谩cil de leer y comprender, sin modificar su significado ni agregar informaci贸n nueva. "
"Reformula frases largas en estructuras m谩s din谩micas y conversacionales. "
"Evita un tono demasiado formal y cualquier tipo de exclamaci贸n innecesaria."
)
prompt = f"{system_prompt}\n\nTexto original: {input_text}\n\nTexto humanizado:"
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
outputs = model.generate(
inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=180, # 馃敼 M谩s control en la extensi贸n del texto
min_length=30, # 馃敼 Evita respuestas demasiado cortas
do_sample=True, # 馃敼 Mantiene variabilidad en la reescritura
temperature=0.75, # 馃敼 M谩s flexibilidad sin perder coherencia
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
repetition_penalty=1.05, # 馃敼 Permite m谩s fluidez en la generaci贸n
num_return_sequences=1, # 馃敼 Genera solo una respuesta bien formulada
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Interfaz en Gradio
with gr.Blocks() as demo:
gr.Markdown("# 鉁嶏笍 Humanizaci贸n de Texto con ALIA (Optimizado para Marketing)")
input_text = gr.Textbox(label="Pega aqu铆 el texto generado por IA para humanizar")
output_text = gr.Textbox(label="Texto humanizado por ALIA", interactive=False)
submit_button = gr.Button("Humanizar Texto")
submit_button.click(humanize_text, inputs=input_text, outputs=output_text)
demo.launch()
|