Rekham1110 commited on
Commit
d8f7fac
·
verified ·
1 Parent(s): ed512b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # Load the model and tokenizer
5
+ model_name = 'Helsinki-NLP/opus-mt-en-hi' # Model for English to Hindi
6
+ model = MarianMTModel.from_pretrained(model_name)
7
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
8
+
9
+ # Function to translate English to Hindi
10
+ def translate_to_hindi(text):
11
+ # Tokenize input text
12
+ tokens = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
13
+ # Perform translation
14
+ translated_tokens = model.generate(**tokens)
15
+ # Decode the translation
16
+ translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
17
+ return translated_text
18
+
19
+ # Gradio interface
20
+ iface = gr.Interface(fn=translate_to_hindi, inputs="text", outputs="text",
21
+ title="English to Hindi Translator",
22
+ description="Enter English text and get the Hindi translation.")
23
+
24
+ # Launch the interface
25
+ iface.launch()