Spaces:
Paused
Paused
File size: 2,464 Bytes
6c036c1 aab52b9 b08158b a9b1392 aab52b9 b08158b 61e43c4 b08158b 19693ec 4025a2e b08158b 5ef6d7c 9894719 5ef6d7c e3d3bd5 b08158b 19693ec b08158b ea085fd f60a1b9 b08158b ea085fd b08158b ea085fd 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 estilo m谩s natural, accesible y persuasivo, "
"manteniendo su significado original. "
"Adopta un tono m谩s cercano y atractivo, ideal para comunicaci贸n en marketing. "
"Convierte estructuras r铆gidas en frases m谩s fluidas, pero sin perder profesionalismo. "
"Haz que el mensaje sea m谩s directo y f谩cil de leer."
)
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=150, # 馃敼 Reducimos para acelerar generaci贸n sin perder calidad
min_length=30, # 馃敼 Evita respuestas demasiado cortas
do_sample=True, # 馃敼 Mantiene variabilidad en la reescritura
temperature=0.7, # 馃敼 Mejor equilibrio entre calidad y velocidad
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
repetition_penalty=1.1, # 馃敼 Reduce repeticiones innecesarias sin limitar la fluidez
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()
|