Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# prompt: create a gradio for this
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and model
|
| 8 |
+
model_name = "jsbeaudry/creole-translation-nllb-600M"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 11 |
+
# Assuming the necessary imports and model loading from the previous code are in place
|
| 12 |
+
|
| 13 |
+
def translate_text(text):
|
| 14 |
+
# Set the source and target language codes
|
| 15 |
+
src_lang = "eng_Latn"
|
| 16 |
+
tgt_lang = "hat_Latn"
|
| 17 |
+
tokenizer_ = tokenizer
|
| 18 |
+
model_ = model
|
| 19 |
+
|
| 20 |
+
# Set tokenizer to source language
|
| 21 |
+
tokenizer_.src_lang = src_lang
|
| 22 |
+
|
| 23 |
+
# Tokenize the input
|
| 24 |
+
inputs = tokenizer_(text, return_tensors="pt")
|
| 25 |
+
|
| 26 |
+
# Find the BOS token ID for the target language
|
| 27 |
+
forced_bos_token_id = tokenizer_.convert_tokens_to_ids(tgt_lang)
|
| 28 |
+
|
| 29 |
+
# Move model to GPU if available
|
| 30 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
+
model_ = model_.to(device)
|
| 32 |
+
inputs = inputs.to(device)
|
| 33 |
+
|
| 34 |
+
# Generate translation
|
| 35 |
+
generated_tokens = model_.generate(
|
| 36 |
+
**inputs,
|
| 37 |
+
forced_bos_token_id=forced_bos_token_id,
|
| 38 |
+
max_length=100
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Decode and print
|
| 42 |
+
translated = tokenizer_.batch_decode(generated_tokens, skip_special_tokens=True)
|
| 43 |
+
return translated[0]
|
| 44 |
+
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=translate_text,
|
| 47 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text to translate"),
|
| 48 |
+
outputs="text",
|
| 49 |
+
title="English to Haitian Creole Translation",
|
| 50 |
+
description="Translate English text to Haitian Creole using a fine-tuned NLLB model."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch()
|