kasper-boy commited on
Commit
25a7c2f
1 Parent(s): ebe4b40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -41
app.py CHANGED
@@ -1,42 +1,38 @@
 
1
  import gradio as gr
2
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
-
4
- # Load pre-trained model and tokenizer
5
- model_name = "t5-small"
6
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
-
9
- # Function to translate text
10
- def translate_text(text, source_lang, target_lang):
11
- input_text = f"translate {source_lang} to {target_lang}: {text}"
12
- inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
13
- outputs = model.generate(**inputs)
14
- translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
15
- return translation
16
-
17
- # List of Indian languages
18
- indian_languages = [
19
- "as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te", "ur"
20
- ]
21
-
22
- # Supported languages
23
- languages = ["en", "fr", "de", "es", "it"] + indian_languages
24
-
25
- # Create Gradio interface
26
- def translate_interface(text, source_lang, target_lang):
27
- return translate_text(text, source_lang, target_lang)
28
-
29
- iface = gr.Interface(
30
- fn=translate_interface,
31
- inputs=[
32
- gr.Textbox(lines=2, placeholder="Enter text to translate"),
33
- gr.Dropdown(choices=languages, label="Source Language"),
34
- gr.Dropdown(choices=languages, label="Target Language")
35
- ],
36
- outputs="text",
37
- title="Hugging Face Translation App",
38
- description="Translate text from one language to another using a T5 model."
39
- )
40
-
41
- if __name__ == "__main__":
42
- iface.launch()
 
1
+ import torch
2
  import gradio as gr
3
+ import json
4
+
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()