Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import json
|
5 |
+
|
6 |
+
# model_path = "C:\\Users\\abdul\\Documents\\genaiproj\\genai\\Models\models--facebook--nllb-200-distilled-600M\\snapshots\\f8d333a098d19b4fd9a8b18f94170487ad3f821d"
|
7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
8 |
+
# text_translator = pipeline("translation", model=model_path, torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
# with open('C:\\Users\\abdul\\Documents\\genaiproj\\genai\\Files\\language.json', 'r') as file:
|
11 |
+
with open('language.json', 'r') as file:
|
12 |
+
|
13 |
+
language_data = json.load(file)
|
14 |
+
|
15 |
+
def get_flores_code_from_language(language_name):
|
16 |
+
for entry in language_data:
|
17 |
+
if entry["Language"].lower() == language_name.lower():
|
18 |
+
return entry["FLORES-200 code"]
|
19 |
+
return "Language not found."
|
20 |
+
|
21 |
+
def translate_text(text, destination_language):
|
22 |
+
# text = input("Enter the text to translate: ")
|
23 |
+
dest_code = get_flores_code_from_language(destination_language)
|
24 |
+
translation = text_translator(text,
|
25 |
+
src_lang ="eng_Latn",
|
26 |
+
tgt_lang =dest_code)
|
27 |
+
return translation[0]["translation_text"]
|
28 |
+
|
29 |
+
|
30 |
+
gr.close_all()
|
31 |
+
|
32 |
+
# demo = gr.Interface(fn=summary, inputs="text", outputs="text")
|
33 |
+
|
34 |
+
demo = gr.Interface(
|
35 |
+
fn=translate_text,
|
36 |
+
inputs=[gr.Textbox(label="Input text to translate", lines=6), gr.Dropdown(["German", "French", "Tamil", "Romanian", "Arabic"], label="Select Destination Language")],
|
37 |
+
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
38 |
+
title="Multilanguage Translator",
|
39 |
+
theme="soft",
|
40 |
+
description="Translate text to any language in seconds!")
|
41 |
+
|
42 |
+
demo.launch(share=True)
|