qasimgill commited on
Commit
eab46ad
·
verified ·
1 Parent(s): 65fcb8f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+ import gradio as gr
4
+
5
+ # Load the pre-trained model and tokenizer
6
+ model_name = "Helsinki-NLP/opus-mt-en-ur"
7
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
8
+ model = MarianMTModel.from_pretrained(model_name)
9
+
10
+ # Define the translation function
11
+ def translate_english_to_urdu(text):
12
+ # Tokenize the input text
13
+ tokenized_text = tokenizer.prepare_seq2seq_batch([text], return_tensors="pt")
14
+
15
+ # Perform the translation
16
+ translated_tokens = model.generate(**tokenized_text)
17
+
18
+ # Decode the translated tokens to a string
19
+ translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
20
+
21
+ return translated_text
22
+
23
+ # Create a Gradio interface
24
+ def gradio_translator(input_text):
25
+ # Translate the input text
26
+ translated_text = translate_english_to_urdu(input_text)
27
+ return translated_text
28
+
29
+ # Define the Gradio interface
30
+ interface = gr.Interface(
31
+ fn=gradio_translator, # Function to call
32
+ inputs=gr.Textbox(lines=2, placeholder="Enter English text here..."), # Input component
33
+ outputs=gr.Textbox(lines=2, placeholder="Urdu translation will appear here..."), # Output component
34
+ title="English to Urdu Translator", # Title of the interface
35
+ description="Translate English text to Urdu using the Helsinki-NLP/opus-mt-en-ur model.",
36
+ examples=[
37
+ ["Hello, how are you?"],
38
+ ["What is your name?"],
39
+ ["I love programming."]
40
+ ]
41
+ )
42
+
43
+ # Launch the Gradio interface
44
+ interface.launch()