Spaces:
Build error
Build error
Download app.py from neena2024/MultiLanguageTranslator: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/neena2024/MultiLanguageTranslator/resolve/main/app.py
- Command line
-
hf download hf://spaces/neena2024/MultiLanguageTranslator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/neena2024/MultiLanguageTranslator/resolve/main/app.py
1.48 kB
| import torch | |
| import gradio as gr | |
| # Use a pipeline as a high-level helper | |
| from transformers import pipeline | |
| text_translation = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.float16) | |
| # Load the JSON data from the file | |
| with open('language.json', 'r') as file: | |
| language_data = json.load(file) | |
| def get_FLORES_code_from_language(language): | |
| for entry in language_data: | |
| if entry['Language'].lower() == language.lower(): | |
| return entry['FLORES-200 code'] | |
| return None | |
| def translate_text(text, destination_language): | |
| # text = "Hello Friends, How are you?" | |
| dest_code= get_FLORES_code_from_language(destination_language) | |
| translation = text_translator(text, | |
| src_lang="eng_Latn", | |
| tgt_lang=dest_code) | |
| return translation[0]["translation_text"] | |
| gr.close_all() | |
| # demo = gr.Interface(fn=summary, inputs="text",outputs="text") | |
| demo = gr.Interface(fn=translate_text, | |
| inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian "], label="Select Destination Language")], | |
| outputs=[gr.Textbox(label="Translated text",lines=4)], | |
| title="@GenAILearniverse Project 4: Multi language translator", | |
| description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.") | |
| demo.launch(share=True) |