mayankchugh-learning commited on
Commit
03f2a52
1 Parent(s): 3084d58

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a pipeline as a high-level helper
2
+ from transformers import pipeline
3
+ import torch
4
+ import json
5
+ import gradio as gr
6
+
7
+ text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
8
+
9
+ # Load the JSON table
10
+ with open('language.json') as f:
11
+ language_data = json.load(f)
12
+
13
+ def get_flores_200_code(language):
14
+ for code in language_data:
15
+ if code['Language'] == language:
16
+ return code['FLORES-200 code']
17
+ return None
18
+
19
+ def translate_text(text, destination_language):
20
+
21
+ # text = "Hello friends how are you?"
22
+ dest_code = get_flores_200_code(destination_language)
23
+
24
+ translation = text_translator(text,
25
+ src_lang="eng_Latn",
26
+ tgt_lang=dest_code)
27
+ return translation[0]["translation_text"]
28
+
29
+ gr.close_all()
30
+
31
+ demo = gr.Interface(fn=translate_text,
32
+ inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["English", "German", "Eastern Panjabi", "Sanskrit", "Urdu", "Tamil", "Telugu", "Yue Chinese", "Chinese (Simplified)", "Chinese (Traditional)", "Hindi", "French", "Spanish"],label="Select destination language")],
33
+ outputs=[gr.Textbox(label="Translated text", lines=4)],
34
+ title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Project 4: Multi Language translator",
35
+ description="THIS APPLICATION WILL BE USED TO TRANSLATE ANY ENGLISH TO MULTIPLE LANGUAGES",
36
+ concurrency_limit=16)
37
+ demo.launch()
38
+
39
+ # Hello Friends, Welcome to my channel. I hope this video helps you understand AI.
40
+ # "Hello friends how are you?"