Spaces:
Paused
Paused
File size: 2,193 Bytes
6c036c1 aab52b9 b08158b a9b1392 aab52b9 b08158b 61e43c4 b08158b 19693ec b08158b 5ef6d7c 8b24bf2 5ef6d7c e3d3bd5 b08158b 19693ec b08158b 8b24bf2 b08158b 8b24bf2 b08158b fd7c118 b08158b 19693ec aab52b9 19693ec aab52b9 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 |
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
def humanize_text(input_text):
system_prompt = (
"Reescribe el siguiente texto para que suene m谩s natural y conversacional. "
"Usa frases m谩s cortas y directas, como si lo explicara alguien en una conversaci贸n informal. "
"Evita estructuras r铆gidas o excesivamente formales. "
"Haz que la lectura sea m谩s fluida sin perder informaci贸n."
)
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, # 馃敼 M谩s margen para reformular el texto
do_sample=True, # 馃敼 Mantiene variabilidad en la reescritura
temperature=0.85, # 馃敼 Permite m谩s fluidez sin inventar datos
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
repetition_penalty=1.05, # 馃敼 Reduce rigidez en la estructura del texto
early_stopping=True,
)
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")
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()
|