sunbal7 commited on
Commit
af5468d
·
verified ·
1 Parent(s): 8edd67e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -1,37 +1,23 @@
1
  import gradio as gr
2
- from transformers import MarianMTModel, MarianTokenizer
3
 
4
- # Load the MarianMT model and tokenizer for English to Urdu translation
5
- def load_model():
6
- model_name = 'Helsinki-NLP/opus-mt-en-ur' # English to Urdu pre-trained model
7
- model = MarianMTModel.from_pretrained(model_name)
8
- tokenizer = MarianTokenizer.from_pretrained(model_name)
9
- return model, tokenizer
10
 
11
- # Define the translation function
12
- def translate(text, model, tokenizer):
13
- # Prepare the text for translation
14
- translated = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
15
- # Generate translation
16
- translated = model.generate(**translated)
17
- # Decode the translated text
18
- translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
19
- return translated_text
20
 
21
- # Load model and tokenizer
22
- model, tokenizer = load_model()
 
 
 
 
 
 
23
 
24
- # Define Gradio interface function
25
- def gradio_interface(text):
26
- return translate(text, model, tokenizer)
27
-
28
- # Set up Gradio interface for the translation web app
29
- interface = gr.Interface(fn=gradio_interface,
30
- inputs="text", # User input type (text box)
31
- outputs="text", # Output type (translated text)
32
- live=True, # Updates live as user types
33
- title="English to Urdu Translation Web App",
34
- description="Translate English text to Urdu using the MarianMT model.")
35
-
36
- # Launch the Gradio interface
37
- interface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Load the pre-trained translation model
5
+ translator = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
 
 
 
 
6
 
7
+ # Translation function
8
+ def translate_text(text):
9
+ translation = translator(text, max_length=400)
10
+ return translation[0]['translation_text']
 
 
 
 
 
11
 
12
+ # Gradio interface
13
+ interface = gr.Interface(
14
+ fn=translate_text,
15
+ inputs=gr.Textbox(lines=2, placeholder="Enter English text here..."),
16
+ outputs=gr.Textbox(lines=2, label="Urdu Translation"),
17
+ title="English to Urdu Translation",
18
+ description="Translate English text to Urdu using a pre-trained model."
19
+ )
20
 
21
+ # Launch the app
22
+ if __name__ == "__main__":
23
+ interface.launch()