Akhil Koduri commited on
Commit
b826719
1 Parent(s): b5487d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ # Use a pipeline as a high-level helper
4
+ from transformers import pipeline
5
+ import gradio as gr
6
+
7
+ # Initialize the translation pipeline with the google-t5/t5-base model
8
+ text_translator = pipeline("translation", model="google/t5-base")
9
+
10
+ # Dictionary mapping destination languages to their T5-compatible names
11
+ language_mapping = {
12
+ "German": "German",
13
+ "Eastern Panjabi": "Punjabi",
14
+ "Sanskrit": "Sanskrit",
15
+ "Urdu": "Urdu",
16
+ "Tamil": "Tamil",
17
+ "Telugu": "Telugu",
18
+ "Yue Chinese": "Chinese",
19
+ "Chinese (Simplified)": "Chinese",
20
+ "Chinese (Traditional)": "Chinese",
21
+ "Hindi": "Hindi",
22
+ "French": "French",
23
+ "Spanish": "Spanish"
24
+ }
25
+
26
+ def translate_text(text, destination_language):
27
+ dest_language = language_mapping.get(destination_language)
28
+
29
+ if dest_language is None:
30
+ return "Unsupported language selected."
31
+
32
+ # T5 model requires a specific format for the translation task
33
+ translation_prompt = f"translate English to {dest_language}: {text}"
34
+
35
+ translation = text_translator(translation_prompt)
36
+ return translation[0]["translation_text"]
37
+
38
+ gr.close_all()
39
+
40
+ demo = gr.Interface(
41
+ fn=translate_text,
42
+ inputs=[
43
+ gr.Textbox(label="Input text to translate", lines=6),
44
+ gr.Dropdown(list(language_mapping.keys()), label="Select destination language")
45
+ ],
46
+ outputs=[gr.Textbox(label="Translated text", lines=4)],
47
+ title="Language translator (model- Google T5 Base)",
48
+ description="THIS APPLICATION WILL BE USED TO TRANSLATE ENGLISH TO MULTIPLE LANGUAGES",
49
+ theme=gr.themes.Soft(),
50
+ concurrency_limit=16
51
+ )
52
+
53
+ demo.launch()