Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
-
# Cargar el modelo
|
6 |
model_name = "BSC-LT/salamandra-2b"
|
7 |
|
8 |
if "tokenizer" not in globals():
|
@@ -10,16 +10,14 @@ if "tokenizer" not in globals():
|
|
10 |
tokenizer.pad_token = tokenizer.eos_token
|
11 |
|
12 |
if "model" not in globals():
|
13 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
14 |
model.eval()
|
15 |
|
16 |
-
# Funci贸n
|
17 |
def humanize_text(input_text):
|
18 |
system_prompt = (
|
19 |
-
"Reescribe el siguiente texto de manera m谩s clara
|
20 |
-
"sin cambiar su significado
|
21 |
-
"que sean m谩s fluidas y conversacionales, pero sin perder precisi贸n. "
|
22 |
-
"Evita tecnicismos y burocracia innecesaria."
|
23 |
)
|
24 |
|
25 |
prompt = f"{system_prompt}\n\nTexto original: {input_text}\n\nTexto humanizado:"
|
@@ -29,20 +27,20 @@ def humanize_text(input_text):
|
|
29 |
outputs = model.generate(
|
30 |
inputs.input_ids,
|
31 |
attention_mask=inputs.attention_mask,
|
32 |
-
max_new_tokens=
|
33 |
min_length=50, # 馃敼 Evita respuestas demasiado cortas
|
34 |
-
do_sample=True, # 馃敼
|
35 |
-
temperature=0.
|
36 |
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
|
37 |
-
repetition_penalty=1.
|
38 |
-
num_return_sequences=1, # 馃敼
|
39 |
)
|
40 |
|
41 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
|
43 |
# Interfaz en Gradio
|
44 |
with gr.Blocks() as demo:
|
45 |
-
gr.Markdown("# 鉁嶏笍 Humanizaci贸n de Texto con ALIA (
|
46 |
input_text = gr.Textbox(label="Pega aqu铆 el texto generado por IA para humanizar")
|
47 |
output_text = gr.Textbox(label="Texto humanizado por ALIA", interactive=False)
|
48 |
submit_button = gr.Button("Humanizar Texto")
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
+
# Cargar el modelo con optimizaci贸n de memoria
|
6 |
model_name = "BSC-LT/salamandra-2b"
|
7 |
|
8 |
if "tokenizer" not in globals():
|
|
|
10 |
tokenizer.pad_token = tokenizer.eos_token
|
11 |
|
12 |
if "model" not in globals():
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="cpu")
|
14 |
model.eval()
|
15 |
|
16 |
+
# Funci贸n optimizada para mejorar la velocidad
|
17 |
def humanize_text(input_text):
|
18 |
system_prompt = (
|
19 |
+
"Reescribe el siguiente texto de manera m谩s clara y natural. "
|
20 |
+
"Hazlo m谩s conversacional sin cambiar su significado ni omitir informaci贸n."
|
|
|
|
|
21 |
)
|
22 |
|
23 |
prompt = f"{system_prompt}\n\nTexto original: {input_text}\n\nTexto humanizado:"
|
|
|
27 |
outputs = model.generate(
|
28 |
inputs.input_ids,
|
29 |
attention_mask=inputs.attention_mask,
|
30 |
+
max_new_tokens=100, # 馃敼 Reducimos la cantidad de tokens generados
|
31 |
min_length=50, # 馃敼 Evita respuestas demasiado cortas
|
32 |
+
do_sample=True, # 馃敼 Mantenemos la variabilidad sin ralentizar
|
33 |
+
temperature=0.7, # 馃敼 Balance entre creatividad y rapidez
|
34 |
top_p=0.9, # 馃敼 Mantiene coherencia en la reescritura
|
35 |
+
repetition_penalty=1.02, # 馃敼 Reduce repeticiones sin afectar fluidez
|
36 |
+
num_return_sequences=1, # 馃敼 Solo una respuesta bien formulada
|
37 |
)
|
38 |
|
39 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
|
41 |
# Interfaz en Gradio
|
42 |
with gr.Blocks() as demo:
|
43 |
+
gr.Markdown("# 鉁嶏笍 Humanizaci贸n de Texto con ALIA (Optimizaci贸n de Velocidad)")
|
44 |
input_text = gr.Textbox(label="Pega aqu铆 el texto generado por IA para humanizar")
|
45 |
output_text = gr.Textbox(label="Texto humanizado por ALIA", interactive=False)
|
46 |
submit_button = gr.Button("Humanizar Texto")
|