Laiba-Huggingface commited on
Commit
448f3b5
Β·
verified Β·
1 Parent(s): 4254e24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -57
app.py CHANGED
@@ -1,73 +1,48 @@
1
  import gradio as gr
 
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
- # Load pre-trained models
 
 
 
 
5
  model_en_to_ur = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-ur")
6
  tokenizer_en_to_ur = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ur")
7
 
8
  model_ur_to_en = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ur-en")
9
  tokenizer_ur_to_en = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ur-en")
10
 
 
11
  def translate(text, direction):
12
- """Translates text based on selected direction."""
13
- if direction == "English β†’ Urdu":
14
  tokenizer, model = tokenizer_en_to_ur, model_en_to_ur
15
  else:
16
  tokenizer, model = tokenizer_ur_to_en, model_ur_to_en
17
 
 
18
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
19
- translated = model.generate(**inputs)
20
- return tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
21
-
22
- # Custom CSS for Better UI
23
- custom_css = """
24
- h1 {
25
- text-align: center;
26
- font-size: 2em;
27
- color: #007BFF;
28
- }
29
-
30
- .gradio-container {
31
- font-family: 'Arial', sans-serif;
32
- background-color: #f9f9f9;
33
- padding: 20px;
34
- border-radius: 10px;
35
- }
36
-
37
- .input-container textarea {
38
- font-size: 16px !important;
39
- border-radius: 12px !important;
40
- border: 2px solid #007BFF !important;
41
- }
42
-
43
- .button {
44
- background-color: #007BFF !important;
45
- color: white !important;
46
- font-size: 16px;
47
- border-radius: 10px !important;
48
- padding: 10px 20px !important;
49
- }
50
-
51
- .output-container {
52
- background-color: white;
53
- padding: 15px;
54
- border-radius: 12px;
55
- border: 1px solid #ddd;
56
- }
57
- """
58
-
59
- # Gradio UI
60
- with gr.Blocks(css=custom_css) as app:
61
- gr.Markdown("# 🌍 **English ↔ Urdu Translator**")
62
- gr.Markdown("Translate text between English and Urdu effortlessly!")
63
-
64
- with gr.Row():
65
- text_input = gr.Textbox(placeholder="Type text here...", label="Enter Text", lines=3)
66
- direction = gr.Radio(["English β†’ Urdu", "Urdu β†’ English"], label="Translation Direction", value="English β†’ Urdu")
67
-
68
- translate_button = gr.Button("πŸ”„ Translate")
69
- output_text = gr.Textbox(label="Translated Text", interactive=False)
70
-
71
- translate_button.click(translate, inputs=[text_input, direction], outputs=output_text)
72
 
73
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
  from transformers import MarianMTModel, MarianTokenizer
4
 
5
+ # Ensure required libraries are installed
6
+ import os
7
+ os.system("pip install sentencepiece")
8
+
9
+ # Load models and tokenizers
10
  model_en_to_ur = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-ur")
11
  tokenizer_en_to_ur = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ur")
12
 
13
  model_ur_to_en = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ur-en")
14
  tokenizer_ur_to_en = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ur-en")
15
 
16
+ # Function to translate text
17
  def translate(text, direction):
18
+ if direction == "English to Urdu":
 
19
  tokenizer, model = tokenizer_en_to_ur, model_en_to_ur
20
  else:
21
  tokenizer, model = tokenizer_ur_to_en, model_ur_to_en
22
 
23
+ # Tokenize input text
24
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Generate translation
27
+ with torch.no_grad():
28
+ translated = model.generate(**inputs)
29
+
30
+ # Decode output text
31
+ output = tokenizer.decode(translated[0], skip_special_tokens=True)
32
+ return output
33
+
34
+ # Gradio interface
35
+ interface = gr.Interface(
36
+ fn=translate,
37
+ inputs=[
38
+ gr.Textbox(label="Enter Text"),
39
+ gr.Radio(["English to Urdu", "Urdu to English"], label="Translation Direction", value="English to Urdu"),
40
+ ],
41
+ outputs=gr.Textbox(label="Translated Text"),
42
+ title="English ↔ Urdu Translator",
43
+ description="Translate text between English and Urdu using a neural machine translation model.",
44
+ )
45
+
46
+ # Launch the Gradio app
47
+ if __name__ == "__main__":
48
+ interface.launch()