Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,32 @@
|
|
| 1 |
import gradio
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
gradio_interface = gradio.Interface(
|
| 7 |
-
fn=
|
| 8 |
inputs="text",
|
| 9 |
outputs="text",
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
],
|
| 14 |
-
title="REST API with Gradio and Huggingface Spaces",
|
| 15 |
-
description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
|
| 16 |
-
article="© Tom Söderlund 2022"
|
| 17 |
)
|
| 18 |
-
gradio_interface.launch()
|
|
|
|
| 1 |
import gradio
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
|
| 4 |
+
|
| 5 |
+
model_name = "zaanind/nllb-ensi-v1-tuning"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
tokenizer.src_lang = "eng_Latn"
|
| 10 |
+
|
| 11 |
+
def translate(text):
|
| 12 |
+
inputs = tokenizer(text=text, return_tensors="pt")
|
| 13 |
+
translated_tokens = model.generate(
|
| 14 |
+
**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["sin_Sinh"]
|
| 15 |
+
)
|
| 16 |
+
translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 17 |
+
return translation
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def nmtapifunc(text):
|
| 21 |
+
text = translate(text)
|
| 22 |
+
return text
|
| 23 |
|
| 24 |
gradio_interface = gradio.Interface(
|
| 25 |
+
fn=nmtapifunc,
|
| 26 |
inputs="text",
|
| 27 |
outputs="text",
|
| 28 |
+
title="En-Si NMT",
|
| 29 |
+
description="",
|
| 30 |
+
article="© zaanind 2024"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
+
gradio_interface.launch()
|