DemahAlmutairi commited on
Commit
99788b0
·
verified ·
1 Parent(s): 516091c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -1,33 +1,56 @@
1
- # Import the logging utility from the Hugging Face transformers library
2
- from transformers.utils import logging
3
-
4
- # Set the logging verbosity to 'error' level
5
- # This means only error messages will be printed, suppressing warnings, info, and debug messages
6
- logging.set_verbosity_error()
7
  from transformers import pipeline
8
  import torch
9
- import gradio as gr
10
-
11
- translator = pipeline(task="translation",
12
- model="facebook/nllb-200-distilled-600M",
13
- torch_dtype=torch.bfloat16)
14
 
 
 
 
 
 
 
15
 
16
-
17
- def translation_func(text, source, target):
18
- text_translated = translator(text, src_lang=source, tgt_lang= target)
19
- return text_translated
20
-
21
- iface = gr.Interface(
22
- fn=translation_func,
23
- inputs = [
24
- gr.Textbox(label = "Enter your text"),
25
- gr.Dropdown( ["eng_Latn", "fra_Latn", "arz_Arab"], label = "Source Language" ),
26
- gr.Dropdown(["eng_Latn", "fra_Latn", "arz_Arab"], label = "Target Language"),
27
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  ],
29
- outputs=gr.Textbox(label="Translation")
 
 
30
  )
31
 
32
- iface.launch()
33
-
 
 
1
+ import gradio as gr
 
 
 
 
 
2
  from transformers import pipeline
3
  import torch
 
 
 
 
 
4
 
5
+ # Load the translation model
6
+ translator = pipeline(
7
+ task="translation",
8
+ model="facebook/nllb-200-distilled-600M",
9
+ torch_dtype=torch.bfloat16
10
+ )
11
 
12
+ # Define the supported languages (this is a subset of possible languages, expand as needed)
13
+ languages = {
14
+ "English": "eng_Latn",
15
+ "French": "fra_Latn",
16
+ "Arabic": "arb_Arab",
17
+ "Spanish": "spa_Latn",
18
+ "German": "deu_Latn",
19
+ "Chinese (Simplified)": "zho_Hans",
20
+ "Hindi": "hin_Deva"
21
+ }
22
+
23
+ # Function to translate text based on selected languages
24
+ def translate(text, src_lang, tgt_lang):
25
+ if src_lang not in languages or tgt_lang not in languages:
26
+ return "Invalid language selection"
27
+
28
+ # Step 1: Define source and target languages using the input language codes
29
+ source_language = languages[src_lang] # Look up the source language
30
+ target_language = languages[tgt_lang] # Look up the target language
31
+
32
+ # Step 2: Call the translator function with the given text, source, and target languages
33
+ result = translator(text, src_lang=source_language, tgt_lang=target_language)
34
+
35
+ # Step 3: Extract the translated text from the result
36
+ translation = result[0]['translation_text']
37
+
38
+ # Step 4: Return the translated text
39
+ return translation
40
+
41
+ # Gradio Interface
42
+ demo = gr.Interface(
43
+ fn=translate, # Translation function
44
+ inputs=[
45
+ gr.Textbox(label="Input Text", placeholder="Enter text to translate"), # Text input
46
+ gr.Dropdown(choices=list(languages.keys()), label="Source Language", value="English"), # Source language
47
+ gr.Dropdown(choices=list(languages.keys()), label="Target Language", value="French") # Target language
48
  ],
49
+ outputs=gr.Textbox(label="Translated Text"), # Output
50
+ title="Translation using NLLB-200", # Title of the interface
51
+ description="Select source and target languages, then translate the text." # Description
52
  )
53
 
54
+ # Launch the Gradio interface
55
+ demo.launch()
56
+