Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,58 +4,65 @@ from transformers import MarianTokenizer
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Load the tokenizer from the local folder
|
| 7 |
-
model_path = "./onnx_model" # Path to the folder containing the
|
| 8 |
tokenizer = MarianTokenizer.from_pretrained(model_path)
|
| 9 |
|
| 10 |
# Load the ONNX model
|
| 11 |
onnx_model_path = "./model.onnx"
|
| 12 |
session = ort.InferenceSession(onnx_model_path)
|
| 13 |
|
| 14 |
-
def translate_text(input_texts):
|
| 15 |
-
# Tokenize input texts
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
interface = gr.Interface(
|
| 53 |
-
|
| 54 |
-
inputs="text",
|
| 55 |
-
outputs="
|
| 56 |
-
title="
|
| 57 |
-
description="Translate text
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# Launch the
|
| 61 |
interface.launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Load the tokenizer from the local folder
|
| 7 |
+
model_path = "./onnx_model" # Path to the folder containing the tokenizer files
|
| 8 |
tokenizer = MarianTokenizer.from_pretrained(model_path)
|
| 9 |
|
| 10 |
# Load the ONNX model
|
| 11 |
onnx_model_path = "./model.onnx"
|
| 12 |
session = ort.InferenceSession(onnx_model_path)
|
| 13 |
|
| 14 |
+
def translate_text(input_texts, max_length=512):
|
| 15 |
+
# Tokenize the input texts
|
| 16 |
+
inputs = tokenizer(input_texts, return_tensors="np", padding=True, truncation=True, max_length=max_length)
|
| 17 |
+
input_ids = inputs["input_ids"].astype(np.int64)
|
| 18 |
+
attention_mask = inputs["attention_mask"].astype(np.int64)
|
| 19 |
+
|
| 20 |
+
# Initialize variables for decoding
|
| 21 |
+
batch_size = input_ids.shape[0]
|
| 22 |
+
decoder_input_ids = np.array([[tokenizer.pad_token_id]] * batch_size, dtype=np.int64) # Start with pad token
|
| 23 |
+
|
| 24 |
+
# Generate output tokens iteratively
|
| 25 |
+
for _ in range(max_length):
|
| 26 |
+
# Run the ONNX model
|
| 27 |
+
ort_outputs = session.run(
|
| 28 |
+
None,
|
| 29 |
+
{
|
| 30 |
+
"input_ids": input_ids,
|
| 31 |
+
"attention_mask": attention_mask,
|
| 32 |
+
"decoder_input_ids": decoder_input_ids,
|
| 33 |
+
},
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Get the next token logits (output of the ONNX model)
|
| 37 |
+
next_token_logits = ort_outputs[0][:, -1, :] # Shape: (batch_size, vocab_size)
|
| 38 |
+
|
| 39 |
+
# Greedy decoding: select the token with the highest probability
|
| 40 |
+
next_tokens = np.argmax(next_token_logits, axis=-1) # Shape: (batch_size,)
|
| 41 |
+
|
| 42 |
+
# Append the next tokens to the decoder input for the next iteration
|
| 43 |
+
decoder_input_ids = np.concatenate([decoder_input_ids, next_tokens[:, None]], axis=-1)
|
| 44 |
+
|
| 45 |
+
# Stop if all sequences have reached the EOS token
|
| 46 |
+
if all(tokenizer.eos_token_id in sequence for sequence in decoder_input_ids):
|
| 47 |
+
break
|
| 48 |
+
|
| 49 |
+
# Decode the output tokens to text
|
| 50 |
+
translations = tokenizer.batch_decode(decoder_input_ids, skip_special_tokens=True)
|
| 51 |
+
return translations
|
| 52 |
|
| 53 |
# Gradio interface
|
| 54 |
+
def gradio_translate(input_texts):
|
| 55 |
+
translations = translate_text(input_texts)
|
| 56 |
+
return translations
|
| 57 |
+
|
| 58 |
+
# Create the Gradio interface
|
| 59 |
interface = gr.Interface(
|
| 60 |
+
fn=gradio_translate,
|
| 61 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text to translate...", label="Input Text"),
|
| 62 |
+
outputs=gr.Textbox(label="Translated Text"),
|
| 63 |
+
title="ONNX English to French Translation",
|
| 64 |
+
description="Translate English text to French using a MarianMT ONNX model.",
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Launch the Gradio app
|
| 68 |
interface.launch()
|