Edit model card

MorseH_Model

This model is designed to convert textual characters into Morse code symbols (dots, dashes, and spaces) using a custom neural network in PyTorch.

Model Architecture

This is built on a simple CNN Model which translates text to morse-code. Please refer Perceptron and Multilayer Perceptron to understand the model architecture. The model uses an embedding layer followed by two fully connected layers to predict Morse code encodings.

Model Inputs and Outputs

  • Inputs: Character indices of textual input.
  • Outputs: Morse code sequence for each character in the input.

Training and Dataset

  • Dataset: Custom Morse code dataset.
  • Training: Trained for 20 epochs with a batch size of 16.

NOTE

This Model cannot translate ',' to morse code because it is not included in the RAW Dataset.
Pull me a request If you find to solve this instead of a csv file as a dataset.

Usage

Below is an example of how to use the model.

# Load the model weights if available
try:
    model.load_state_dict(torch.load('morse_model_weights.pth', weights_only=True))
except FileNotFoundError:
    print("Pre-trained weights not found, start training from scratch.")

# INFERENCE FUNCTIONS
def predict(character_index):
    """Predict the Morse code sequence for a given character index."""
    with torch.no_grad():
        output = model(torch.tensor([character_index]))
        _, prediction = torch.max(output, 2)
        return prediction[0]

def decode(prediction):
    """Decode a prediction from numerical values to Morse code symbols."""
    prediction = [p for p in prediction if p != 2]
    return ''.join('.' if c == 0 else '-' for c in prediction)

def encode(word):
    """Encode a word into character indices."""
    return [label_encoder.transform([char])[0] for char in word.upper()]

def get_morse_word(word):
    """Convert a word into Morse code using the model predictions."""
    char_indices = encode(word)
    morse_sequence = []
    for index in char_indices:
        pred = predict(index)
        morse_sequence.append(decode(pred))
        morse_sequence.append(' ')
    return ''.join(morse_sequence)

# USER INPUT INFERENCE
user_input = input("Type your message: ")
response = [get_morse_word(word) + '   ' for word in user_input.split()]
response = ''.join(response)

print("Response: ", response)
Downloads last month
12
Inference API
Unable to determine this model’s pipeline type. Check the docs .