Spaces:
Running
Running
Commit
·
2e78fc6
1
Parent(s):
fa80eae
progress %
Browse files
app.py
CHANGED
@@ -41,19 +41,32 @@ def translate(text):
|
|
41 |
# Tokenize the input text
|
42 |
inputs = translation_tokenizer(text, return_tensors="pt", truncation=True)
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Decode the translated tokens
|
59 |
translated_text = translation_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
|
|
41 |
# Tokenize the input text
|
42 |
inputs = translation_tokenizer(text, return_tensors="pt", truncation=True)
|
43 |
|
44 |
+
# Calculate max_length based on input length (you may need to adjust this ratio)
|
45 |
+
max_length = min(512, int(inputs.input_ids.shape[1] * 1.5))
|
46 |
+
|
47 |
+
# Calculate max_new_tokens
|
48 |
+
max_new_tokens = max_length - inputs.input_ids.shape[1]
|
49 |
+
|
50 |
+
# Set up the progress bar
|
51 |
+
pbar = tqdm(total=max_new_tokens, desc="Translating", unit="token")
|
52 |
+
|
53 |
+
# Custom callback to update the progress bar
|
54 |
+
def update_progress_bar(beam_idx, token_idx, token):
|
55 |
+
pbar.update(1)
|
56 |
+
|
57 |
+
# Generate translation with progress updates
|
58 |
+
translated_tokens = translation_model.generate(
|
59 |
+
**inputs,
|
60 |
+
max_length=max_length,
|
61 |
+
num_beams=5,
|
62 |
+
no_repeat_ngram_size=2,
|
63 |
+
early_stopping=True,
|
64 |
+
callback=update_progress_bar,
|
65 |
+
callback_steps=1
|
66 |
+
)
|
67 |
+
|
68 |
+
# Close the progress bar
|
69 |
+
pbar.close()
|
70 |
|
71 |
# Decode the translated tokens
|
72 |
translated_text = translation_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|