Downloads · 30 days
14
14% of all-time downloads
Harinivas-28/MorseH_Model
MorseH_Model is a machine learning model from Harinivas-28. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for pytorch. The card lists the license as apache-2.0.
This model is designed to convert textual characters into Morse code symbols (dots, dashes, and spaces) using a custom neural network in PyTorch.
Downloads · 30 days
14
14% of all-time downloads
All-time downloads
98
Public
Repo size
35.1 KB
Likes
2
Public
Click a slice to open those files.
.pth23.9 KB · 36%
From the Hugging Face model README
This model is designed to convert textual characters into Morse code symbols (dots, dashes, and spaces) using a custom neural network in PyTorch.
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.
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.
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)