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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()