Spaces:
Runtime error
Runtime error
Upload interface_MT_fo_en.py
Browse files- interface_MT_fo_en.py +45 -0
interface_MT_fo_en.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
2 |
+
|
3 |
+
# Path to your model's checkpoints
|
4 |
+
model_checkpoint_path = "barbaroo/nllb_200_600M_fo_en"
|
5 |
+
|
6 |
+
# Load the tokenizer and model
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint_path)
|
8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint_path)
|
9 |
+
|
10 |
+
# Function to perform translation
|
11 |
+
def translate(text, model, tokenizer):
|
12 |
+
# Encode the text
|
13 |
+
inputs = tokenizer.encode(text, return_tensors="pt")
|
14 |
+
|
15 |
+
# Generate translation outputs
|
16 |
+
outputs = model.generate(inputs, max_length=80, num_beams=4, early_stopping=True)
|
17 |
+
|
18 |
+
# Decode and return the translated text
|
19 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
+
return translated_text
|
21 |
+
|
22 |
+
# Sample translation (You can comment this out when deploying to Gradio)
|
23 |
+
faroese_text = "Granskingarvirksemi fevnir bæði um føroyskar og altjóða granskingarverkætlanir."
|
24 |
+
translated_text = translate(faroese_text, model, tokenizer)
|
25 |
+
print(translated_text)
|
26 |
+
|
27 |
+
# Gradio Interface setup
|
28 |
+
# Ensure Gradio is installed
|
29 |
+
!pip install gradio
|
30 |
+
|
31 |
+
# Importing Gradio
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
# Define the Gradio interface
|
35 |
+
def gradio_translate(text):
|
36 |
+
return translate(text, model, tokenizer)
|
37 |
+
|
38 |
+
iface = gr.Interface(fn=gradio_translate,
|
39 |
+
inputs="text",
|
40 |
+
outputs="text",
|
41 |
+
title="Faroese to English Translator",
|
42 |
+
description="Translate Faroese text to English using a state-of-the-art model.")
|
43 |
+
|
44 |
+
# Launch the interface
|
45 |
+
iface.launch()
|