Spaces:
Running
Running
File size: 1,229 Bytes
b190ac0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import gradio as gr
from transformers import pipeline
import spaces
import torch
# Check if CUDA is available and set device accordingly
device = 0 if torch.cuda.is_available() else -1
# Initialize the translation pipeline with the device argument
translator = pipeline("translation", model="facebook/m2m100_418M", device=device)
LANGS = ["hi", "zh", "fr", "en"]
@spaces.GPU
def translate(text, src_lang, tgt_lang):
"""
Translate the text from source lang to target lang
"""
translation = translator(text, src_lang=src_lang, tgt_lang=tgt_lang)
return translation[0]['translation_text']
demo = gr.Interface(
fn=translate,
inputs=[
gr.components.Textbox(label="Text"),
gr.components.Dropdown(label="Source Language", choices=LANGS),
gr.components.Dropdown(label="Target Language", choices=LANGS),
],
outputs=["text"],
examples=[["Building a translation demo with Gradio is so easy!", "en", "fr"]],
cache_examples=False,
title="Translation Demo",
description="This demo is a simplified version of the original [NLLB-Translator](https://huggingface.co/spaces/Narrativaai/NLLB-Translator) space"
)
demo.queue(default_concurrency_limit=8)
demo.launch() |