lodhrangpt commited on
Commit
54473e1
1 Parent(s): b2ffe46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,38 +1,40 @@
1
  import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
3
 
4
- # Load the translation model and tokenizer from Hugging Face
5
- model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
-
9
- # Define the translation function
10
- def translate_text(text, target_language):
11
- model_name = f"Helsinki-NLP/opus-mt-en-{target_language}"
12
- translator = pipeline("translation", model=model_name)
13
- translation = translator(text)[0]['translation_text']
14
- return translation
15
-
16
- # Define available languages
17
  languages = {
18
- "english": "English",
19
- "spanish": "Spanish",
20
- "french": "French",
21
- "german": "German",
22
- "chinese": "Chinese (Mandarin)",
23
- "japanese": "Japanese",
24
- "korean": "Korean",
25
- "italian": "Italian",
26
- "portuguese": "Portuguese",
27
- "russian": "Russian",
28
- "hindi": "Hindi",
29
- "arabic": "Arabic",
30
- "dutch": "Dutch",
31
- "turkish": "Turkish",
32
- "greek": "Greek",
33
- "urdu": "Urdu"
34
  }
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Set up the Gradio interface with a submit button and side-by-side layout
38
  with gr.Blocks() as iface:
 
1
  import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
3
 
4
+ # Define available languages with their corresponding model suffixes
 
 
 
 
 
 
 
 
 
 
 
 
5
  languages = {
6
+ "english": "en",
7
+ "spanish": "es",
8
+ "french": "fr",
9
+ "german": "de",
10
+ "chinese": "zh",
11
+ "japanese": "ja",
12
+ "korean": "ko",
13
+ "italian": "it",
14
+ "portuguese": "pt",
15
+ "russian": "ru",
16
+ "hindi": "hi",
17
+ "arabic": "ar",
18
+ "dutch": "nl",
19
+ "turkish": "tr",
20
+ "greek": "el",
21
+ "urdu": "ur"
22
  }
23
 
24
+ # Define the translation function
25
+ def translate_text(text, target_language):
26
+ # Get the correct model name based on the target language
27
+ target_language_code = languages.get(target_language.lower())
28
+
29
+ if target_language_code:
30
+ model_name = f"Helsinki-NLP/opus-mt-en-{target_language_code}"
31
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
32
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
33
+ translator = pipeline("translation", model=model, tokenizer=tokenizer)
34
+ translation = translator(text)[0]['translation_text']
35
+ return translation
36
+ else:
37
+ return "Error: Language not supported or incorrect."
38
 
39
  # Set up the Gradio interface with a submit button and side-by-side layout
40
  with gr.Blocks() as iface: