Update README.md
Browse files
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=
|
| 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 |
-
#
|
| 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 =
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
decoded, carry_over_tokens = decode_predictions(input_ids.tolist(), preds.tolist(), tokenizer, carry_over_tokens)
|
| 184 |
sentences.extend(decoded)
|
| 185 |
|
| 186 |
-
#
|
| 187 |
if carry_over_tokens:
|
| 188 |
-
final_sentence = tokenizer.decode(carry_over_tokens, skip_special_tokens=
|
| 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 = "μλ
νμΈμ. μ€λ λ μ¨κ° μ°Έ μ’λ€μ. μ λ μ°μ±
μ λκ° μμ μ
λλ€."
|