Spaces:
Runtime error
Runtime error
seawolf2357
commited on
Commit
•
5906ade
1
Parent(s):
a0ed680
initial commit
Browse files
app.py
CHANGED
@@ -1,16 +1,31 @@
|
|
1 |
-
import gradio
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
])
|
12 |
-
|
13 |
-
sepia_img /= sepia_img.max()
|
14 |
-
return sepia_img
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ko-en")
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ko-en")
|
7 |
+
|
8 |
+
device = 0 if torch.cuda.is_available() else -1
|
9 |
+
|
10 |
+
def translate(text, src_lang, tgt_lang):
|
11 |
+
"""
|
12 |
+
Translate the text from source lang to target lang
|
13 |
+
"""
|
14 |
+
translation_pipeline = pipeline("translation", model=model, tokenizer=tokenizer, src_lang=src_lang, tgt_lang=tgt_lang, max_length=400, device=device)
|
15 |
+
result = translation_pipeline(text)
|
16 |
+
return result[0]['translation_text']
|
17 |
+
|
18 |
+
demo = gr.Interface(
|
19 |
+
fn=translate,
|
20 |
+
inputs=[
|
21 |
+
gr.components.Textbox(label="Text"),
|
22 |
|
23 |
+
],
|
24 |
+
outputs=["text"],
|
25 |
+
examples=[["Building a translation demo with Gradio is so easy!", "eng_Latn", "spa_Latn"]],
|
26 |
+
cache_examples=False,
|
27 |
+
title="Translation Demo",
|
28 |
+
description="This demo is a simplified version of the original [NLLB-Translator](https://huggingface.co/spaces/Narrativaai/NLLB-Translator) space"
|
29 |
+
)
|
|
|
|
|
30 |
|
31 |
+
demo.launch()
|