Views
No views yet
pip install cadence-punctuation1from Cadence import PunctuationModel
2
3# Load model from local path or downloads at the specified directory
4model = PunctuationModel(model="Cadence-Fast","path/to/model")
5
6# Punctuate single text
7text = "hello world how are you today"
8result = model.punctuate([text])
9print(result[0]) # "Hello world, how are you today?"
10
11# Punctuate multiple texts
12texts = [
13 "hello world how are you",
14 "this is another test sentence",
15 "यह एक हिंदी वाक्य है" # Hindi example
16]
17results = model.punctuate(texts, batch_size=8)
18for original, punctuated in zip(texts, results):
19 print(f"Original: {original}")
20 print(f"Punctuated: {punctuated}")
21 print()1from transformers import AutoTokenizer, AutoModel
2import torch
3# Load model and tokenizer
4model_name = "ai4bharat/Cadence-Fast"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
7id2label = model.config.id2label
8text = "यह एक वाक्य है इसका क्या मतलब है"
9# text = "this is a test sentence what do you think"
10# Tokenize input and prepare for model
11inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
12input_ids = inputs['input_ids'][0] # Get input_ids for the first (and only) sentence
13with torch.no_grad():
14 outputs = model(**inputs)
15 predictions_for_sentence = torch.argmax(outputs.logits, dim=-1)[0]
16result_tokens_and_punctuation = []
17all_token_strings = tokenizer.convert_ids_to_tokens(input_ids.tolist()) # Get all token strings
18for i, token_id_value in enumerate(input_ids.tolist()):
19 # Process only non-padding tokens based on the attention mask
20 if inputs['attention_mask'][0][i] == 0:
21 continue
22 current_token_string = all_token_strings[i]
23 is_special_token = token_id_value in tokenizer.all_special_ids
24
25 if not is_special_token:
26 result_tokens_and_punctuation.append(current_token_string)
27
28 predicted_punctuation_id = predictions_for_sentence[i].item()
29 punctuation_character = id2label[predicted_punctuation_id]
30 if punctuation_character != "O" and not is_special_token:
31 result_tokens_and_punctuation.append(punctuation_character)
32punctuated_text = tokenizer.convert_tokens_to_string(result_tokens_and_punctuation)
33print(f"Original Text: {text}")
34print(f"Punctuated Text: {punctuated_text}")model: Can be choosen between "Cadence" (based on Gemma-3-1B) and "Cadence-Fast" (based on Gemma-3-270M). (default: "Cadence")model_path: Path to a local directory where model weights will be downloaded to and cached, or from which pre-downloaded weights will be loaded. If None, weights downloaded to default HuggingFace cache location.gpu_id: Specific GPU device ID to use (e.g., 0, 1). If None, the model will attempt to auto-detect and use an available GPU. This parameter is ignored if cpu is True. (default: None)cpu: If True, forces the model to run on the CPU, even if a GPU is available. (default: False)max_length: Maximum sequence length the model can process at once. If sliding_window is True, this value is used as the width of each sliding window. If sliding_window is False, texts longer than max_length will be truncated. (default: 300)attn_implementation: The attention implementation to use. (default: "eager")sliding_window: If True, enables sliding window mechanism to process texts longer than max_length. The text is split into overlapping chunks of max_length. If False, texts longer than max_length are truncated. (default: True)verbose: Enable verbose logging. (default: False)d_type: Precision with which weights are loaded. (default: bfloat16)batch_size ((for punctuate() method)): Batch size to use. (default: 8)1# Custom configuration
2model = PunctuationModel(
3 model="Cadence-Fast"
4 model_path="path/to/download/weights",
5 gpu_id=0, # Use specific GPU
6 max_length=512, # length for trunation; also used as window size when sliding_window=True
7 attn_implementation="flash_attention_2",
8 sliding_window=True, # Handle long texts
9 verbose=False, # Quiet mode
10 d_type="bfloat16"
11)
12batch_size=32
13# Process long texts with sliding window
14long_text = "Your very long text here..." * 100
15short_text = "a short text"
16result = model.punctuate([long_text, short_text],batch_size=batch_size)