Spaces:
Sleeping
Sleeping
Create inference.py
Browse files- inference.py +46 -0
inference.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import onnxruntime as ort
|
4 |
+
from transformers import AutoTokenizer
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Load the ONNX model and tokenizer
|
8 |
+
model_path = "model.onnx"
|
9 |
+
translation_session = ort.InferenceSession(model_path)
|
10 |
+
translation_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
|
11 |
+
|
12 |
+
def translate_text(input_text):
|
13 |
+
# Tokenize input text
|
14 |
+
tokenized_input = translation_tokenizer(
|
15 |
+
input_text, return_tensors="np", padding=True, truncation=True, max_length=512
|
16 |
+
)
|
17 |
+
|
18 |
+
input_ids = tokenized_input["input_ids"].astype(np.int64)
|
19 |
+
attention_mask = tokenized_input["attention_mask"].astype(np.int64)
|
20 |
+
|
21 |
+
# Run inference with the ONNX model
|
22 |
+
outputs = translation_session.run(
|
23 |
+
None,
|
24 |
+
{
|
25 |
+
"input_ids": input_ids,
|
26 |
+
"attention_mask": attention_mask,
|
27 |
+
}
|
28 |
+
)
|
29 |
+
|
30 |
+
# Decode the output tokens
|
31 |
+
translated_tokens = np.argmax(outputs[0], axis=-1)
|
32 |
+
translated_text = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
33 |
+
|
34 |
+
return translated_text
|
35 |
+
|
36 |
+
# Create a Gradio interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=translate_text,
|
39 |
+
inputs="text",
|
40 |
+
outputs="text",
|
41 |
+
title="Frenchizer Translation Model",
|
42 |
+
description="Translate text from English to French using an ONNX model."
|
43 |
+
)
|
44 |
+
|
45 |
+
# Launch the Gradio app
|
46 |
+
interface.launch()
|