someshb07 commited on
Commit
691539b
·
verified ·
1 Parent(s): 808767e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def get_fores_code(language):
2
+ # Loop through the list of languages
3
+ for entry in languages['languages']:
4
+ if entry['name'].lower() == language.lower(): # Compare to the 'name' field in the dictionary
5
+ return entry['code']
6
+ return None # Return None if no match is found
7
+
8
+
9
+ def translate_text(text, destination_language):
10
+ # Convert language name to language code using get_fores_code function
11
+ dest_code = get_fores_code(destination_language)
12
+
13
+ # Ensure the code is valid (for debugging purposes)
14
+ print(f"Destination language code: {dest_code}")
15
+
16
+ # Call the translation pipeline with the correct language codes
17
+ translation = pipe(text,
18
+ src_lang='eng_Latn',
19
+ tgt_lang=dest_code) # Pass the actual code (no quotes)
20
+
21
+ return translation[0]['translation_text']
22
+
23
+ demo = gr.Interface(fn=translate_text,
24
+ inputs= [
25
+ gr.TextArea(label='Insert the text in English to translate'),
26
+ gr.Dropdown(
27
+ label='Select the language to translate',
28
+ choices=[lang['name'] for lang in languages['languages']], # Extract the 'name' field
29
+ value='English'
30
+ )
31
+ ],
32
+ outputs=[gr.TextArea(label='This is the translated text')])
33
+
34
+ demo.launch()