Spaces:
Paused
Paused
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # Cargar el modelo y el tokenizador en su versi贸n funcional | |
| 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 | |
| if "model" not in globals(): | |
| model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16) | |
| model.eval() | |
| # Funci贸n de humanizaci贸n con los par谩metros que mejor funcionaban | |
| def humanize_text(input_text): | |
| system_prompt = ( | |
| "Reescribe el siguiente texto de manera m谩s clara, natural y atractiva, " | |
| "sin cambiar su significado. Reformula frases r铆gidas y estructuradas para " | |
| "que sean m谩s fluidas y conversacionales, pero sin perder precisi贸n. " | |
| "Evita tecnicismos y burocracia 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=130, # 馃敼 Mantiene equilibrio entre reformulaci贸n y velocidad | |
| min_length=50, # 馃敼 Evita respuestas demasiado cortas | |
| do_sample=True, # 馃敼 Mantiene variabilidad sin ralentizar | |
| temperature=0.75, # 馃敼 Buen balance entre creatividad y rapidez | |
| top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura | |
| repetition_penalty=1.05, # 馃敼 Reduce repeticiones sin afectar fluidez | |
| num_return_sequences=1, # 馃敼 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 (Versi贸n 脫ptima Restaurada)") | |
| 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() | |