Spaces:
Runtime error
Runtime error
File size: 2,388 Bytes
0777539 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import os
import torch
import gradio as gr
import time
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from code_flores_latest import flores_codes_latest
def load_models():
model_name_dict = {'nllb-distilled-600M': 'facebook/nllb-200-distilled-600M'}
model_dict = {}
for call_name, real_name in model_name_dict.items():
print('\tLoading model: %s' % call_name)
model = AutoModelForSeq2SeqLM.from_pretrained(real_name)
tokenizer = AutoTokenizer.from_pretrained(real_name)
model_dict[call_name + '_model'] = model
model_dict[call_name + '_tokenizer'] = tokenizer
return model_dict
def translation(source, target, text):
model_name = 'nllb-distilled-600M'
source_code = flores_codes_latest.get(source, None)
target_code = flores_codes_latest.get(target, None)
if not source_code or not target_code:
return "<p>Error: Language code not found.</p>"
model = model_dict[model_name + '_model']
tokenizer = model_dict[model_name + '_tokenizer']
translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=source_code, tgt_lang=target_code)
output = translator(text, max_length=400)
output_text = output[0]['translation_text']
formatted_output = f"<p><strong>Original Text ({source}):</strong><br>{text}</p><p><strong>Translated Text ({target}):</strong> <span style='color: red;'>{output_text}</span></p>"
return formatted_output
if __name__ == '__main__':
print('\tInitializing models')
model_dict = load_models()
lang_names = list(flores_codes_latest.keys())
source_dropdown = gr.Dropdown(lang_names, label='Source', allow_custom_value=True)
target_dropdown = gr.Dropdown(lang_names, label='Target', allow_custom_value=True)
textbox = gr.Textbox(lines=5, label="Input text")
title = "nllb-distilled-600M - Example implementation"
description = f"Nots: please note that not all translations are accurate, and some models codes are not accepted . "
initial_output_value = "<p>Translation results will appear here.</p>"
output_html = gr.HTML(label="Translation Result", value=initial_output_value)
iface = gr.Interface(fn=translation, inputs=[source_dropdown, target_dropdown, textbox], outputs=output_html,
title=title, description=description)
iface.launch()
|