pentarosarium commited on
Commit
fc37b5b
·
1 Parent(s): f6e9269

russification and optimization continued errors correction

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -43,22 +43,33 @@ def translate(text):
43
  # Get the number of tokens in the input
44
  input_length = inputs.input_ids.shape[1]
45
 
46
- # Set up the progress bar
47
- progress_bar = tqdm(total=input_length, desc="Translating", unit="token")
48
 
49
- # Custom callback to update the progress bar
50
- def update_progress_bar(beam_idx, token_idx, token):
51
- progress_bar.update(1)
52
 
53
- # Generate translation with progress updates
 
54
  translated_tokens = translation_model.generate(
55
  **inputs,
56
  num_beams=5,
57
- max_length=input_length + 50, # Adjust as needed
58
- callback=update_progress_bar
 
59
  )
60
 
61
- # Close the progress bar
 
 
 
 
 
 
 
 
 
 
62
  progress_bar.close()
63
 
64
  # Decode the translated tokens
 
43
  # Get the number of tokens in the input
44
  input_length = inputs.input_ids.shape[1]
45
 
46
+ # Estimate the maximum length of the output
47
+ max_length = input_length * 2 # This is an estimate, adjust as needed
48
 
49
+ # Set up the progress bar
50
+ progress_bar = tqdm(total=100, desc="Translating", unit="%")
 
51
 
52
+ # Generate translation
53
+ start_time = time.time()
54
  translated_tokens = translation_model.generate(
55
  **inputs,
56
  num_beams=5,
57
+ max_length=max_length,
58
+ no_repeat_ngram_size=2,
59
+ early_stopping=True
60
  )
61
 
62
+ # Estimate progress based on time
63
+ while not translated_tokens.size(1):
64
+ elapsed_time = time.time() - start_time
65
+ estimated_progress = min(int((elapsed_time / (input_length * 0.1)) * 100), 99)
66
+ progress_bar.n = estimated_progress
67
+ progress_bar.refresh()
68
+ time.sleep(0.1)
69
+
70
+ # Ensure the progress bar reaches 100%
71
+ progress_bar.n = 100
72
+ progress_bar.refresh()
73
  progress_bar.close()
74
 
75
  # Decode the translated tokens