chburhan64 commited on
Commit
da59205
·
verified ·
1 Parent(s): 49e710c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # Load Hugging Face translation model
5
+ model_name = "Helsinki-NLP/opus-mt-en-ur" # ✅ Pretrained English to Urdu Model
6
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
7
+ model = MarianMTModel.from_pretrained(model_name)
8
+
9
+ # Define the translation function
10
+ def translate_to_urdu(text):
11
+ # Tokenize input text
12
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
13
+
14
+ # Generate translation
15
+ translated = model.generate(**inputs)
16
+
17
+ # Decode the translation
18
+ output_text = tokenizer.decode(translated[0], skip_special_tokens=True)
19
+ return output_text
20
+
21
+ # Create a Gradio interface for the translator
22
+ translator = gr.Interface(
23
+ fn=translate_to_urdu,
24
+ inputs=gr.Textbox(lines=2, placeholder="Enter English text..."),
25
+ outputs="text",
26
+ title="English to Urdu Translator",
27
+ description="Enter English text and get the Urdu translation instantly.",
28
+ theme="soft",
29
+ )
30
+
31
+ # Launch the Gradio app
32
+ translator.launch()