Commit
·
ddf9cdc
1
Parent(s):
2d59f11
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
# Check if CUDA (GPU) is available
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import T5ForConditionalGeneration, PreTrainedTokenizerFast
|
| 6 |
+
|
| 7 |
+
# Define the path to the checkpoint directory
|
| 8 |
+
checkpoint_dir = "/home/only_sainaa/home/Huggingface/marianmt_conversion/results/checkpoint-221496"
|
| 9 |
+
|
| 10 |
+
# Load the model
|
| 11 |
+
model = T5ForConditionalGeneration.from_pretrained(checkpoint_dir)
|
| 12 |
+
model.eval()
|
| 13 |
+
# Load the tokenizer using PreTrainedTokenizerFast
|
| 14 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained(checkpoint_dir)
|
| 15 |
+
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
|
| 18 |
+
# Move the model to the same device (GPU or CPU)
|
| 19 |
+
model.to(device)
|
| 20 |
+
|
| 21 |
+
# Function to perform translation using the model
|
| 22 |
+
def translate_text(input_text):
|
| 23 |
+
# Tokenize the input text
|
| 24 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
| 25 |
+
|
| 26 |
+
# Move the input tensors to the same device as the model
|
| 27 |
+
inputs = {k: v.to(device) for k, v in inputs.items() if k in ['input_ids', 'attention_mask']}
|
| 28 |
+
|
| 29 |
+
# Generate translation
|
| 30 |
+
outputs = model.generate(**inputs)
|
| 31 |
+
|
| 32 |
+
# Decode the output to human-readable text
|
| 33 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
+
|
| 35 |
+
return translated_text
|
| 36 |
+
|
| 37 |
+
# Create a Gradio interface
|
| 38 |
+
gr_interface = gr.Interface(
|
| 39 |
+
fn=translate_text,
|
| 40 |
+
inputs="text",
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="Mongolian Cyrillic to Mongolian Script Model",
|
| 43 |
+
description="Enter text in Mongolian Cyrillic"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Launch the Gradio interface
|
| 47 |
+
gr_interface.launch()
|