Spaces:
Running
Running
bolleman
commited on
Commit
β’
b190ac0
0
Parent(s):
initial
Browse files- README.md +15 -0
- app.py +38 -0
- requirements.txt +5 -0
README.md
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Translation
|
3 |
+
emoji: π
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: red
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.40.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
# Gradio Translation Demo
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import spaces
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Check if CUDA is available and set device accordingly
|
7 |
+
device = 0 if torch.cuda.is_available() else -1
|
8 |
+
|
9 |
+
# Initialize the translation pipeline with the device argument
|
10 |
+
translator = pipeline("translation", model="facebook/m2m100_418M", device=device)
|
11 |
+
|
12 |
+
LANGS = ["hi", "zh", "fr", "en"]
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def translate(text, src_lang, tgt_lang):
|
16 |
+
"""
|
17 |
+
Translate the text from source lang to target lang
|
18 |
+
"""
|
19 |
+
translation = translator(text, src_lang=src_lang, tgt_lang=tgt_lang)
|
20 |
+
return translation[0]['translation_text']
|
21 |
+
|
22 |
+
demo = gr.Interface(
|
23 |
+
fn=translate,
|
24 |
+
inputs=[
|
25 |
+
gr.components.Textbox(label="Text"),
|
26 |
+
gr.components.Dropdown(label="Source Language", choices=LANGS),
|
27 |
+
gr.components.Dropdown(label="Target Language", choices=LANGS),
|
28 |
+
],
|
29 |
+
outputs=["text"],
|
30 |
+
examples=[["Building a translation demo with Gradio is so easy!", "en", "fr"]],
|
31 |
+
cache_examples=False,
|
32 |
+
title="Translation Demo",
|
33 |
+
description="This demo is a simplified version of the original [NLLB-Translator](https://huggingface.co/spaces/Narrativaai/NLLB-Translator) space"
|
34 |
+
)
|
35 |
+
|
36 |
+
demo.queue(default_concurrency_limit=8)
|
37 |
+
|
38 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
sentencepiece
|
5 |
+
spaces
|