#import gradio as gr | |
#from transformers import pipeline | |
#from fairseq.models.transformer import TransformerModel | |
# Load the English to Urdu translation model from the transformers library | |
#model_name_or_path = "Helsinki-NLP/opus-mt-en-ur" | |
#model_name_or_path = TransformerModel.from_pretrained('samiulhaq/iwslt-bt-en-ur') | |
#translator = pipeline("translation", model=model_name_or_path, tokenizer=model_name_or_path) | |
# Create a Gradio interface for the translation app | |
#def translate(text): | |
# Use the translator pipeline to translate the input text | |
# result = translator(text, max_length=500) | |
# return result[0]['translation_text'] | |
#input_text = gr.inputs.Textbox(label="Input English Text") | |
#output_text = gr.outputs.Textbox(label="Output Urdu Text") | |
#app = gr.Interface(fn=translate, inputs=input_text, outputs=output_text) | |
# Launch the app | |
#app.launch() | |
import gradio as gr | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
# Load the English to Urdu translation model from the transformers library | |
model_name_or_path = "aryanc55/english-urdu" | |
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) | |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path) | |
# Create a Gradio interface for the translation app | |
def translate(text): | |
# Tokenize the input text | |
inputs = tokenizer(text, return_tensors="pt") | |
# Use the model to generate the translated text | |
outputs = model.generate(inputs["input_ids"], max_length=500, early_stopping=True) | |
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return translated_text | |
input_text = gr.inputs.Textbox(label="Input English Text") | |
output_text = gr.outputs.Textbox(label="Output Urdu Text") | |
app = gr.Interface(fn=translate, inputs=input_text, outputs=output_text) | |
# Launch the app | |
app.launch() | |