File size: 2,458 Bytes
6c036c1
aab52b9
 
 
b08158b
a9b1392
aab52b9
b08158b
61e43c4
 
 
b08158b
 
 
19693ec
4025a2e
b08158b
5ef6d7c
4025a2e
b1f7af6
 
 
5ef6d7c
e3d3bd5
b08158b
 
 
 
19693ec
 
b08158b
b1f7af6
 
b08158b
b1f7af6
b08158b
4025a2e
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
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, conversacional y cercano, ideal para marketing. "
        "No agregues informaci贸n nueva ni cambies el significado del texto original. "
        "Haz que fluya mejor, pero sin perder ning煤n detalle. "
        "Mant茅n el contenido preciso y fiel a la versi贸n original, solo mejorando su claridad y atractivo."
    )

    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_length=256,  # 馃敼 Fijamos un l铆mite claro para evitar respuestas descontroladas
        max_new_tokens=200,  # 馃敼 M谩s margen para reformular el texto sin cortes
        do_sample=True,  # 馃敼 Mantiene variabilidad en la reescritura
        temperature=0.6,  # 馃敼 Reduce el riesgo de respuestas err谩ticas y aleatorias
        top_p=0.9,  # 馃敼 Mantiene coherencia en la reescritura
        repetition_penalty=1.1,  # 馃敼 Evita repeticiones innecesarias
        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()