Mds21 commited on
Commit
3a69c47
1 Parent(s): 1e74823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -2,14 +2,32 @@ import gradio as gr
2
  from transformers import pipeline
3
  translation_pipeline_german = pipeline('translation_en_to_de')
4
  translation_pipeline_hindi = pipeline('translation_en_to_de')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # results = translation_pipeline('I love ice cream')
6
  # results[0]['translation_text']
7
  def translate_transformers(English,Language_To_Translate):
8
  if "German" in Language_To_Translate:
9
  results = translation_pipeline_german(English)
 
10
  elif "Hindi" in Language_To_Translate:
11
- results = translation_pipeline_hindi(English)
12
- return results[0]['translation_text']
 
13
 
14
  interface = gr.Interface(fn=translate_transformers,
15
  inputs=[gr.inputs.Textbox(lines=2, placeholder='Text to translate'),
 
2
  from transformers import pipeline
3
  translation_pipeline_german = pipeline('translation_en_to_de')
4
  translation_pipeline_hindi = pipeline('translation_en_to_de')
5
+ def hindi_translate(text_):
6
+ from transformers import MarianMTModel, MarianTokenizer
7
+ # Load the English to Hindi translation model and tokenizer
8
+ model_name = "Helsinki-NLP/opus-mt-en-hi"
9
+ model = MarianMTModel.from_pretrained(model_name)
10
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
11
+ # English text to be translated
12
+ english_text = text_
13
+ # Tokenize the input text
14
+ inputs = tokenizer.encode(english_text, return_tensors="pt")
15
+ # Perform translation
16
+ translation = model.generate(inputs)
17
+ # Decode the translation
18
+ hindi_translation = tokenizer.decode(translation[0], skip_special_tokens=True)
19
+ return hindi_translation
20
+
21
  # results = translation_pipeline('I love ice cream')
22
  # results[0]['translation_text']
23
  def translate_transformers(English,Language_To_Translate):
24
  if "German" in Language_To_Translate:
25
  results = translation_pipeline_german(English)
26
+ return results[0]['translation_text']
27
  elif "Hindi" in Language_To_Translate:
28
+ results = hindi_translate(English)
29
+ return results
30
+
31
 
32
  interface = gr.Interface(fn=translate_transformers,
33
  inputs=[gr.inputs.Textbox(lines=2, placeholder='Text to translate'),