Harinivas-28
commited on
Commit
•
99d616a
1
Parent(s):
b3c527b
Added Inference API
Browse files- inference.py +30 -0
- model_index.json +5 -0
inference.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from MorseH_Model import MorseHModel
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load model
|
6 |
+
model_path = "pytorch_model.bin" # Your model weights file
|
7 |
+
model = MorseHModel(input_size=<input_size>, output_size=<output_size>, max_length=<max_len>)
|
8 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Define preprocessing and decoding functions
|
12 |
+
def encode(text):
|
13 |
+
# Convert text to a list of indices
|
14 |
+
return [<label_encoder>.transform([c])[0] for c in text]
|
15 |
+
|
16 |
+
def predict(input_data):
|
17 |
+
with torch.no_grad():
|
18 |
+
inputs = torch.tensor([encode(input_data)])
|
19 |
+
output = model(inputs)
|
20 |
+
_, predicted = torch.max(output, 2)
|
21 |
+
return predicted[0]
|
22 |
+
|
23 |
+
def decode(prediction):
|
24 |
+
morse_code = ''.join(['.' if c == 0 else '-' for c in prediction if c != 2])
|
25 |
+
return morse_code
|
26 |
+
|
27 |
+
# Hugging Face Inference API function
|
28 |
+
def inference(text):
|
29 |
+
morse_code = [decode(predict(word)) for word in text.split()]
|
30 |
+
return " ".join(morse_code)
|
model_index.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"pipeline_tag": "text-to-morse",
|
3 |
+
"tags": ["morse-code", "pytorch", "custom-model"]
|
4 |
+
}
|
5 |
+
|