kasper-boy commited on
Commit
0674d75
·
verified ·
1 Parent(s): 25a7c2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -5,34 +5,41 @@ import json
5
  # Use a pipeline as a high-level helper
6
  from transformers import pipeline
7
 
8
- text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M",
9
- torch_dtype=torch.bfloat16)
10
 
11
  # Load the JSON data from the file
12
  with open('language.json', 'r') as file:
13
  language_data = json.load(file)
14
 
 
 
 
15
  def get_FLORES_code_from_language(language):
16
  for entry in language_data:
17
  if entry['Language'].lower() == language.lower():
18
  return entry['FLORES-200 code']
19
  return None
20
 
21
-
22
  def translate_text(text, destination_language):
23
- # text = "Hello Friends, How are you?"
24
- dest_code= get_FLORES_code_from_language(destination_language)
25
- translation = text_translator(text,
26
- src_lang="eng_Latn",
27
- tgt_lang=dest_code)
28
- return translation[0]["translation_text"]
29
-
 
30
  gr.close_all()
31
-
32
- # demo = gr.Interface(fn=summary, inputs="text",outputs="text")
33
- demo = gr.Interface(fn=translate_text,
34
- inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian "], label="Select Destination Language")],
35
- outputs=[gr.Textbox(label="Translated text",lines=4)],
36
- title="@GenAILearniverse Project 4: Multi language translator",
37
- description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
38
- demo.launch()
 
 
 
 
 
5
  # Use a pipeline as a high-level helper
6
  from transformers import pipeline
7
 
8
+ # Initialize the translation pipeline
9
+ text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
10
 
11
  # Load the JSON data from the file
12
  with open('language.json', 'r') as file:
13
  language_data = json.load(file)
14
 
15
+ # Extract language names from the JSON data
16
+ language_names = [entry['Language'] for entry in language_data]
17
+
18
  def get_FLORES_code_from_language(language):
19
  for entry in language_data:
20
  if entry['Language'].lower() == language.lower():
21
  return entry['FLORES-200 code']
22
  return None
23
 
 
24
  def translate_text(text, destination_language):
25
+ dest_code = get_FLORES_code_from_language(destination_language)
26
+ if dest_code:
27
+ translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
28
+ return translation[0]["translation_text"]
29
+ else:
30
+ return "Destination language code not found."
31
+
32
+ # Create and launch the Gradio interface
33
  gr.close_all()
34
+ demo = gr.Interface(
35
+ fn=translate_text,
36
+ inputs=[
37
+ gr.Textbox(label="Input text to translate", lines=6),
38
+ gr.Dropdown(language_names, label="Select Destination Language")
39
+ ],
40
+ outputs=[gr.Textbox(label="Translated text", lines=4)],
41
+ title="Multi-language Translator",
42
+ description="This application translates any English text to multiple languages."
43
+ )
44
+
45
+ demo.launch(auth=('user', 'test@123'))