ggomarobot commited on
Commit
f9350ef
Β·
verified Β·
1 Parent(s): 68609e4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +22 -10
README.md CHANGED
@@ -90,11 +90,11 @@ def decode_predictions(input_ids: torch.Tensor, predictions: torch.Tensor, token
90
 
91
  for token_id, pred in zip(input_ids, predictions):
92
  current_tokens.append(token_id)
93
- if pred == 1: # λ¬Έμž₯이 λλ‚˜λŠ” 토큰
94
- sentence = tokenizer.decode(current_tokens, skip_special_tokens=True).strip()
95
  if sentence:
96
  results.append(sentence)
97
- current_tokens = [] # λ‹€μŒ λ¬Έμž₯을 μœ„ν•΄ μ΄ˆκΈ°ν™”
98
 
99
  return results, current_tokens
100
 
@@ -167,30 +167,42 @@ def inference(input_text: str) -> List[str]:
167
  """
168
  chunks = preprocess_text(input_text)
169
  carry_over_tokens = []
 
170
  sentences = []
171
 
172
  for chunk in chunks:
173
- # Move inputs to the same device as the model
174
  inputs = {
175
  "input_ids": torch.tensor([chunk["input_ids"]], dtype=torch.long, device=device),
176
  "attention_mask": torch.tensor([chunk["attention_mask"]], dtype=torch.long, device=device),
177
  }
178
  with torch.no_grad():
179
  outputs = model(**inputs)
180
- predictions = outputs.logits.argmax(dim=-1)
181
 
182
- for input_ids, preds in zip(inputs["input_ids"].cpu(), predictions.cpu()):
 
 
 
 
 
 
 
 
 
 
 
 
183
  decoded, carry_over_tokens = decode_predictions(input_ids.tolist(), preds.tolist(), tokenizer, carry_over_tokens)
184
  sentences.extend(decoded)
185
 
186
- # Handle any remaining carry-over tokens
187
  if carry_over_tokens:
188
- final_sentence = tokenizer.decode(carry_over_tokens, skip_special_tokens=True).strip()
189
  if final_sentence:
190
  sentences.append(final_sentence)
191
 
192
- # Process repetitions in the final output
193
- return handle_repetitions(sentences)
194
 
195
  # μ‚¬μš© 예제
196
  input_text = "μ•ˆλ…•ν•˜μ„Έμš”. 였늘 날씨가 μ°Έ μ’‹λ„€μš”. μ €λŠ” 산책을 λ‚˜κ°ˆ μ˜ˆμ •μž…λ‹ˆλ‹€."
 
90
 
91
  for token_id, pred in zip(input_ids, predictions):
92
  current_tokens.append(token_id)
93
+ if pred == 1: # Sentence-ending label
94
+ sentence = tokenizer.decode(current_tokens, skip_special_tokens=False, clean_up_tokenization_spaces=False).strip()
95
  if sentence:
96
  results.append(sentence)
97
+ current_tokens = [] # Reset for the next sentence
98
 
99
  return results, current_tokens
100
 
 
167
  """
168
  chunks = preprocess_text(input_text)
169
  carry_over_tokens = []
170
+ carry_over_labels = []
171
  sentences = []
172
 
173
  for chunk in chunks:
174
+ # Prepare inputs for the model
175
  inputs = {
176
  "input_ids": torch.tensor([chunk["input_ids"]], dtype=torch.long, device=device),
177
  "attention_mask": torch.tensor([chunk["attention_mask"]], dtype=torch.long, device=device),
178
  }
179
  with torch.no_grad():
180
  outputs = model(**inputs)
181
+ predictions = torch.argmax(outputs.logits, dim=-1).squeeze().tolist()
182
 
183
+ # Ensure predictions is a list
184
+ if isinstance(predictions, int):
185
+ predictions = [predictions]
186
+
187
+ # Handle carry-over tokens
188
+ if carry_over_tokens:
189
+ chunk["input_ids"] = carry_over_tokens + chunk["input_ids"]
190
+ predictions = carry_over_labels + predictions
191
+ carry_over_tokens = []
192
+ carry_over_labels = []
193
+
194
+ # Decode predictions
195
+ for input_ids, preds in zip(inputs["input_ids"].cpu(), predictions):
196
  decoded, carry_over_tokens = decode_predictions(input_ids.tolist(), preds.tolist(), tokenizer, carry_over_tokens)
197
  sentences.extend(decoded)
198
 
199
+ # Process remaining carry-over tokens
200
  if carry_over_tokens:
201
+ final_sentence = tokenizer.decode(carry_over_tokens, skip_special_tokens=False, clean_up_tokenization_spaces=False).strip()
202
  if final_sentence:
203
  sentences.append(final_sentence)
204
 
205
+ return handle_repetitions(sentences) # Process repetitions in the final output
 
206
 
207
  # μ‚¬μš© 예제
208
  input_text = "μ•ˆλ…•ν•˜μ„Έμš”. 였늘 날씨가 μ°Έ μ’‹λ„€μš”. μ €λŠ” 산책을 λ‚˜κ°ˆ μ˜ˆμ •μž…λ‹ˆλ‹€."