Muhammad Anas Akhtar commited on
Commit
8cf3f87
·
verified ·
1 Parent(s): 33bd68b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -5,41 +5,62 @@ import json
5
  # Use a pipeline as a high-level helper
6
  from transformers import pipeline
7
 
8
- model_path= ("../Models/models--facebook--nllb-200-distilled-600M/snapshots"
9
  "/f8d333a098d19b4fd9a8b18f94170487ad3f821d")
10
 
11
  text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M",
12
- torch_dtype=torch.bfloat16)
13
 
14
- # text_translator = pipeline("translation", model=model_path,
15
- # torch_dtype=torch.bfloat16)
16
  # Load the JSON data from the file
17
  with open('language.json', 'r') as file:
18
  language_data = json.load(file)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def get_FLORES_code_from_language(language):
 
21
  for entry in language_data:
22
  if entry['Language'].lower() == language.lower():
23
  return entry['FLORES-200 code']
 
 
 
 
 
24
  return None
25
 
26
-
27
  def translate_text(text, destination_language):
28
- # text = "Hello Friends, How are you?"
29
- dest_code= get_FLORES_code_from_language(destination_language)
 
 
30
  translation = text_translator(text,
31
- src_lang="eng_Latn",
32
- tgt_lang=dest_code)
33
  return translation[0]["translation_text"]
34
 
35
  gr.close_all()
36
 
37
- # demo = gr.Interface(fn=summary, inputs="text",outputs="text")
38
- demo = gr.Interface(fn=translate_text,
39
- inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian "], label="Select Destination Language")],
40
- outputs=[gr.Textbox(label="Translated text",lines=4)],
41
- title="@GenAILearniverse Project 4: Multi language translator",
42
- description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
43
- demo.launch()
44
-
 
 
45
 
 
 
 
5
  # Use a pipeline as a high-level helper
6
  from transformers import pipeline
7
 
8
+ model_path = ("../Models/models--facebook--nllb-200-distilled-600M/snapshots"
9
  "/f8d333a098d19b4fd9a8b18f94170487ad3f821d")
10
 
11
  text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M",
12
+ torch_dtype=torch.bfloat16)
13
 
 
 
14
  # Load the JSON data from the file
15
  with open('language.json', 'r') as file:
16
  language_data = json.load(file)
17
 
18
+ # Get all available languages (excluding duplicates with different scripts)
19
+ available_languages = []
20
+ seen_languages = set()
21
+ for entry in language_data:
22
+ base_language = entry['Language'].split('(')[0].strip()
23
+ if base_language not in seen_languages:
24
+ available_languages.append(base_language)
25
+ seen_languages.add(base_language)
26
+
27
+ # Sort languages alphabetically
28
+ available_languages.sort()
29
+
30
  def get_FLORES_code_from_language(language):
31
+ # First try exact match
32
  for entry in language_data:
33
  if entry['Language'].lower() == language.lower():
34
  return entry['FLORES-200 code']
35
+
36
+ # If no exact match, try matching the base language name
37
+ for entry in language_data:
38
+ if entry['Language'].lower().startswith(language.lower()):
39
+ return entry['FLORES-200 code']
40
  return None
41
 
 
42
  def translate_text(text, destination_language):
43
+ dest_code = get_FLORES_code_from_language(destination_language)
44
+ if dest_code is None:
45
+ return f"Error: Could not find FLORES code for language {destination_language}"
46
+
47
  translation = text_translator(text,
48
+ src_lang="eng_Latn",
49
+ tgt_lang=dest_code)
50
  return translation[0]["translation_text"]
51
 
52
  gr.close_all()
53
 
54
+ demo = gr.Interface(
55
+ fn=translate_text,
56
+ inputs=[
57
+ gr.Textbox(label="Input text to translate", lines=6),
58
+ gr.Dropdown(choices=available_languages, label="Select Destination Language")
59
+ ],
60
+ outputs=[gr.Textbox(label="Translated text", lines=4)],
61
+ title="@GenAILearniverse Project 4: Multi Language Translator",
62
+ description="This application translates English text to multiple languages. Select your desired target language from the dropdown menu."
63
+ )
64
 
65
+ if __name__ == "__main__":
66
+ demo.launch()