Spaces:
Sleeping
Sleeping
nicekd commited on
Commit ·
d434d0e
1
Parent(s): d386855
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
@@ -25,11 +26,20 @@ def load_model():
|
|
| 25 |
|
| 26 |
@torch.inference_mode()
|
| 27 |
def translate_es_to_pt(text, beams, max_new_tokens):
|
|
|
|
| 28 |
if not text or not text.strip():
|
| 29 |
-
return ""
|
| 30 |
|
| 31 |
tok, mdl = load_model()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
inputs = tok(
|
| 34 |
text,
|
| 35 |
return_tensors="pt",
|
|
@@ -39,22 +49,85 @@ def translate_es_to_pt(text, beams, max_new_tokens):
|
|
| 39 |
|
| 40 |
outputs = mdl.generate(
|
| 41 |
**inputs,
|
| 42 |
-
num_beams=
|
| 43 |
-
max_new_tokens=
|
| 44 |
length_penalty=1.0,
|
| 45 |
early_stopping=True,
|
| 46 |
-
no_repeat_ngram_size=3,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
with gr.Blocks(theme=gr.themes.Soft(), title="ES → PT Translator") as demo:
|
| 53 |
gr.Markdown(
|
| 54 |
-
"""
|
| 55 |
# ES → PT Translator (BART)
|
| 56 |
|
| 57 |
-
**Model:** `
|
| 58 |
**Dataset:** Helsinki-NLP/Tatoeba (es-pt)
|
| 59 |
**Metric:** chrF (beam search evaluation)
|
| 60 |
|
|
@@ -78,15 +151,26 @@ Tip: If output looks repetitive, try lowering **beams** or **max_new_tokens**.
|
|
| 78 |
btn_translate = gr.Button("Translate", variant="primary")
|
| 79 |
btn_clear = gr.Button("Clear")
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
out = gr.Textbox(label="Portuguese (pt) output", lines=6)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
examples = gr.Examples(
|
| 85 |
examples=[
|
| 86 |
["Las personas dicen que estoy loco."],
|
| 87 |
["¿Puedes ayudarme a traducir esta frase, por favor?"],
|
| 88 |
-
["Mañana vamos al mercado a comprar frutas y pan."],
|
| 89 |
-
["Si
|
| 90 |
],
|
| 91 |
inputs=inp,
|
| 92 |
label="Examples",
|
|
@@ -95,11 +179,17 @@ Tip: If output looks repetitive, try lowering **beams** or **max_new_tokens**.
|
|
| 95 |
btn_translate.click(
|
| 96 |
fn=translate_es_to_pt,
|
| 97 |
inputs=[inp, beams, max_new],
|
| 98 |
-
outputs=out,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
-
btn_clear.click(lambda: ("", ""), outputs=[inp, out])
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.queue()
|
| 105 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import time
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 26 |
|
| 27 |
@torch.inference_mode()
|
| 28 |
def translate_es_to_pt(text, beams, max_new_tokens):
|
| 29 |
+
"""Single run translation + debug info."""
|
| 30 |
if not text or not text.strip():
|
| 31 |
+
return "", "No input."
|
| 32 |
|
| 33 |
tok, mdl = load_model()
|
| 34 |
|
| 35 |
+
beams = int(beams)
|
| 36 |
+
max_new_tokens = int(max_new_tokens)
|
| 37 |
+
|
| 38 |
+
# LOG: verify slider values are reaching the backend
|
| 39 |
+
print(f"[RUN] beams={beams} max_new_tokens={max_new_tokens} device={device}")
|
| 40 |
+
|
| 41 |
+
t0 = time.time()
|
| 42 |
+
|
| 43 |
inputs = tok(
|
| 44 |
text,
|
| 45 |
return_tensors="pt",
|
|
|
|
| 49 |
|
| 50 |
outputs = mdl.generate(
|
| 51 |
**inputs,
|
| 52 |
+
num_beams=beams,
|
| 53 |
+
max_new_tokens=max_new_tokens,
|
| 54 |
length_penalty=1.0,
|
| 55 |
early_stopping=True,
|
| 56 |
+
no_repeat_ngram_size=3,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
out_text = tok.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
|
| 61 |
+
dt = time.time() - t0
|
| 62 |
+
debug = (
|
| 63 |
+
f"beams={beams} | max_new_tokens={max_new_tokens} | device={device} | "
|
| 64 |
+
f"gen_time={dt:.2f}s | output_chars={len(out_text)}"
|
| 65 |
)
|
| 66 |
|
| 67 |
+
return out_text, debug
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@torch.inference_mode()
|
| 71 |
+
def quick_test_suite(text):
|
| 72 |
+
"""
|
| 73 |
+
Runs the same input through multiple settings so you can compare
|
| 74 |
+
beams/tokens behavior in one shot.
|
| 75 |
+
"""
|
| 76 |
+
if not text or not text.strip():
|
| 77 |
+
return "No input."
|
| 78 |
+
|
| 79 |
+
tok, mdl = load_model()
|
| 80 |
+
|
| 81 |
+
settings = [
|
| 82 |
+
(1, 64),
|
| 83 |
+
(3, 128),
|
| 84 |
+
(5, 128),
|
| 85 |
+
(8, 128),
|
| 86 |
+
(5, 32), # truncation stress test
|
| 87 |
+
(5, 256), # long generation stress test
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
lines = []
|
| 91 |
+
lines.append(f"MODEL: {MODEL_ID}")
|
| 92 |
+
lines.append(f"DEVICE: {device}")
|
| 93 |
+
lines.append("-" * 60)
|
| 94 |
+
|
| 95 |
+
for beams, max_new in settings:
|
| 96 |
+
print(f"[TEST] beams={beams} max_new_tokens={max_new}")
|
| 97 |
+
t0 = time.time()
|
| 98 |
+
|
| 99 |
+
inputs = tok(
|
| 100 |
+
text,
|
| 101 |
+
return_tensors="pt",
|
| 102 |
+
truncation=True,
|
| 103 |
+
max_length=256,
|
| 104 |
+
).to(device)
|
| 105 |
+
|
| 106 |
+
outputs = mdl.generate(
|
| 107 |
+
**inputs,
|
| 108 |
+
num_beams=beams,
|
| 109 |
+
max_new_tokens=max_new,
|
| 110 |
+
length_penalty=1.0,
|
| 111 |
+
early_stopping=True,
|
| 112 |
+
no_repeat_ngram_size=3,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
out_text = tok.decode(outputs[0], skip_special_tokens=True)
|
| 116 |
+
dt = time.time() - t0
|
| 117 |
+
|
| 118 |
+
lines.append(f"[beams={beams}, max_new={max_new}] time={dt:.2f}s chars={len(out_text)}")
|
| 119 |
+
lines.append(out_text)
|
| 120 |
+
lines.append("-" * 60)
|
| 121 |
+
|
| 122 |
+
return "\n".join(lines)
|
| 123 |
|
| 124 |
|
| 125 |
with gr.Blocks(theme=gr.themes.Soft(), title="ES → PT Translator") as demo:
|
| 126 |
gr.Markdown(
|
| 127 |
+
f"""
|
| 128 |
# ES → PT Translator (BART)
|
| 129 |
|
| 130 |
+
**Model:** `{MODEL_ID}`
|
| 131 |
**Dataset:** Helsinki-NLP/Tatoeba (es-pt)
|
| 132 |
**Metric:** chrF (beam search evaluation)
|
| 133 |
|
|
|
|
| 151 |
btn_translate = gr.Button("Translate", variant="primary")
|
| 152 |
btn_clear = gr.Button("Clear")
|
| 153 |
|
| 154 |
+
debug_box = gr.Textbox(
|
| 155 |
+
label="Run info (debug)",
|
| 156 |
+
value="",
|
| 157 |
+
lines=2,
|
| 158 |
+
interactive=False,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
with gr.Column(scale=1):
|
| 162 |
out = gr.Textbox(label="Portuguese (pt) output", lines=6)
|
| 163 |
|
| 164 |
+
with gr.Row():
|
| 165 |
+
btn_test = gr.Button("Run quick test suite (compare beams/tokens)")
|
| 166 |
+
test_report = gr.Textbox(label="Test suite report", lines=18)
|
| 167 |
+
|
| 168 |
examples = gr.Examples(
|
| 169 |
examples=[
|
| 170 |
["Las personas dicen que estoy loco."],
|
| 171 |
["¿Puedes ayudarme a traducir esta frase, por favor?"],
|
| 172 |
+
["Mañana vamos al mercado a comprar frutas y pan, y después visitaremos a mis abuelos."],
|
| 173 |
+
["Si el proyecto termina hoy, podremos presentar los resultados mañana, pero necesitamos revisar los datos con cuidado para evitar errores."],
|
| 174 |
],
|
| 175 |
inputs=inp,
|
| 176 |
label="Examples",
|
|
|
|
| 179 |
btn_translate.click(
|
| 180 |
fn=translate_es_to_pt,
|
| 181 |
inputs=[inp, beams, max_new],
|
| 182 |
+
outputs=[out, debug_box],
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
btn_test.click(
|
| 186 |
+
fn=quick_test_suite,
|
| 187 |
+
inputs=inp,
|
| 188 |
+
outputs=test_report,
|
| 189 |
)
|
| 190 |
|
| 191 |
+
btn_clear.click(lambda: ("", "", ""), outputs=[inp, out, debug_box])
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
demo.queue()
|
| 195 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|